Simple UPDATE: error performing query

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nado
Forum Newbie
Posts: 20
Joined: Tue Aug 29, 2006 3:47 am

Simple UPDATE: error performing query

Post by nado »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


hi

I have a simple form to update a row. It was working fine until I started adding more fields (in both the database and form)... but I'm getting this error:

[quote]Error performing query: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE (quote.id = 4)' at line 1[/quote]

Here's my code... it's split into functions, connections should be working as I haven't changed them, and also the data loads correctly (only getting the error when going to edit).

Code: Select all

function edit_quote() {    
/* Calls our connection function */

$conn = my_conn(); 

/* Defines query */

$sql = "SELECT * FROM quote WHERE (quote.id = " . $_REQUEST['id'] . ")"; 

/* Passes query to database */

$result = mysql_query($sql, $conn);
if (!$result) {
  echo("<p>Error performing query: " . mysql_error() . "</p>");
  exit();
} 

/* creates our row array with an if statement to report errors */

if ($row = @mysql_fetch_array($result, MYSQL_ASSOC)) { 

/* prints out our HTML form '\"' */

print "<form name=\"Update\" method=\"post\" action=\"$_SERVER[PHP_SELF]\">";

/* prints out our HTML table and fields 'escaping' any double quotes '\"' */ 
 
print "<br><table> 

<tr>
<td width=\"125\"></td>
<td><h4>$row[name] - $row[email]</h4></td>
</tr>
<tr>
<td><strong>ID</strong></td>
<td><input type=\"text\" name=\"id\" value=\"$row[id]\" readonly=\"true\"></td>
</td>
</tr> 
<tr>
<td><strong>Name</strong></td> 
<td><input type=\"text\" name=\"name\" value=\"$row[name]\"></td> 
</tr> 
<tr> 
<td><strong>Email</strong></td> 
<td><input type=\"text\" name=\"email\" value=\"$row[email]\"></td> 
</tr> 
<tr> 
<td><strong>Date</strong></td> 
<td><input type=\"text\" name=\"date\" value=\"$row[date]\"></td> 
</tr>
<tr> 
<td><strong>Discount</strong></td> 
<td><input type=\"text\" name=\"discount\" value=\"$row[discount]\"></td> 
</tr>
</table>
<br>
<table>
<tr>
<td width=\"125\"></td>
<td><strong>Product #1</strong></td>
<td><strong>Product #2</strong></td>
<td><strong>Product #3</strong></td>
<td><strong>Product #4</strong></td>
<td><strong>Product #5</strong></td>
</tr>
<tr> 
<td><strong>Quantity</strong></td> 
<td><input type=\"text\" name=\"pquant1\" value=\"$row[pquant1]\"></td> 
<td><input type=\"text\" name=\"pquant2\" value=\"$row[pquant2]\"></td> 
<td><input type=\"text\" name=\"pquant3\" value=\"$row[pquant3]\"></td> 
<td><input type=\"text\" name=\"pquant4\" value=\"$row[pquant4]\"></td> 
<td><input type=\"text\" name=\"pquant5\" value=\"$row[pquant5]\"></td> 
</tr> 
<tr> 
<td><strong>Description</strong></td> 
<td><textarea cols=\"17\" rows=\"10\" id=\"pdes1\" name=\"pdes1\">$row[pdes1]</textarea></td> 
<td><textarea cols=\"17\" rows=\"10\" id=\"pdes2\" name=\"pdes2\">$row[pdes2]</textarea></td> 
<td><textarea cols=\"17\" rows=\"10\" id=\"pdes3\" name=\"pdes3\">$row[pdes3]</textarea></td> 
<td><textarea cols=\"17\" rows=\"10\" id=\"pdes4\" name=\"pdes4\">$row[pdes4]</textarea></td> 
<td><textarea cols=\"17\" rows=\"10\" id=\"pdes5\" name=\"pdes5\">$row[pdes5]</textarea></td> 
</tr> 
<tr> 
<td><strong>Image</strong></td> 
<td><input type=\"text\" name=\"pimage1\" value=\"$row[pimage1]\"></td> 
<td><input type=\"text\" name=\"pimage2\" value=\"$row[pimage2]\"></td> 
<td><input type=\"text\" name=\"pimage3\" value=\"$row[pimage3]\"></td> 
<td><input type=\"text\" name=\"pimage4\" value=\"$row[pimage4]\"></td> 
<td><input type=\"text\" name=\"pimage5\" value=\"$row[pimage5]\"></td> 
</tr> 
<tr> 
<td><strong>Link</strong></td> 
<td><input type=\"text\" name=\"plink1\" value=\"$row[plink1]\"></td> 
<td><input type=\"text\" name=\"plink2\" value=\"$row[plink2]\"></td> 
<td><input type=\"text\" name=\"plink3\" value=\"$row[plink3]\"></td> 
<td><input type=\"text\" name=\"plink4\" value=\"$row[plink4]\"></td> 
<td><input type=\"text\" name=\"plink5\" value=\"$row[plink5]\"></td> 
</tr> 
<tr> 
<td><strong>Price</strong></td> 
<td><input type=\"text\" name=\"pprice1\" value=\"$row[pprice1]\"></td> 
<td><input type=\"text\" name=\"pprice2\" value=\"$row[pprice2]\"></td> 
<td><input type=\"text\" name=\"pprice3\" value=\"$row[pprice3]\"></td> 
<td><input type=\"text\" name=\"pprice4\" value=\"$row[pprice4]\"></td> 
<td><input type=\"text\" name=\"pprice5\" value=\"$row[pprice5]\"></td> 
</tr> 
<tr> 
<td><strong>Postage</strong></td> 
<td><input type=\"text\" name=\"ppost1\" value=\"$row[ppost1]\"></td> 
<td><input type=\"text\" name=\"ppost2\" value=\"$row[ppost2]\"></td> 
<td><input type=\"text\" name=\"ppost3\" value=\"$row[ppost3]\"></td> 
<td><input type=\"text\" name=\"ppost4\" value=\"$row[ppost4]\"></td> 
<td><input type=\"text\" name=\"ppost5\" value=\"$row[ppost5]\"></td> 
</tr> 
<tr><td>&nbsp</td></tr>
<tr> 
<td><strong>Comments</strong></td> 
<td colspan=2><textarea cols=\"37\" rows=\"10\" name=\"comments\" id=\"comments\">$row[comments]</textarea></td> 
</tr>
<tr><td>&nbsp</td></tr>
<tr>
<td></td>
<td valign=\"top\"><input type=\"submit\" name=\"Submit\" value=\"Update\">
</tr> 
</table> 
</form>"; 

} else { 
    echo("There has been an error" . mysql_error()); 
} 

/* closes connection */ 

mysql_close ($conn); 

}  

