我遇到下面表单的问题,每当我点击一个项目它捕获项目并更新mySQL表时,它只显示1项(如何让它显示我检查过的所有项目? )。
此外,当我输入数量时,它不会在mySQL表上更新,当我放入多个数量的每个项目时它只是说0。
如果可以的话,你们能帮忙吗?
<tr>
<th>Shirts</th>
<th>Quantity</th>
</tr>
<tr>
<td>
<br />
<input type="checkbox" name="items" value="SH01" /><label for="rd1">Obey T-Shirt: $9.99</label></div> <br />
<input type="checkbox" name="items" value="SH02" /><label for="rd1">Obey Professor: $9.99</label></div> <br />
<input type="checkbox" name="items" value="SH03" /><label for="rd1">Hustle T-Shirt: $9.99</label></div> <br />
<input type="checkbox" name="items" value="SH04" /><label for="rd1">Hip-Hop Support: $9.99</label></div> <br />
<input type="checkbox" name="items" value="SH05" /><label for="rd1">90's Shirt: $9.99</label></div> <br />
<input type="checkbox" name="items" value="SH06" /><label for="rd1">DOPE Shirt: $9.99</label></div> <br />
<br />
</td>
<td>
<br />
<input type="text" name="qty" size ="2"/><br/>
<input type="text" name="qty" size="2"/><br/>
<input type="text" name="qty" size="2"/><br/>
<input type="text" name="qty" size="2"/><br/>
<input type="text" name="qty" size="2"/><br/>
<input type="text" name="qty" size="2"/><br/>
<br />
</td>
</tr>
<tr>
<td>
<br />
<input type="checkbox" name="items" value="SO1" /><label for="rd1">Shoe - Red Lace: $19.99</label></div><br />
<input type="checkbox" name="items" value="SO2" /><label for="rd1">Shoe - Red High Top: $19.99</label></div><br />
<input type="checkbox" name="items" value="SO3" /><label for="rd1">Shoe - White: $19.99</label></div><br />
<input type="checkbox" name="items" value="SO4" /><label for="rd1">Shoe - Black: $19.99</label></div><br />
<input type="checkbox" name="items" value="SO5" /><label for="rd1">Shoe - Black High Top: $19.99</label></div><br />
<input type="checkbox" name="items" value="SO6" /> <label for="rd1">Red Basketball: $19.99</label></div><br />
<br />
</td>
<td>
<br />
<input type="text" name="qty[]" size ="2"/><br/>
<input type="text" name="qty[]" size="2"/><br/>
<input type="text" name="qty[]" size="2"/><br/>
<input type="text" name="qty[]" size="2"/><br/>
<input type="text" name="qty[]" size="2"/><br/>
<input type="text" name="qty[]" size="2"/><br/>
<br />
</td>
</tr>
<tr>
<td>
<br />
<input type="checkbox" name="items" value="SN1" /> <label for="rd1">Snapback Bullets: $29.99</label></div><br />
<input type="checkbox" name="items" value="SN2" /><label for="rd1">Snapback: $29.99</label></div><br />
<input type="checkbox" name="items" value="SN3" /><label for="rd1">Snapback Bullets: $29.99</label></div><br />
<input type="checkbox" name="items" value="SN4" /><label for="rd1">Snapback Bullets: $29.99</label></div><br />
<input type="checkbox" name="items" value="SN5" /><label for="rd1">Snapback Bullets: $29.99</label></div><br />
<input type="checkbox" name="items" value="SN6" /><label for="rd1">Snapback Bullets: $29.99</label></div><br />
<br />
</td>
<td>
<br />
<input type="text" name="qty" size ="2"/><br/>
<input type="text" name="qty" size="2"/><br/>
<input type="text" name="qty" size="2"/><br/>
<input type="text" name="qty" size="2"/><br/>
<input type="text" name="qty" size="2"/><br/>
<input type="text" name="qty" size="2"/><br/>
<br />
</td>
</tr>
</tr>
</table>
<br />
<input type="submit" name="submit">
</form>
<?php
if (isset($_POST['submit']))
{
$con = mysql_connect('$localhost','$url','$pass');
if (!$con)
{
die("Could Not Connect: " . mysql_error());
}
mysql_select_db("$username",$con);
$sql = "INSERT INTO Order_Information(Order_ID,Order_Items,Order_Quantity) VALUES (null,'$_POST[items]','$_POST[qty]')";
mysql_query($sql,$con);
mysql_close($con);
}
?>
答案 0 :(得分:3)
您需要做的第一件事是在foreach循环中更新,以便为每个选中的项插入一行;其次,您需要使用数组将数量字段与这些项关联。
将数量字段名称更改为包含项目代码的数组:
<input type="text" name="qty['SO1']" size ="2"/><br/>
<input type="text" name="qty['SO2']" size="2"/><br/>
etc ...
然后在你的PHP中:
foreach ($_POST['items'] as $item) {
foreach($_POST['qty'] as $key => $value) {
$quantity = 0;
if ($key == $item) {
$quantity = $value;
}
}
$sql = "INSERT INTO Order_Information(Order_Items,Order_Quantity) VALUES ('$item', '$quantity')";
.
.
}
答案 1 :(得分:1)
HTML
<input type="checkbox" name="items" value="SH01" />
<input type="text" name="qty_SH01" ..../>
PHP
foreach ($_POST as $key => $val) {
if (!preg_match("/^SH/", $key)) continue;
$qty = $_POST['qty_' . $key];
$sql = "INSERT INTO Order_Information(Order_Items,Order_Quantity) VALUES ('$key', '$qty')";
}
答案 2 :(得分:-1)
使用$_POST[items]
时,您忘记了撇号 - $_POST['items']
。
要获取所有选中的复选框,您需要一个循环:
if(!empty($_POST['check_list']))
{
foreach($_POST['items'] as $item)
{
....
}
}