简体   繁体   English

从SQL结果中识别特定的行

[英]Identifying specific row from SQL results

I have a list of 'orders' being pulled out, which consist of product name, description etc, one of the fields is quantity which is in an editable text box, next to that is an update button (which has an unique ID for that row pulled from the DB). 我有一个要提取的“订单”列表,其中包括产品名称,说明等,其中一个字段是数量,该数量位于可编辑的文本框中,旁边是一个更新按钮(该按钮具有唯一的ID)行从数据库中拉出)。 Now when the update button is pressed, I want the quantity for that product to be updated. 现在,当按下更新按钮时,我想要更新该产品的数量。 However i'm having problems getting the correct updated quantity to be matched with the ID of that row. 但是我在获取正确的更新数量与该行的ID匹配时遇到问题。

I can see that the problem is me setting the $quantity1 variable with just the last result pulled out inside the IF statement, but I can't think how to get it to relate the row i'm clicking on. 我可以看到问题是我将$ quantity1变量设置为仅在IF语句中提取了最后一个结果,但是我不认为如何使它与我单击的行相关。 Here is part of the code: 这是代码的一部分:

    echo "<td>".$row['uName']."</td>";
echo "<td>".$row['prodID']."</td>";?>
<form method="post" action="reserved.php">
<td><input name="quantity1" type="text" id="quantity1" size="1" value='<?= $qty ?>' />
<td><input name="order2"  id="order2" type="submit" class="button_add" value='<?= $row['ID']?>' /></td><?
echo "</tr>";       

}

}elseif(!empty($studyDir) && $rowCount == 0){
?>
<?
}
}
if (isset($_POST['order2'])){
$order2 = $_POST['order2'];
$quantity1 = $_POST['quantity1'];

\\echo $quantity1;

$link3 = mysql_connect('localhost', '******', '******');
$SQL1 = "UPDATE ybsinter_stock.reservedStock SET qty = $quantity1 WHERE ID = '$order2'";
$result1 = mysql_query($SQL1);  
mysql_close($link3);
unset($quantity1);
unset($order2);
header("Location:reserved.php");
}
?>  

I can't see your form ending ie there is no <\\form>. 我看不到您的表单结尾,即没有<\\ form>。 Also note that declaring forms in tables (except entirely enclosed in a td) is bad HTML, run your code through the W3C validator . 另请注意,在表格中声明表格(除非完全封装在td中)是错误的HTML,请通过W3C验证程序运行代码。

Also try PHP heredocs for outputting blocks of HTML with embedded data.... 也可以尝试使用PHP heredocs输出带有嵌入式数据的HTML块。

echo <<<EOF
<tr>
<td>{$row['uName']}</td>
<td>{$row['prodID']}</td>
<td>
    <form method="post" action="reserved.php">
    <input name="quantity1" type="text" id="quantity1" size="1" value="{$qty}" />
    // style this button right with CSS if you want ...
    <input name="order2"  id="order2" type="submit" class="button_add" value="{$row['ID']}" />
    </form>
</td>
</tr>
EOF;   

The above form will only submit data to your script with the id that you're interested in.. 上面的表单只会将您感兴趣的ID提交数据到脚本。

Your SQL query seems roughly correct, but beware of SQL injection - please bind your variables into your queries instead of inserting them. 您的SQL查询看起来大致正确,但要提防SQL注入 -请将变量绑定到查询中,而不要插入变量。 Use the mysqli or PDO libraries instead of the outdated basic mysql functions. 使用mysqliPDO库而不是过时的基本mysql函数。

$mysqli = new mysqli( /* your connection params here */ );
$sql1 = 'UPDATE ybsinter_stock.reservedStock SET qty = ? WHERE ID = ?';

$stmt = $mysqli->query( $sql1);
$stmt->bind_param( 'sd', $quantity1, $order2);
$result = $stmt->execute();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM