循环更新无法正常工作

时间:2012-02-05 22:31:30

标签: php

我正在尝试通过$ _post数据循环更新,但更新无法正常工作

require_once("include/session.php");
require_once("include/dataconnect.php"); 
require_once("include/functions.php"); 
if(array_key_exists('item', $_POST)){
    $items = $_POST['item'];
    //Loop through $_POST items, updating the database for each item
    foreach ($items as $item) { 
        $Pquantity = intval($item[0]);
        $Pidno = intval($item[1]);
        $queryreg = mysql_query("
            UPDATE repplac
                 SET Pquantity = {$Pquantity}
                 WHERE
                       Pidno = {$Pidno}
                 AND
                       username = '{$_SESSION['username']}'
        "); 
    }

这是发送数据的表单

 echo "
<form action='updatepplac.php' method='Post' class='slistbar'>
<table border='1'>
  <tr>
    <th>SHOP NAME</th>
    <th>PRODUCT NAME</th>
    <th>PRODUCT SIZE</th>
    <th>PRODUCT COLOUR</th>
    <th>PRODUCT QUANTITY</th>
    <th>PRICE</th>
    <th></th>
  </tr>";

//获取DB结果并循环,输出带计数器的表行

$pplresult = mysql_query("SELECT * FROM repplac") or die(mysql_error());
for ($i = 0; $row = mysql_fetch_assoc($pplresult); $i++) 
{
    echo "
      <tr>
        <td>".htmlspecialchars($row['Sname'])."</td>
        <td>".htmlspecialchars($row['Pname'])."</td>
        <td>".htmlspecialchars($row['Psize'])."</td>
        <td>".htmlspecialchars($row['Pcolour'])."</td>
        <td>
          <input type='text' name='item[$i][Pquantity]' id='Pquantity' 
    value='".htmlspecialchars($row['Pquantity'])."' />
          <input type='hidden' name='item[$i][Pidno]' id='Pidno' value='".htmlspecialchars($row['Pidno'])."' />
        </td>
        <td>".htmlspecialchars($row['Price'])."</td>
        <td><a href='deleteproduct.php?del=".htmlspecialchars($row['Pidno'])."'>delete</a></td>
      </tr>";
  }

1 个答案:

答案 0 :(得分:1)

首先在变量中存储$ _SESSION ['username'],然后在查询中使用它。所以你的查询应该是类似的 $username = $_SESSION['username'];

$queryreg = mysql_query("UPDATE repplac SET Pquantity = '$Pquantity' WHERE Pidno = '$Pidno' AND username = '$username'");