add unique id to php mysql upload

261

Is there a quick method to add a unique id to a php mysql upload- I have scrolled through these forums but was hoping there is a much simpler method to achieve my aim.

Essentially, I have an upload that works perfectly - and I am hoping to add a product code to each item that will be generated using the auto-incremented unique id field in mysql.

So far I have the following php:

<?php include 'dbc.php'; page_protect();

if(!checkAdmin()) {header("Location: login.php");
exit();
}

$host  = $_SERVER['HTTP_HOST'];
$host_upper = strtoupper($host);
$login_path = @ereg_replace('admin','',dirname($_SERVER['PHP_SELF']));
$path   = rtrim($login_path, '/\\');

foreach($_GET as $key => $value) {
    $get[$key] = filter($value);
}

foreach($_POST as $key => $value) {
    $post[$key] = filter($value);
}   
?>

<?php 
if($_FILES['photo']) //check if we uploading a file
{
    $target = "images/furnishings/"; 
    $target = $target . basename( $_FILES['photo']['name']); 

    $title = mysql_real_escape_string($_POST['title']); 
    $desc = mysql_real_escape_string($_POST['desc']); 
    $price = mysql_real_escape_string($_POST['price']);  
    $pandp = mysql_real_escape_string($_POST['pandp']);  
    $pic = "images/furnishings/" .(mysql_real_escape_string($_FILES['photo']['name']));
    $productcode = "FUR000" .(mysql_real_escape_string($_POST['id']));
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
{
    mysql_query("INSERT INTO `furnishings` (`title`, `desc`, `price`, `pandp`, `photo`,`productcode`) VALUES ('$title', '$desc', '$price', '$pandp', '$pic', '$productcode')") ;     

    echo "The product has been added to the furnishings category"; 
} 
else 
{ 
    echo "Please fill out the specifications and select the respective file to upload for the main image"; 

}
} 
?> 

And the following HTML:

<form enctype="multipart/form-data" action="addfurn.php" method="POST">
 <table width="100%" border="2" cellpadding="5">
  <tr>
    <td>Title: </td>
    <td><input type="text" name="title" /></td>
  </tr>
   <tr>
    <td>Description: </td>
    <td><input type="text" name = "desc" /></td>
   </tr>
     <tr>
    <td>Price: </td>
    <td><input type="text" name = "price" /></td>
   </tr>
    <tr>
    <td>P&amp;P: </td>
    <td><input type="text" name = "pandp" /></td>
   </tr>
   <tr>
    <td>Main Image: </td>
    <td><input type="file" name="photo" /></td>
   </tr>
   <tr>
    <td colspan="2"><input type="submit" value="Add" /></td>
   </tr>
 </table>
</form>

Now given everything works - the only "problem line" in the code is:

$productcode = "FUR000" .(mysql_real_escape_string($_POST['id']));

assuming that as the id hasnt yet been generated it cannot add it to the insert query - therefore the table in mysql simply returns FUR000 for each new item added.

Is there a way to amend this line to auto-increment in mysql in a similar fashion to the addition of new lines - or do I have to include a unique code for each item in my HTML table?

Any help much appreciated!

Thanks JD

156

Answer

Solution:

you need 2 queries for this.

first, insert your data without productcode.
next, get id using mysql_insert_id()
finally, create your productcode and update your table using this newly generated id

however, I see no point in such a field. Why not to create it on the fly?

People are also looking for solutions to the problem: io - Writing to Text files via php

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.