Page 1 of 1

Please help with UPDATING DATABASE from php form.

Posted: Tue Jan 28, 2003 1:55 am
by urb
I am trying to update information i have stored in a database table that i have already created. I keep getting a sql error of:

You have an error in your SQL syntax near '' at line 1
SELECT id FROM resources WHERE id =


My code for the php script to do the actual update is the following:

<?php

$sql = "SELECT id FROM resources WHERE id = $id";
@mysql_select_db(webtemp_123webtemplates) or die(mysql_error());
@$result = mysql_query($sql) or die(mysql_error().'<p>'.$sql.'</p>');

if (mysql_num_rows($result) < 1) {
echo 'Required information has been deleted.';
} else {
$sql = "UPDATE resources";
$sql .= "SET link_name = '".addslashes(trim($link_name))."'";
$sql .= "SET link_url = '".addslashes(trim($link_url))."'";
$sql .= "SET link_rating = '".addslashes(trim($link_rating))."'";
$sql .= "SET link_desc = '".addslashes(trim($link_desc))."'";
$sql .= "WHERE id = $id";
@mysql_query($sql) or die(mysql_error().'<p>'.$sql.'</p>');
}

?>


Any help is always greatly appreciated from the members of this forum,

Matt Urban
a.k.a -=urb=-

Posted: Tue Jan 28, 2003 2:23 am
by oldtimer
Try the following

Code: Select all

<?php
$sql = "SELECT id FROM resources WHERE id = '".$id."'"; 


?>

Posted: Tue Jan 28, 2003 2:37 am
by twigletmac
Where does $id come from and is it numeric or does it contain other characters?

Mac

Posted: Tue Jan 28, 2003 9:09 am
by puckeye
I you anwser to the posters above you should be able to find the problem with your first query however I think I see a problem with your second query:

Code: Select all

$sql = "UPDATE resources"; 
$sql .= "SET link_name = '".addslashes(trim($link_name))."'"; 
$sql .= "SET link_url = '".addslashes(trim($link_url))."'"; 
$sql .= "SET link_rating = '".addslashes(trim($link_rating))."'"; 
$sql .= "SET link_desc = '".addslashes(trim($link_desc))."'"; 
$sql .= "WHERE id = $id"; 
@mysql_query($sql) or die(mysql_error().'<p>'.$sql.'</p>');
The problem with this query is that you do not leave a space at the end or beginning of each of the $sql .= " line... This would give a $sql looking similar to this: UPDATE resourcesSET link_name = 'NAME'SET link_url = 'URL' etc... first of you will probably need to place a space in front of each SET. Second I believe that you don't need the kwyword SET with each field simply place a comma (,) between each field and you're set... (no punn intended).