Jump to content

[SOLVED] On duplicate key update.....problem


pcbguy

Recommended Posts

I thought I figured out how to do this but I am getting an error on the line that starts with ON DUPLICATE KEY.....Tells me unexpected T_String. What did I do wrong?

Thanks

 

<?php

//This is the directory where the images will be saved
$target="images/";
$target=$target.basename($_FILES['picture']['name']);

//This gets all the other information from the form
$position=$_POST['position'];
$year=$_POST['year'];
$make=$_POST['make'];
$model=$_POST['model'];
$price=$_POST['price'];
$description=$_POST['description'];
$picture=($_FILES['picture']['name']);

//Connects to Database
include "connect.php";

//Writes the information to the database
mysql_query("INSERT INTO for_sale (position,year,make,model,price,description,picture) VALUES ('$position','$year','$make','$model','$price','$description','$picture')");
	ON DUPLICATE KEY UPDATE year=VALUES($year), make=VALUES($make), model=VALUES($model), price=VALUES($price), description=VALUES($description), picture=VALUES($picture)''

//Writes photo to the server
if(move_uploaded_file($_FILES['picture']['tmp_name'],$target))
{

	//Tells you if its all okay
	echo "The file" .basename($_FILES['uploadedfile']['name']). "has been uploaded, and your information has been added to the directory";
}
else{

	//Gives and error if its not
	echo "Sorry, there was a problem uploading your file.";
}
?>	

Link to comment
Share on other sites

Just take a look at the code you posted above and the colorization should show your problem. You are ending the query on the first line and the rest of the query is down on the second code being interpreted as junk code.

 

Personally I always try to keep my queries from getting too wide. This should fix your problem:

 

<?php
$query = "INSERT INTO for_sale (position,year,make,model,price,description,picture)
                      VALUES ('$position','$year','$make','$model','$price','$description','$picture')
          ON DUPLICATE KEY UPDATE
                      year='$year', make='$make', model='$model',
                      price='$price', description='$description', picture='$picture'";
mysql_query($query);
?>

Link to comment
Share on other sites

After looking at that again it still doesn't make sense. What field is the KEY? It shouldn't be included in the UPDATE clause.

 

 

ADDED: For example if the client ID is the key for my client table, if I try to insert a record with a duplicate clientID then it should only update the other fields. So, the INSERT statement would need to include the key and the UPDATE should not include the key (unless it is used as part of the values being created)

Link to comment
Share on other sites

The key is the 'position' field.

 

I thought it would automatically look for a duplicate in a key or unique field and use that. 'Position' is the only unique field.

 

OK, I missed that. As long as "Position" is identified as the Key in the table the above should work.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.