php mysql更新表使用变量不起作用

i have data on one form which is echoed form a table, i want to use this data to update information in another table. whenever i hit the update button it show an error, "could not update data: query was empty", here is the update code, please help me out.

  <?php
     if(isset($_POST['submit'])) {
        $dbhost = 'localhost';
        $dbuser = 'root';
        $dbpass = '';

        $conn = mysql_connect($dbhost, $dbuser, $dbpass);

        if(! $conn ) {
           die('Could not connect: ' . mysql_error());
        }

        $ItemId = $_POST['ItemId'];
        $Quantity = $_POST['Quantity'];

       //$sql = "UPDATE stationery ". "SET Quantity = $Quantity ". "WHERE ItemId = $ItemId" ;
        $sql = mysql_query("UPDATE stationery set Quantity = ". $Quantity ." WHERE ItemId = '".$ItemId."'");
       //$sql=mysql_query("UPDATE stationery SET Quantity = ".mysql_real_escape_string($Quantity)." WHERE IltemId = '".mysql_real_escape_string($ItemId)."'");
        mysql_select_db('dbtest');
        $retval = mysql_query( $sql, $conn );

        if(! $retval ) {
           die('Could not update data: ' . mysql_error());
        }
        echo "Updated data successfully
";

        mysql_close($conn);
     }
  ?>

and this is the table where data is echoed from a mysql table

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("dbtest", $con);

 $result = mysql_query ("SELECT * FROM stationery");
echo "<table border = '1' style='margin-left:18px;margin-right:18px;'bgcolor='#CFC'>
  <tr><th bgcolor='#34495E' colspan='9'>
   <h1><font color='white' align='center'>&nbsp&nbsp&nbspORDER OFFICE SUPPLIES</font></h1>
</th></tr>
<tr bgcolor='#CFC' font size='18'>
<th>Item Name</th>
<th>Item Id</th>
<th>Quantity</th>
</tr>";

 while ($row = mysql_fetch_array($result))
 {
    echo "<form action=\"\" method=\"post\" enctype=\"multipart/form-data\">";
    echo "<tr>";
    echo "<td><input type=\"text\" name=\"ItemName\"  size=\"30\" value=\" ". $row ['ItemName'] . "\" readonly></td>";
    echo "<td><input type=\"text\" name=\"ItemId\" value=\" ". $row ['ItemId'] . "\" readonly></td>";
   echo "<td><input type=\"text\" name=\"Quantity\" required></td>";
   echo "<td><input type=\"submit\" name=\"submit\" size=\"30\" style='background-color:#3366FF' value=\"Update  \"></td>";

 echo "</tr>";
   echo "</form>";
  }
  echo "</table>";


  mysql_close($con);

 ?>
doufu2496
doufu2496 错误是:无法更新数据:查询为空
4 年多之前 回复
duanhu2414
duanhu2414 不推荐使用mysql_*函数,而是使用mysqli_*或PDO
4 年多之前 回复
doujinge9648
doujinge9648 它显示的错误是什么?
4 年多之前 回复

2个回答



对PHP变量使用单引号,为SQL相关名称使用反向标记:</ p>

 <  code> $ sql = mysql_query(“UPDATEcopite setQuedity ='$ Quantity'WHERE'TitleId` ='$ ItemId'”); 
</ code> </ pre>
</ div>

展开原文

原文

Use single-quotes for PHP Variables and back ticks for SQL-related names:

$sql = mysql_query("UPDATE `stationery` set `Quantity` = '$Quantity' WHERE `ItemId` = '$ItemId'");

Change this line:

$sql = mysql_query("UPDATE stationery set Quantity = ". $Quantity ." WHERE ItemId = '".$ItemId."'");

To:

$sql = "UPDATE stationery set Quantity = '". $Quantity ."' WHERE ItemId = '".$ItemId."'";

Full Code:

<?php 
    $dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';

$conn = mysql_connect($dbhost, $dbuser, $dbpass);

if(! $conn ) {
   die('Could not connect: ' . mysql_error());
}

$ItemId = $_POST['ItemId'];
$Quantity = $_POST['Quantity'];

$sql = "UPDATE stationery set Quantity = '". $Quantity ."' WHERE ItemId = '".$ItemId."'";;

mysql_select_db('test');
$retval = mysql_query( $sql, $conn );

if(! $retval ) {
   die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully
";

mysql_close($conn);
?>
drbz99867
drbz99867 当我运行SQL查询描述文具时它给了我这条消息当前选择不包含唯一列。 网格编辑,复选框,编辑,复制和删除功能不可用。 表的列是ItemName,这是varchar的唯一键,ItemId varchar(主键和Quantity int
4 年多之前 回复
duanji9264
duanji9264 你能在这里张贴你的桌子结构吗?
4 年多之前 回复
duanlumei5941
duanlumei5941 还没有工作
4 年多之前 回复
dongyiyu3953
dongyiyu3953 数量是一个整数,而itemid是varchar
4 年多之前 回复
donglin6313
donglin6313 数量的数据类型是什么?
4 年多之前 回复
dongxiezhi9564
dongxiezhi9564 错误不再存在,但仍然不会更新数据
4 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