传递 id 来更新表是否安全?

我想用用户添加的产品数量更新表格。


我首先选择表中所有具有相同代码的产品,然后在输入名称中写上id(表中)。


客户端:


    <input name="<?php echo $product['id']; ?>" type="number" min="1" placeholder="Quantity" required/>


服务器端:


    <?php

 session_start();

 require __DIR__."/../connectDB.php";

 $memberId = $_SESSION['user_id'];


 foreach ($_POST as $prodId => $quantity) {

   $stmt =$conn->prepare("UPDATE tbl_product SET stock = ? WHERE id = ? AND member_id = ?");

     $stmt->bind_param("iii", $quantity, $prodId, $memberId);

     $stmt->execute();

     $stmt->close();

 }

  ?>

它有效,但安全吗?


湖上湖
浏览 149回答 2
2回答

一只名叫tom的猫

它与通过互联网传输数据的任何其他方法一样安全,但您的方法似乎有点奇怪。正如评论所指出的,处理这个问题的常用方法是包含一个隐藏字段来传递应该对用户隐藏的数据。您仍然需要验证数据,因为用户也可以编辑隐藏的输入;他们只是从普通用户那里稍微混淆了它。<form type="post" ...>&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; <input type="hidden" name="product_id" value="<?php echo $product['id']; ?>">&nbsp; &nbsp; <input type="number" name="quantity" min="1" placeholder="Quantity" required >&nbsp; &nbsp; ...</form><?php$productId = $_POST['product_id'];$quantity = $_POST['quantity'];$query = 'UPDATE product SET quantity = ? WHERE product = ?'...编辑如果您需要从表单传递产品数据数组,您可以使用数组语法[]来命名表单输入。<?php// This array just represents the data coming from your DB.// Change it to suit.$products = [&nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; 'id' => 1281,&nbsp; &nbsp; &nbsp; &nbsp; 'quantity' => 7&nbsp; &nbsp; ],&nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; 'id' => 234,&nbsp; &nbsp; &nbsp; &nbsp; 'quantity' => 2&nbsp; &nbsp; ],&nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; 'id' => 3455,&nbsp; &nbsp; &nbsp; &nbsp; 'quantity' => 25&nbsp; &nbsp; ],&nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; 'id' => 64563,&nbsp; &nbsp; &nbsp; &nbsp; 'quantity' => 84&nbsp; &nbsp; ],&nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; 'id' => 235,&nbsp; &nbsp; &nbsp; &nbsp; 'quantity' => 7&nbsp; &nbsp; ],];if ($_SERVER['REQUEST_METHOD'] === 'POST') {&nbsp; &nbsp; var_dump($_POST);&nbsp; &nbsp; // Handle the form processing here.}?><form method="post">&nbsp; &nbsp; <?php foreach ($products as $product): ?>&nbsp; &nbsp; &nbsp; &nbsp; <input type="hidden" name="product[<?php echo $product['id']; ?>]" value="<?php echo $product['id']; ?>">&nbsp; &nbsp; &nbsp; &nbsp; <label for="product[<?php echo $product['id']; ?>]"><?php echo $product['id']; ?>:</label>&nbsp; &nbsp; &nbsp; &nbsp; <input type="number" name="product[<?php echo $product['id']; ?>]" value="<?php echo $product['quantity']; ?>">&nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; <?php endforeach; ?>&nbsp; &nbsp; <input type="submit" name="submit" value="Submit"></form>输出:array (size=2)&nbsp; 'product' =>&nbsp;&nbsp; &nbsp; array (size=5)&nbsp; &nbsp; &nbsp; 1281 => string '52' (length=2)&nbsp; &nbsp; &nbsp; 234 => string '2' (length=1)&nbsp; &nbsp; &nbsp; 3455 => string '25' (length=2)&nbsp; &nbsp; &nbsp; 64563 => string '84' (length=2)&nbsp; &nbsp; &nbsp; 235 => string '7' (length=1)&nbsp; 'submit' => string 'Submit' (length=6)然后,您可以遍历此数据以创建 SQL 查询。

紫衣仙女

当我想接受来自用户的信息时,我使用了这种方法,我想确保他们不会修改任何内容。我传递了一个我想用作参考的值的加盐 MD5 哈希值,这对应用程序很重要,如果用户更改会影响代码的执行或登录。HTML 表单<input name="<?php echo $product['id']; ?>" type="number" min="1" placeholder="Quantity" required/><input name="<?php echo $product['id']; ?>_hash" type="hidden" value="<?php md5($product['id'] . "rand0mC0d3") ?>">服务器端<?php&nbsp;session_start();&nbsp;require __DIR__."/../connectDB.php";&nbsp;$memberId = $_SESSION['user_id'];foreach ($_POST as $prodId => $quantity) {&nbsp; &nbsp;if(is_numeric($propId) and $_POST[$propId . '_hash'] == md5($propId . "rand0mC0d3") ){&nbsp; &nbsp; &nbsp;if(is_numeric($quantity)){&nbsp; &nbsp; &nbsp; &nbsp; $stmt =$conn->prepare("UPDATE tbl_product SET stock = ? WHERE id = ? AND member_id = ?");&nbsp; &nbsp; &nbsp; &nbsp; $stmt->bind_param("iii", $quantity, $prodId, $memberId);&nbsp; &nbsp; &nbsp; &nbsp; $stmt->execute();&nbsp; &nbsp; &nbsp; &nbsp; $stmt->close();&nbsp; &nbsp; &nbsp;}else{&nbsp; &nbsp; &nbsp; &nbsp; // Value not numeric.&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp;}}?>注意** 我想提出一个概念,即传递一个加盐的 md5 哈希来验证我们想要用来识别记录的值没有被修改。您可能需要对其进行更改以满足您的特定需求。快乐编码。
打开App,查看更多内容
随时随地看视频慕课网APP