Jump to content

What's wrong with this query? I can't figure it out


music_lp90

Recommended Posts

I can't figure out what is wrong with this query. The prblem seems to be with the file section.Here's the query.

<?php//Set query string to update mysql$query = "UPDATE `price_info`   		 SET `style` = '$style',		`price` = '$price',		`title1` = '$title1',		`description1` = '$description1',		`title2` = '$title2',		`description2` = '$description2' ";//If there is no file, do not update the file field, if ($image != "No File"){	$query .= ", 'file' = '$image' WHERE `price_info`.`id` = '$id' LIMIT 1 ;";	}//but if there is, then update the fileelse { $query .= " WHERE `price_info`.`id` = '$id' LIMIT 1 ;"; }//Run the query mysql_query("$query");	?>

The $file variable is set to this:

<?php//Check for file and assign file name to $image variableif ($_FILES["file"]["name"] > " "){$image = mysql_prep($_FILES["file"]["name"]);}else $image = "No File";?>

When I echo the query string back after entering values into the form that the query handles it says:UPDATE `price_info` SET `style` = 'Cape', `price` = 'test price', `title1` = 'test title1', `description1` = 'test description1', `title2` = 'test title 2', `description2` = 'test description 2' , 'file' = 'ranch_lussier.gif' WHERE `price_info`.`id` = '1048729087' LIMIT 1 ;If anyone can help me I would greatly appreciate it. It works when no file is being uploaded, but when I'm using the file section the query doesn't update anything. The file does get uploaded to the server however.Thanks!

Link to comment
Share on other sites

Well, I have no idea what was wrong, but it's working now. I looked it over and over I don't know how many times and I wrote it over and over and now it works. So I must have been missing something, but I don't know what. I'm just happy its working now.Thanks for taking a look at it.

Link to comment
Share on other sites

... just so you know, all the weird ` you've got in there, don't need to be there. You just made yourself type alot more than you had to. And yeah, that did appear to be the issue

Link to comment
Share on other sites

Dot syntax is table.field. The backquotes are for specifying field names, I don't know if you use a backquote to also specify a table name. If your fields are reserved words (like To or From), then you need the backquotes. Otherwise you don't need them, but it doesn't hurt. In this case he could leave the table name off the query altogether, you don't need the table name in the where clause of an update statement, it doesn't make sense to do that, you wouldn't update one table based off another table unless there are some foreign keys and relationships set up.You should be able to run that query directly in phpMyAdmin though, I don't see anything wrong with the SQL syntax.

Link to comment
Share on other sites

Thats actually a good idea. I usually forget about phpMyAdmin because i use the Query Browser solution provided with my MySQL installation. But, if you echo out the SQL, you should be able to put it directly into phpMyAdmin and then further debug.

Link to comment
Share on other sites

Actually, I did use php MyAdmin to create the query. Before I entered it into the code, I ran the query in php MyAdmin by editing a row and then I copied and pasted the sql that it used to update the table. The problem started occuring when I was concatenating the sql to allow for the if else statement. Here is how I have the code now incase anyone wants to see it:

$query = "UPDATE `price_info` SET `price` = '$price',`title1` = '$title1',`description1` = '$description1',`title2` = '$title2',`description2` = '$description2' ";if ($image != "No File") {$query .= ", `file` = '$image' WHERE `price_info`.`id` = '$id' LIMIT 1 ;";}else { $query .= " WHERE `price_info`.`id` = '$id' LIMIT 1 ;"; }mysql_query("$query");	

Thanks everyone for your input.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...