php - 我在更新表格中的产品数量时遇到问题

标签 php mysql mysqli

我在更新一种产品时遇到问题,假设我有交易 ID(tr_id) 25678,它有 3 种不同的产品。当我去更新产品数量(数量)中的 1 个时,它最终用我更新的相同产品替换了其他两个产品。我不确定如何解决此问题,我们将不胜感激。我不希望所有其他产品都替换为同一产品。

索引.php

<html>
<head>
<title></title>
</head>

<link rel="stylesheet" type="text/css" href="../assets/bootstrap-3.3.6-dist/css/bootstrap.css">
<body>
<?php
    ini_set('display_errors', 1);
    error_reporting(~0);

    $strKeyword = null;

    if(isset($_POST["txtKeyword"]))
    {
        $strKeyword = $_POST["txtKeyword"];
    }
    if(isset($_GET["txtKeyword"]))
    {
        $strKeyword = $_GET["txtKeyword"];
    }
?>
<div class="container">
<form name="frmSearch" method="post" action="<?php echo $_SERVER['SCRIPT_NAME'];?>">
  <table width="599" border="1">
    <tr>
      <th>Keyword
      <input name="txtKeyword"  type="text" id="txtKeyword" value="<?php echo $strKeyword;?>">
      <input type="submit" class="btn btn-primary btn-sm" value="Search"></th>
    </tr>
  </table>
</form>
<?php
   $serverName = "localhost";
   $userName = "root";
   $userPassword = "test";
   $dbName = "test";

   $conn = mysqli_connect($serverName,$userName,$userPassword,$dbName);

    $sql = "SELECT * FROM customer_order WHERE tr_id LIKE '%".$strKeyword."%' ";
    $query = mysqli_query($conn,$sql);

    $num_rows = mysqli_num_rows($query);

    $per_page = 10;   // Per Page
    $page  = 1;

    if(isset($_GET["Page"]))
    {
        $page = $_GET["Page"];
    }

    $prev_page = $page-1;
    $next_page = $page+1;

    $row_start = (($per_page*$page)-$per_page);
    if($num_rows<=$per_page)
    {
        $num_pages =1;
    }
    else if(($num_rows % $per_page)==0)
    {
        $num_pages =($num_rows/$per_page) ;
    }
    else
    {
        $num_pages =($num_rows/$per_page)+1;
        $num_pages = (int)$num_pages;
    }
    $row_end = $per_page * $page;
    if($row_end > $num_rows)
    {
        $row_end = $num_rows;
    }

    $sql .= " ORDER BY id ASC LIMIT $row_start ,$row_end ";
    $query = mysqli_query($conn,$sql);

?>
<table width="600" border="1">
  <tr>
    <th width="91"> <div align="center">Trid </div></th>
     <th width="91"> <div align="center">Product Name </div></th>
      <th width="91"> <div align="center">Quanity </div></th>
       <th width="91"> <div align="center">Price </div></th>
     <th width="91"> <div align="center">Edit </div></th>

  </tr>
<?php
$total = 0;
while($result=mysqli_fetch_array($query,MYSQLI_ASSOC))
{
?>
  <tr>
    <td><div align="center"><?php echo $result["tr_id"];?></div></td>
    <td><div align="center"><?php echo $result["p_name"];?></div></td>
    <td><div align="center"><?php echo $result["p_qty"];?></div></td>
     <td><div align="center"><?php echo $result["p_price"];?></div></td>
<td align="center"><a href="edit.php?id=<?php echo $result["id"];?>">Edit</a></td>


  </tr>
<?php
 $total = $total + $result['p_qty'] * $result['p_price'];
}
?>
</table>
<h3><?php echo "Total Price = $".$total;?></h3>
<br>
Total <?php echo $num_rows;?> Record : <?php echo $num_pages;?> Page :
<?php
if($prev_page)
{
    echo " <a href='$_SERVER[SCRIPT_NAME]?Page=$prev_page&txtKeyword=$strKeyword'><< Back</a> ";
}

for($i=1; $i<=$num_pages; $i++){
    if($i != $page)
    {
        echo "[ <a href='$_SERVER[SCRIPT_NAME]?Page=$i&txtKeyword=$strKeyword'>$i</a> ]";
    }
    else
    {
        echo "<b> $i </b>";
    }
}
if($page!=$num_pages)
{
    echo " <a href ='$_SERVER[SCRIPT_NAME]?Page=$next_page&txtKeyword=$strKeyword'>Next>></a> ";
}
$conn = null;
?>
</div>
</body>
</html>

编辑.php

