【发布时间】:2014-10-18 16:38:37
【问题描述】:
好的,所以我有一个从 mysql 表中提取的 PHP 数组。该数组是根据表中的项目生成的,该表中的项目是频繁添加和删除的。我在项目名称旁边有一个按钮,“提交”。我希望按钮能够识别同一索引中的项目。然后它将提交的项目传递到新表。
<form class="omb_loginForm" action="inc/contribute_item.php" autocomplete="off" method="POST">
<?php
$item_array;
$index = 0;
$index_2 = 1;
$r = "r";
$b="b";
foreach ($item_array as $id_array){ ?>
<tr id="<?php echo $r.$index_2; ?>">
<td><?php echo $item_array[$index] ?></td>
<td> <?php echo $quantity_array[$index]; ?></td>
<td> <?php echo $price_array[$index];
$selectedItem = $item_array[$index]; ?>
<input type='hidden' name='hidden' value='<?php $selectedItem ?>'>
<input type='submit' name='submit' value"submit">
</form> </td>
<?php $index++;
$index_2++; ?>
</tr>
这里是 PHP:
if ($_POST['submit']) {
$connect = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
$user_contrib = $_SESSION['first_name'];
$selected = $selectedItem;
$connect->query("UPDATE `items` SET `purchased_by` = '$user_contrib' WHERE `name` = '$selected'");
}
【问题讨论】:
-
使用
mysqli时,您应该使用参数化查询和bind_param将用户数据添加到您的查询中。 请勿使用字符串插值来完成此操作,因为您将创建严重的SQL injection bugs。