在一个SQL查询中更新多行

时间:2016-07-01 06:46:14

标签: php mysql

我是php新手。这个问题困扰了我两天。 ergh。我想显示一个包含输入字段的表,以便用户可以插入数据并将其更新到数据库中。用户可以更改表中的所有数据。我想一次更新多行,但最终只更新1行(最后一行)。有人请帮助我。 Thnks。 This are the form. 这是以下代码。

    [<table>
       <thead>
         <tr>
         <td><b>Item Code</b></td>
         <td><b>Item Barcode</b></td>
         <td><b>Item</b></td>
         <td><b>QOH</b></td>
         <td><b>Quantity</br>Checked</b></td>
         <td><b>Quantity</br>Order</b></td>                            
         </tr>
      </thead>
<?php
$Barcode=$_SESSION\["Barcode"\];
$code=$_SESSION\["Code"\];
$itemcode=$_SESSION\["Itemcode"\];
$guid=$_SESSION\["guid"\];

$sql = "SELECT itemmastersupcode.*, itembarcode.*, stock_count_item.*, stock_count.*, po_ex_c.*, d.itemlink_total_qty  
FROM itemmastersupcode 
WHERE stock_count.SupCode = '$code' and stock_count_item.TRANS_GUID = '$guid'
GROUP BY itemmastersupcode.Itemcode";
$result=mysqli_query($conn2,$sql);
$rowcount = mysqli_num_rows($result);

while($row = mysqli_fetch_assoc($result))
{
  ?>

  <tbody>
     <tr>

     <td><?php echo $row\["Itemcode"\]; ?></td>

     <td><?php echo $row\["Barcode"\]; ?></td>

     <td><?php echo $row\["Description"\]; ?></td>

     <td><?php echo $row\["itemlink_total_qty"\]; ?></td>

     <td><?php echo $row\["qty"\]; ?></td>

     <form class="form-inline" role="form" method="POST" id="myForm">
     <td><input type="number" name="qty" value=""/></td>

     </tr>
     </tbody>

  <input type="hidden" name="itemcode" value="<?php echo $row\["itemcode"\]; ?>"/>
  <input type="hidden" name="supcode" value="<?php echo $_SESSION\["Code"\] ?>"/>
  <input type="hidden" name="guid" value="<?php echo $_SESSION\["guid"\] ?>"/>

<?php   
  }
    ?>
    </table>

    <button name="save" type="submit" ><b>SUBMIT</b></button>
    </form>

    <?php
      if (isset($_POST\["save"\]))
       {

       $itemcode=$_POST\['itemcode'\];
       $qty=$_POST\['qty'\];
       $supcode=$_POST\['supcode'\];
       $guid=$_POST\['guid'\];


$sql = "UPDATE stock_count, stock_count_item SET stock_count.posted = '1', stock_count_item.qty_order = '$qty'
WHERE stock_count.TRANS_GUID = '$guid' AND stock_count_item.Itemcode ='$itemcode' 
and stock_count_item.TRANS_GUID = '$guid' ";][1]

2 个答案:

答案 0 :(得分:0)

UPDATE查询将更新由WHERE子句过滤的所有记录。如果没有给出WHERE,则更新所有记录:

UPDATE table1 SET field1='value1'; 

会将所有记录的field1列设置为value1

UPDATE table1 SET field1='value1' WHERE field2='value2'; 

会将field1列设置为value1field2等于value2的所有记录。

因此,在您的情况下,通过WHERE子句找到的记录会更新。顺便说一下,这是所有基本的SQL内容,所以我建议你阅读SQL。

最后,还要在代码中使用预准备语句来防止SQL注入。

答案 1 :(得分:-1)

<table>
       <thead>
         <tr>
         <td><b>Item Code</b></td>
         <td><b>Item Barcode</b></td>
         <td><b>Item</b></td>
         <td><b>QOH</b></td>
         <td><b>Quantity</br>Checked</b></td>
         <td><b>Quantity</br>Order</b></td>                            
         </tr>
      </thead>
<?php
$Barcode=$_SESSION["Barcode"];
$code=$_SESSION["Code"];
$itemcode=$_SESSION["Itemcode"];
$guid=$_SESSION["guid"];

$sql = "SELECT itemmastersupcode.*, itembarcode.*, stock_count_item.*, stock_count.*, po_ex_c.*, d.itemlink_total_qty  
FROM itemmastersupcode 
WHERE stock_count.SupCode = '$code' and stock_count_item.TRANS_GUID = '$guid'
GROUP BY itemmastersupcode.Itemcode";
$result=mysqli_query($conn2,$sql);
$rowcount = mysqli_num_rows($result);

while($row = mysqli_fetch_assoc($result))
{
  ?>

  <tbody>
     <tr>

     <td><?php echo $row["Itemcode"]; ?></td>

     <td><?php echo $row["Barcode"]; ?></td>

     <td><?php echo $row["Description"]; ?></td>

     <td><?php echo $row["itemlink_total_qty"]; ?></td>

     <td><?php echo $row["qty"]; ?></td>

     <form class="form-inline" role="form" method="POST" id="myForm">
     <td><input type="number" name="itemcode[<?php echo $row["itemcode"]; ?>][qty]" value=""/></td> //update here

     </tr>
     </tbody>

<?php   
  }
    ?>
    </table>
  <input type="hidden" name="supcode" value="<?php echo $_SESSION["Code"] ?>"/> //update here
  <input type="hidden" name="guid" value="<?php echo $_SESSION["guid"] ?>"/>//update here
    <button name="save" type="submit" ><b>SUBMIT</b></button>
    </form>

Php内容

   <?php
      if (isset($_POST["save"]))
       {

       $itemcodes=$_POST['itemcode'];
       $qty=$_POST['qty'];
       $supcode=$_POST['supcode'];
       $guid=$_POST['guid']; 

$sql = "UPDATE stock_count, stock_count_item SET
 stock_count.posted = '1', 
 stock_count_item.qty_order = CASE  stock_count_item.Itemcode";
 foreach( $itemcodes as $itmcode=>$qty)
 {
        $sql.=" WHEN ".$itmcode." THEN ".$qty
 }
$sql.=" END  WHERE stock_count.TRANS_GUID =$guid AND stock_count_item.TRANS_GUID=$guid AND stock_count_item.Itemcode IN (".implode(",",array_keys(   $itemcodes)).") ";


?>

希望它会有所帮助