pcbguy Posted December 5, 2007 Share Posted December 5, 2007 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."; } ?> Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 5, 2007 Share Posted December 5, 2007 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); ?> Quote Link to comment Share on other sites More sharing options...
pcbguy Posted December 5, 2007 Author Share Posted December 5, 2007 Wow. Now I feel stupid. Oops. Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 5, 2007 Share Posted December 5, 2007 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) Quote Link to comment Share on other sites More sharing options...
pcbguy Posted December 5, 2007 Author Share Posted December 5, 2007 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. Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 5, 2007 Share Posted December 5, 2007 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. Quote Link to comment Share on other sites More sharing options...
pcbguy Posted December 5, 2007 Author Share Posted December 5, 2007 Cool. Thanks. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.