function update_quote() {

/* Calls our connection function */

$conn = my_conn();

/* Defines query */

$sql_update = "UPDATE quote SET ";
$sql_update .= "quote.name = '" . $_REQUEST['name'] . "', ";
$sql_update .= "quote.email = '" . $_REQUEST['email'] . "', ";
$sql_update .= "quote.date = '" . $_REQUEST['date'] . "', ";
$sql_update .= "quote.plink1 = '" . $_REQUEST['plink1'] . "', ";
$sql_update .= "quote.plink2 = '" . $_REQUEST['plink2'] . "', ";
$sql_update .= "quote.plink3 = '" . $_REQUEST['plink3'] . "', ";
$sql_update .= "quote.plink4 = '" . $_REQUEST['plink4'] . "', ";
$sql_update .= "quote.plink5 = '" . $_REQUEST['plink5'] . "', ";
$sql_update .= "quote.pimage1 = '" . $_REQUEST['pimage1'] . "', ";
$sql_update .= "quote.pimage2 = '" . $_REQUEST['pimage2'] . "', ";
$sql_update .= "quote.pimage3 = '" . $_REQUEST['pimage3'] . "', ";
$sql_update .= "quote.pimage4 = '" . $_REQUEST['pimage4'] . "', ";
$sql_update .= "quote.pimage5 = '" . $_REQUEST['pimage5'] . "', ";
$sql_update .= "quote.pdes1 = '" . $_REQUEST['pdes1'] . "', ";
$sql_update .= "quote.pdes2 = '" . $_REQUEST['pdes2'] . "', ";
$sql_update .= "quote.pdes3 = '" . $_REQUEST['pdes3'] . "', ";
$sql_update .= "quote.pdes4 = '" . $_REQUEST['pdes4'] . "', ";
$sql_update .= "quote.pdes5 = '" . $_REQUEST['pdes5'] . "', ";
$sql_update .= "quote.pprice1 = '" . $_REQUEST['pprice1'] . "', ";
$sql_update .= "quote.pprice2 = '" . $_REQUEST['pprice2'] . "', ";
$sql_update .= "quote.pprice3 = '" . $_REQUEST['pprice3'] . "', ";
$sql_update .= "quote.pprice4 = '" . $_REQUEST['pprice4'] . "', ";
$sql_update .= "quote.pprice5 = '" . $_REQUEST['pprice5'] . "', ";
$sql_update .= "quote.ppost1 = '" . $_REQUEST['ppost1'] . "', ";
$sql_update .= "quote.ppost2 = '" . $_REQUEST['ppost2'] . "', ";
$sql_update .= "quote.ppost3 = '" . $_REQUEST['ppost3'] . "', ";
$sql_update .= "quote.ppost4 = '" . $_REQUEST['ppost4'] . "', ";
$sql_update .= "quote.ppost5 = '" . $_REQUEST['ppost5'] . "', ";
$sql_update .= "quote.pquant1 = '" . $_REQUEST['pquant1'] . "', ";
$sql_update .= "quote.pquant2 = '" . $_REQUEST['pquant2'] . "', ";
$sql_update .= "quote.pquant3 = '" . $_REQUEST['pquant3'] . "', ";
$sql_update .= "quote.pquant4 = '" . $_REQUEST['pquant4'] . "', ";
$sql_update .= "quote.pquant5 = '" . $_REQUEST['pquant5'] . "', ";
$sql_update .= "quote.discount = '" . $_REQUEST['discount'] . "', ";
$sql_update .= "quote.comments = '" . $_REQUEST['comments'] . "', ";
$sql_update .= "WHERE (quote.id = " . $_REQUEST['id'] . ")";

/* Passes query to database */

$result = mysql_query($sql_update, $conn);
if (!$result) {
  echo("<p>Error performing query: " . mysql_error() . "</p>");
  exit();
}

/* Prints succes message */

print "<p>Successfully Updated</p><hr>";

/* Closes Connection to the MySQL server */

mysql_close ($conn);
html_form(); 

}
Any help would be great

thanks


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

printing the query string out you should notice an extra comma.
nado
Forum Newbie
Posts: 20
Joined: Tue Aug 29, 2006 3:47 am

Post by nado »

feyd wrote:printing the query string out you should notice an extra comma.
omg lol... thanks feyd

*starts making more coffee*
Post Reply