php - 更新查询,更新多个数据

标签 php mysql

我想使用此代码更新多个数据,但问题是,当我尝试它时,它会更新具有相同类别 id 的整个数据,并且它应该单独更新。可能有什么解决方案。请帮忙。预先感谢:)

<?php
if (isset($_GET['pid'])){
    $view="";
    $targetID = $_GET['pid'];
    $sql = mysql_query("SELECT specs, category_id, price FROM specs WHERE category_id='$targetID'");
                                $productCount = mysql_num_rows($sql);
                                    if($productCount > 0){
                                        while($row = mysql_fetch_array($sql)){
                                        $specs = $row["specs"];
                                        $category_id = $row["category_id"]; 
                                        $price = $row["price"];
                                        $view .=  '<div class="control-group">
                            <label class="control-label" >Specs</label>
                            <div class="controls">
                            <input type="text"  placeholder="Specs" name="specs" value="'.$specs.'">
                            </div>
                            </div>
                            <div class="control-group">
                            <label class="control-label" >Price</label>
                            <div class="controls">
                            <input type="text"  placeholder="Price" name="price" value="PHP&nbsp;'.number_format($price, 2).'">
                            </div>
                            </div>';
                                }
                                }
        }
    ?>
    <?php
    if (isset($_POST['specs'])){

    $pid = mysql_real_escape_string($_POST['thisID']);
    $specs = mysql_real_escape_string($_POST['specs']);;
    $price = mysql_real_escape_string($_POST['price']);
    $sql= mysql_query("UPDATE specs SET specs='$specs', price='$price' WHERE category_id='$pid'");

    header("Location: manageproducts.php");
    exit();
    }
    ?>

这是 html。

 <div class="container">
    <div class="page-header">
      <h1>Manage Products</h1>
    </div>
    <div class="row-fluid ">

            <div class="box span12center-align" >
                <div class="box-header well" data-original-title>
                    <center><h2><i class="icon-edit"></i> Edit Specifications </h2></center>
                </div>

                <div class="box-content" >
                    <form class="form-horizontal" action="" method='post'>

                        <fieldset>

                         <?php echo $view; ?>


                          <div class="form-actions">
                            <input name="thisID" type="hidden" value="<?php echo $targetID; ?>">
                            <button type="submit" class="btn btn-primary" name="add_product">Update Item</button>
                            <button class="btn">Cancel</button>
                          </div>
                        </fieldset>
                    </form>
                </div>
            </div><!--/span-->

        </div><!--/row--></center>
        </div>
        </div>
        </div>

最佳答案

您需要将主键添加到要更新的行的 WHERE 子句中,现在您只需更新具有特定 category_id 的所有行。

因此,向表中添加一个主键 id(如果您的表还没有)并将其设置为自动递增。然后修改您的选择查询:

"SELECT id, specs, category_id, price FROM specs WHERE category_id='$targetID'"

将该 id 添加到隐藏的输入字段。

然后您可以像这样修改更新查询:

"UPDATE specs SET specs='$specs', price='$price' WHERE category_id='$pid' AND id='$id'"

SQL注入(inject)警报

您还应该知道,您编写的代码非常危险,容易出现 SQL injection 。永远不要直接在查询中使用 GET/POST 变量。请使用mysqli with prepared statementsPDO .

关于php - 更新查询,更新多个数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18159893/

相关文章:

php - SQL=INSERT INTO `#__user_usergroup_map` (`user_id` ,`group_id` ) VALUES (, 2) -- 数据库未更新

php - 散列/加盐时密码不匹配

php - Laravel SQL 条件查询

mysql查询获取学生未注册的所有单元

sql - 在连接表中选择所有不满足特定条件的记录

javascript - 如何在 php 或 javascript 中逐列读取文本文件

php - 使用 jquery 计算具有相同 MySQL 行 ID 的输入

php - 如何使用 file_get_contents 获取变量中的 HTTP 状态

java - 如何知道应用程序用户是否离线android应用程序?

php - 如何更新多行在 MySQL 的 int 列中添加值?