0

I have this php file which will update a table in mysql database.

it works to a certain. it will update the name of the product (product_name) and it will update the (date_added) too. but it is not updating the id and as a result the image that was uploaded with that id is not showing!!

here is the code:

<?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();
}
?>

and here is the form:

<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>

anyone knows why this is happening?

Thanks

David Smith
  • 105
  • 2
  • 10
  • Why are you using an `UPDATE` to `INSERT` a record into the database? That update query would set ALL product_name and date_added fields to `$product_name` and `now()` respectively. – WWW Mar 27 '13 at 13:30
  • I don't see an actual INSERT anywhere. – isotrope Mar 27 '13 at 13:31
  • 5
    **Don't use `mysql_*` functions in new code** ([why?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php)), they are [deprecated](https://wiki.php.net/rfc/mysql_deprecation). Use [PDO or MySQLi](http://php.net/manual/en/mysqlinfo.api.choosing.php) instead – kero Mar 27 '13 at 13:32
  • You are calling `mysql_insert_id()` yet haven't actually `INSERT`ed anything? – Peter Featherstone Mar 27 '13 at 13:32
  • @kingkero, i know mate, i am just working on someone else's script. I have already told them to let me change the script to mysqli but they don't want to. so its their choice. – David Smith Mar 27 '13 at 13:47

1 Answers1

2

mysql_insert_id() returns the last INSERT record, not UPDATE.

Damonsson
  • 1,532
  • 14
  • 25