<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="../assets/bootstrap-3.3.6-dist/css/bootstrap.css">
</head>
<body>
<?php
   ini_set('display_errors', 1);
   error_reporting(~0);

   $serverName = "localhost";
   $userName = "root";
   $userPassword = "test";
   $dbName = "test";

   $strid = null;

   if(isset($_GET["id"]))
   {
       $strid = $_GET["id"];
   }



   $conn = mysqli_connect($serverName,$userName,$userPassword,$dbName);

   $sql = "SELECT * FROM customer_order WHERE id = '".$strid."' ";

   $query = mysqli_query($conn,$sql);

   $result=mysqli_fetch_array($query,MYSQLI_ASSOC);

?>
<form action="save.php" name="frmAdd" method="post">
<table width="284" border="1">


     <tr>
    <th width="120">Product name</th>
    <td><input type="text" name="p_name" size="20" value="<?php echo $result["p_name"];?>"></td>
    </tr>
      <tr>
    <th width="120">Quantity</th>
    <td><input type="text" name="p_qty" size="20" value="<?php echo $result["p_qty"];?>"></td>
    </tr>

  </table>
  <br>
  <input type="submit" name="submit" value="submit">
</form>
<?php
mysqli_close($conn);
?>
</body>
</html>

保存.php

<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="../assets/bootstrap-3.3.6-dist/css/bootstrap.css">
</head>
<body>
<?php
    ini_set('display_errors', 1);
    error_reporting(~0);

    $serverName = "localhost";
    $userName = "root";
    $userPassword = "test";
    $dbName = "test";

    $conn = mysqli_connect($serverName,$userName,$userPassword,$dbName);

    $sql = "UPDATE customer_order SET 
            p_name = '".$_POST["p_name"]."' ,
            p_qty = '".$_POST["p_qty"]."' ";

    $query = mysqli_query($conn,$sql);

    if($query) {
     echo "Record update successfully";
     header("Location: http://localhost/supplytest2/admin");
    }

    mysqli_close($conn);
?>
</body>
</html>

最佳答案

在您的 UPDATE 命令中,您没有 where 子句。您正在更新表中的每条记录。

编辑.php

<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="../assets/bootstrap-3.3.6-dist/css/bootstrap.css">
</head>
<body>
<?php
   ini_set('display_errors', 1);
   error_reporting(~0);

   $serverName = "localhost";
   $userName = "root";
   $userPassword = "test";
   $dbName = "test";

   $id = null;

   if(isset($_GET["id"]))
   {
       $id = $_GET["id"];
   }



   $conn = mysqli_connect($serverName,$userName,$userPassword,$dbName);

   $sql = "SELECT * FROM customer_order WHERE id = '".$id."' ";

   $query = mysqli_query($conn,$sql);

   $result=mysqli_fetch_array($query,MYSQLI_ASSOC);

?>
<form action="save.php" name="frmAdd" method="post">
<table width="284" border="1">
  <tr>
    <th width="120">ID</th>
    <td><input type="text" name="id" size="20" value="<?php echo $result["id"];?>"></td>
    </tr>

     <tr>
    <th width="120">Product name</th>
    <td><input type="text" name="p_name" size="20" value="<?php echo $result["p_name"];?>"></td>
    </tr>
      <tr>
    <th width="120">Quantity</th>
    <td><input type="text" name="p_qty" size="20" value="<?php echo $result["p_qty"];?>"></td>
    </tr>

  </table>
  <br>
  <input type="submit" name="submit" value="submit">
</form>
<?php
mysqli_close($conn);
?>
</body>
</html>

保存.php

<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="../assets/bootstrap-3.3.6-dist/css/bootstrap.css">
</head>
<body>
<?php
    ini_set('display_errors', 1);
    error_reporting(~0);

    $serverName = "localhost";
    $userName = "root";
    $userPassword = "test";
    $dbName = "test";

    $conn = mysqli_connect($serverName,$userName,$userPassword,$dbName);


        $sql = "UPDATE customer_order SET 

p_name = '".$_POST["p_name"]."' ,

p_qty = '".$_POST["p_qty"]."'

WHERE id = '".$_POST["id"]."' ";


    $query = mysqli_query($conn,$sql);

    if($query) {
     echo "Record update successfully";
     header("Location: http://localhost/supplytest2/admin");
    }

    mysqli_close($conn);
?>
</body>
</html>

关于php - 我在更新表格中的产品数量时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46353228/

相关文章:

php - 如何安全地存储网站的配置文件?

php - Mysqli 更新查询不起作用

PHP、MYSQL选择行更新

php - Activerecord 时区格式不正确

javascript - 在 javascript 和 PHP 之间发送 JSON 字符串的正确方法是什么?

php - 如何将绝对系统路径转换为 ​​IShellFolder?

php - 使用 $_POST 值更新查询

javascript - ERR_EMPTY_RESPONSE(每个打开的地方)

php - 更新第一行mysql php

php - 在 CodeIgniter 中调用未定义的方法 CI_DB_mysqli_result::where()