2011-11-25 53 views
0

我想从用户提交的表单中插入一些值到数据库中。PHP插入数据库 - 复杂

$id = mysql_insert_id();//Gets ID of last value inserted into related table 
foreach($_POST[ 'equipment' ] as $checkBoxIndex => $checkBoxValue) { 

mysql_query("INSERT INTO recipeequipment (recipeid, equipmentid, datetimeentered) VALUES('".$id."', '".$checkBoxValue."', '".$datetime."')"); 

} 

需要加入到这个INSERT语句中的唯一的事情是数量,这是在一个类似的方式复选框举行,但在文本框。像这样:

<?php while($rowequipment = mysql_fetch_assoc($sqlequipment)) { 
echo '<input type="checkbox" name="equipment[]" value="'.$rowequipment['equipmentid'].'"/> 
     <input type="text" name="count[]" id="count[]" size="3" value="" />' 
    .$rowequipment['description']."<br />"; 
     }?> 

我需要帮助的一块是第二个输入列。我需要将与复选框相对应的值插入到插入语句中,但我不确定如何执行此操作。

+1

我会推荐阅读关于sql注入 –

+0

好的SQL注入漏洞。请记住,如果我驾驶这辆数字卡车将他们带到您的网站并在您的服务器上做一些甜甜圈? –

+0

为什么选择复选框?为什么你不能只使用文本字段? –

回答

1

忽略所有vunrabilities在你的代码现在,做这样的事情应该让你得到一个唯一的ID,并匹配复选框,直到输入:

<?php while($rowequipment = mysql_fetch_assoc($sqlequipment)) { 
echo '<input type="checkbox" name="equipment['.$rowequipment['equipmentid'].']" value="'.$rowequipment['equipmentid'].'"/> 
     <input type="text" name="count['.$rowequipment['equipmentid'].']" id="count-'.$rowequipment['equipmentid'].'" size="3" value="" />' 
    .$rowequipment['description']."<br />"; 
     }?> 

然后一旦提交表单,你会就那么你忽略更新的行,他们不检查关闭或 -

foreach ($_POST['equipment'] as $id => $checkboxValue) { 
    $textInputValue = $_POST['count'][$id]; 
    // do something with the above value here 
} 

虽然,我不明白为什么你用这样的复选框:通过每个复选框通过执行类似简单循环什么?