简体   繁体   English

PHP,MySQL表更新问题

[英]PHP, MySQL Table UPDATE issue

Am trying to UPDATE a innodb table "products" by using a form of which last column is subid(fk) reference to table "subcategory" subid(PK), but i only want to update the "products" table without making any changes to subid(fk) column in "products" table, here is my full code 我试图通过使用最后一个列是对表“ subcategory” subid(PK)的subid(fk)引用的形式来更新innodb表“ products”,但是我只想更新“ products”表而无需进行任何更改“产品”表中的subid(fk)列,这是我的完整代码

<?php   

if (isset($_POST['PRODUCT_NAME'])) {

$pid = mysql_real_escape_string($_POST['thisPID']);
$catalog_no = mysql_real_escape_string($_POST['CATALOG_NO']);
$product_name = mysql_real_escape_string($_POST['PRODUCT_NAME']);
$price = mysql_real_escape_string($_POST['PRICE']);
$composition = mysql_real_escape_string($_POST['COMPOSITION']);
$size = mysql_real_escape_string($_POST['SIZE']);
// See if that product name is an identical match to another product in the system
$sql = mysql_query("UPDATE products SET CATALOG_NO='$catalog_no', PRODUCT_NAME='$product_name', PRICE='$price', COMPOSITION='$composition', SIZE='$size' WHERE PID='$pid'") or die(mysql_error());
header("location: inventory_list.php"); 
exit();  
}
?> 

<?php 
if (isset($_GET['PID'])) {
$targetID = $_GET['PID'];
$sql = mysql_query("SELECT products.PID, products.CATALOG_NO, products.PRODUCT_NAME, products.PRICE, products.COMPOSITION, products.SIZE FROM products WHERE PID='$targetID' LIMIT 1") or die(mysql_error());
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
    while($row = mysql_fetch_array($sql)){ 

         $catalog_no = $row["CATALOG_NO"];
         $product_name = $row["PRODUCT_NAME"];
         $price = $row["PRICE"];
         $composition = $row["COMPOSITION"];
         $size = $row["SIZE"];
    }
} else {
    echo "You dont have that product";
    exit();
}
}
?>  

The form uses the following code 该表格使用以下代码

<form action="inventory_edit.php" enctype="multipart/form-data" name="myForm" id="myform" method="post">
<table width="90%" border="0" cellspacing="0" cellpadding="6">
  <tr>
    <td width="20%" align="right">Product Name</td>
    <td width="80%"><label>
      <input name="product_name" type="text" id="product_name" size="64" value="<?php echo $product_name; ?>" />
    </label></td>
  </tr>
  <tr>
    <td align="right">Product Price</td>
    <td><label>
      $
      <input name="price" type="text" id="price" size="12" value="<?php echo $price; ?>" />
    </label></td>
  </tr>
  <tr>
    <td align="right">Composition</td>
    <td><label>
      <textarea name="composition" id="composition" cols="64" rows="5"><?php echo $composition; ?></textarea>
    </label></td>
  </tr>
  <tr>
    <td align="right">Size</td>
    <td><label>
      <input type="text" name="size" id="size" value="<?php echo $size; ?>" />
    </label></td>
  </tr>      
  <tr>
    <td>&nbsp;</td>
    <td><label>
      <input name="thisID" type="hidden" value="<?php echo $targetID; ?>" />
      <input type="submit" name="button" id="button" value="Make Changes" />
    </label></td>
  </tr>
</table>
</form>

The form does nothing (it just refreshes the page), it does not UPDATE the table. 表单不执行任何操作(仅刷新页面),不更新表。 How to solve this problem? 如何解决这个问题呢?

Here is my table structure: table name - "products" set to ON UPDATE and ON DELETE CASCADE 这是我的表结构:表名-“产品”设置为ON UPDATE和ON DELETE CASCADE

PID(PK) CATALOG_NO  PRODUCT_NAME PRICE COMPOSITION SIZE SUBCAT_ID(FK)
  1         bbp2         NO2      $45    1% NO     10ml      7
$pid = mysql_real_escape_string($_POST['thisPID']);

在这里,您错过了<input name="thisID" type="hidden">它应该是thisPID

It's been a while since I did a mysql_query, but is this line correct? 自从我做了mysql_query以来已经有一段时间了,但这行正确吗?

$sql = mysql_query("UPDATE products SET CATALOG_NO='$catalog_no', PRODUCT_NAME='$product_name', PRICE='$price', COMPOSITION='$composition', SIZE='$size' WHERE PID='$pid'") or die(mysql_error());

THis looks like it is writing the SQL as this 看起来它正在像这样编写SQL

"UPDATE products SET CATALOG_NO='$catalog_no'" 

instead of the value of $catalog_no. 而不是$ catalog_no的值。

Should the line not be constructed like this: 如果该行不是这样构造的:

"UPDATE products SET CATALOG_NO='" . $catalog_no . "', PRODUCT_NAME='" . $product_name . '"... 

etc..? 等等..?

Debug your code like this submit your form & check each value 像这样调试代码,提交表单并检查每个值

// See if that product name is an identical match to another product in the system echo $sql = "UPDATE products SET CATALOG_NO='$catalog_no', PRODUCT_NAME='$product_name', PRICE='$price', COMPOSITION='$composition', SIZE='$size' WHERE PID='$pid'"; // //查看该产品名称是否与系统中的另一个产品相同。echo $ sql =“ UPDATE products SET CATALOG_NO ='$ catalog_no',PRODUCT_NAME ='$ product_name',PRICE ='$ price',COMPOSITION ='$ composition',SIZE ='$ size'WHERE PID ='$ pid'“;

exit; 出口;

Since the query doesn't seem to be doing anything, but isn't triggering an error condition (or you'd see it via the die() call, you should check to see exactly what the generated query string looks like: 由于查询似乎没有执行任何操作,但是没有触发错误条件(或者您会通过die()调用看到它,因此您应该检查一下生成的查询字符串的模样:

$sql = "UPDATE products SET CATALOG_NO='$catalog_no', PRODUCT_NAME='$product_name', PRICE='$price', COMPOSITION='$composition', SIZE='$size' WHERE PID='$pid'";
$result = mysql_query($sql) or die(mysql_error());

echo $sql;

Just because the query call didn't do the die() doesn't mean the query is valid. 仅仅因为查询调用没有执行die()并不意味着查询有效。 Examine the generated query, try to run it manually, see what happens then. 检查生成的查询,尝试手动运行它,然后查看会发生什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM