0

我有这个 php 文件,它将更新 mysql 数据库中的表。

它在一定程度上起作用。它会更新产品的名称(product_name),也会更新(date_added)。但它没有更新 id,因此没有显示使用该 id 上传的图像!

这是代码:

<?php 
// Parse the form data and add inventory item to the system
if (isset($_POST['product_name'])) {

    $product_name = mysql_real_escape_string($_POST['product_name']);
    // See if that product name is an identical match to another product in the system
    $sql = mysql_query("SELECT id FROM tomProduct WHERE product_name='$product_name' LIMIT 1");
    $productMatch = mysql_num_rows($sql); // count the output amount
    if ($productMatch > 0) {
        echo 'Sorry you tried to place a duplicate "Product Name" into the system, <a href="index.php">click here</a>';
        exit();
    }
    // Add this product into the database now
    $sql = mysql_query("UPDATE tomProduct SET product_name='$product_name', date_added=now()") or die (mysql_error());
     $pid = mysql_insert_id();
    // Place image in the folder 
    $newname = "$pid.jpg";
    move_uploaded_file( $_FILES['fileField']['tmp_name'], "../tom/$newname");
    header("location: verify_members.php"); 
    exit();
}
?>

这是表格:

<form action="verify_members.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" />
    </label></td>
  </tr>
  <tr>
    <td align="right">Product Image</td>
    <td><label>
      <input type="file" name="fileField" id="fileField" />
    </label></td>
  </tr> 
        <tr>      
  <tr>
    <td>&nbsp;</td>
    <td><label>
      <input type="submit" name="button" id="button" value="Add This Item Now" />
    </label></td>
  </tr>
</table>
</form>

有谁知道为什么会这样?

谢谢

4

1 回答 1

2

mysql_insert_id()返回最后一条INSERT记录,而不是UPDATE.

于 2013-03-27T13:32:32.777 回答