查询使用php将单行中的多个数据添加到mysql中

时间:2015-01-05 05:45:19

标签: php mysql

我想为单个行添加多个数据。我可以为注册人员添加权限。权限是代码,管理员,用户,审计。我可以为此人创建一个权限,但需要的是添加多个权限,因为具有用户权限的人员也应具有审核权限。

我所做的是,注册一个拥有单一权限的人并从会话中获取他们的信息,我一直使用直到会话结束。我工作得很完美。但我需要的是创建多个权限。请帮我php编码和mysql查询。我是php和mysql的新手(用于编程)


这是我创建的注册的Html表单。

<form class="form-inline"enctype="multipart/form-data" action="add.php" method="POST">
   <div class="form-group">  
   <label class="sr-only" for="Employee name">Employee name</label>
   <input type="text" class="form-control" id="employee name" placeholder="Enter Employee Name" name="empname">
  </div>
  <div class="form-group">  
   <label class="sr-only" for="Employee name">Employee ID</label>
   <input type="text" class="form-control" id="employee id" placeholder="Enter Employee ID" name="empid">
  </div>
  <div class="form-group">  
   <label class="sr-only" for="Employee password">Employee Password</label>
   <input type="password" class="form-control" id="employee name" placeholder="Enter Password" name="password">
  </div>
  <div class="form-group">  
   <label class="sr-only" for="Employee name">Confirm password</label>
   <input type="password" class="form-control" id="confirm password" placeholder="Re-Enter Password" name="repassword">
  </div>
    <div class="form-group">  
   <label class="sr-only" for="Employee Photo">Employee Photo</label>
   <input type="file" class="form-control" id="employee photo" placeholder="Upload Employee photo" name="photo" value="upload image">
  </div>
    <div class="form-group">  
   <label for="privilege">privilege</label>
   <select name="privilege" class="form-control">
   <option value="Admin">Admin</option>
   <option value="Super Admin">Super Admin</option>
   <option value="HR">HR</option>
   <option value="Manager">Manager</option>
   <option value="Client">Client</option>
    <option value="Audit">Audit</option>
     <option value="User">User</option>
      <option value="Coder">Coder</option>
      <option value="User & Audit">User and Audit</option>

   </select>
  </div>
  <br>
  <div class="form-group">
    <button type="submit" class="btn btn-primary">Register</button></div>
     <div class="form-group">
    <button type="reset" class="btn btn-primary">Reset</button>
</div>
 <div class="form-group">
    <a href="home.html"><button type="button" class="btn btn-primary">Cancel</button></a>
</div>

    </form>

add.php

<?php 

 //This is the directory where images will be saved 
 $target = "profiles/"; 
 $target = $target . basename( $_FILES['photo']['name']); 

 //This gets all the other information from the form 
 $empname=$_POST['empname']; 
 $empid=$_POST['empid']; 
 $pic=($_FILES['photo']['name']); 
 $password=$_POST['password']; 
 $repassword=$_POST['repassword']; 
 $privilege=$_POST['privilege'];


 // Connects to your Database 
 mysql_connect("localhost", "root", "") or die(mysql_error()) ; 
 mysql_select_db("records") or die(mysql_error()) ; 

 //Writes the information to the database 
 mysql_query("INSERT INTO `registration` VALUES ('$empname', '$empid', '$pic', '$password', '$repassword', '$privilege')") ; 

 //Writes the photo to the server 
 if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
 { 

 //Tells you if its all ok 
 echo "The file ".basename($_FILES['photo']['name']). " has been uploaded, and your information has been added to the directory"; 
 } 
 else { 

 //Gives and error if its not 
 echo "<p class='text-danger'>Sorry, there was a problem uploading your file.</p>"; 
 echo "<a href='register.php'>Click here for registration</a>";
 } 
 ?> 
  

SQL:

     

CREATE TABLE IF NOT NOT EXISTS registrationempname varchar(30)   NOT NULL,empid varchar(30)NOT NULL DEFAULT&#39;&#39;,photo   varchar(30)NOT NULL,password varchar(30)NOT NULL,
  repassword varchar(30)NOT NULL,privilege varchar(50)NOT NULL   )ENGINE = InnoDB DEFAULT CHARSET = latin1; enter code here

1 个答案:

答案 0 :(得分:0)

在这里你可以使用它来获得....制作你的选择字段名称数组[],这样你就可以在单行中插入多个值

 <select name="privilege[]" class="form-control">
 <option value="Admin">Admin</option>
 <option value="Super Admin">Super Admin</option>
 <option value="HR">HR</option>
 <option value="Manager">Manager</option>
 <option value="Client">Client</option>
 <option value="Audit">Audit</option>
 <option value="User">User</option>
 <option value="Coder">Coder</option>
 <option value="User & Audit">User and Audit</option>

 </select>

插入内爆时

$implode=implode(',',$_POST['privilege']);

在表字段名称

中插入此$ implode