2014-10-07 53 views
1

我是一名初学者。我在一张桌子上显示了10名学生的学生证和学生姓名。对每个学生ID,应该有一个checkbox(动态)。当我点击ADD按钮时,所有查看的学生详细信息(ID,姓名)必须插入另一个databasetable。我该怎么办?如何在php中获取多个选中(动态)复选框的值?

+0

你有什么迄今所做? – roullie 2014-10-07 05:27:19

回答

2

使用复选框名称为数组, 例如:

<form method="post" action="" id="frm_id"> 
     <input type="checkbox" name="chkid[]" value="10,Anu" />Anu 
     <input type="checkbox" name="chkid[]" value="11,Raj" />Raj 
     <input type="checkbox" name="chkid[]" value="12,Ram" />Ram 
     <input type="checkbox" name="chkid[]" value="13,xxx" />xxx 
     <input type="checkbox" name="chkid[]" value="14,yyy" />yyyy 
     <input type="checkbox" name="chkid[]" value="15,zzz" />zzz 
     <input type="checkbox" name="chkid[]" value="16,qqqq" />qqqq 
     <input type="submit" value="Insert" name="sub"/> 
     </form> 
     <?php 
     if(isset($_POST['sub'])) 
     { 
     $id=$_POST['chkid']; 
     for($i=0;$i<count($id);$i++) 
     { 
     $exp=explode(',',$id[$i]);//Explode id and name 
     echo 'id='.$exp[0].',Name='.$exp[1];echo "<br>"; 
     echo $query="INSERT INTO tbl_student (id,name) values ('$exp[0]','$exp[1]')";echo "<br><br>"; 
     } 
     } 
     ?> 
+0

我认为我有问题的复选框的值。这是错的吗? – MIA 2014-10-07 06:35:38

+0

**危险**:您很容易受到[SQL注入攻击](http://bobby-tables.com/)**,您需要[防御](http://stackoverflow.com/questions/ 60174/best-way-to-prevent-sql -injection-in-php)自己从。 – Quentin 2014-10-07 06:45:20

+0

@MIA使用php标记,”/> – 2014-10-07 06:51:37

0

尝试使用复选框元素的数组是这样的:

<input type="checkbox" name="months[]" value="feb">February<br> 
<input type="checkbox" name="months[]" value="mar">March<br> 
<input type="checkbox" name="months[]" value="apr">April<br> 
1
<form method="post" action="pageurl"> 
     <input type="checkbox" name="studentid[]" value="1,Student1" />Student1 
     <input type="checkbox" name="studentid[]" value="2,Student2" />Student2 
     <input type="checkbox" name="studentid[]" value="3,Student3" />Student3 
     <input type="checkbox" name="studentid[]" value="4,Student4" />Student4 

     <input type="submit" /> 
     </form> 
     <?php 

     $id=$_POST['studentid']; 
     foreach($id as $student) 
     { 
     $extract = explode(',',$student); 
     $query="INSERT INTO student (id,name) values ('$extract[0]','$extract[1]')"; 
     } 

     ?> 
+0

**危险**:你很容易[SQL注入攻击](http://bobby-tables.com/)**,你需要[防守](http ://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php)自己从。 – Quentin 2014-10-07 06:44:51

相关问题