更新mysql数据库以及与id相关的id和图像?

时间:2013-03-27 13:28:42

标签: php mysql

我有这个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>

有谁知道为什么会这样?

由于

1 个答案:

答案 0 :(得分:2)

mysql_insert_id()会返回上一个INSERT记录,而不是UPDATE