Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在一个sql查询中更新多行_Php_Mysql - Fatal编程技术网

Php 在一个sql查询中更新多行

Php 在一个sql查询中更新多行,php,mysql,Php,Mysql,我是php新手。这个问题困扰了我两天。嗯。我想显示一个包含输入字段的表,以便用户可以插入数据并将其更新到数据库中。用户可以更改表中的所有数据。我想一次更新多行,但最终只更新了一行(最后一行)。任何人都可以帮我。Thnks。 这是下面的代码 [<table> <thead> <tr> <td><b>Item Code</b></td> &l

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

    [<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]
[
项目代码
商品条码
项目
库赫
数量
已检查 数量
订单
UPDATE
查询将更新由
WHERE
子句过滤的所有记录。如果未给出
WHERE
,则更新所有记录:

UPDATE table1 SET field1='value1'; 
将所有记录的
field1
列设置为
value1

UPDATE table1 SET field1='value1' WHERE field2='value2'; 
field1
列设置为
value1
field2
等于
value2
的所有记录

因此,在您的例子中,通过
WHERE
子句找到的记录将被更新。顺便说一下,这些都是基本的SQL内容,所以我建议您阅读SQL

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


<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>
项目代码 商品条码 项目 库赫 数量
已检查 数量
订单
为此,您必须将更新查询放入foreach循环中。对于更新多行。您的格式有什么问题?方括号前的所有斜杠都不属于那里。您可以指定要由
WHERE
更新的列。因此,如果
WHERE
包含多个列,它将更新多个列。现在,后面的语句r
其中
肯定满足1行而不是更多的条件。尝试更改它们。这里有很多错误:-(.我建议您将问题分解为可管理的部分-从SELECT开始。为什么要进行向下投票我不知道,我正在使用案例更新记录,我希望100%有效谢谢您的回复@Naisa purushotham。我将检查代码
   <?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)).") ";


?>