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=-
Please help with UPDATING DATABASE from php form.
Moderator: General Moderators
Try the following
Code: Select all
<?php
$sql = "SELECT id FROM resources WHERE id = '".$id."'";
?>- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
- puckeye
- Forum Contributor
- Posts: 105
- Joined: Fri Dec 06, 2002 7:26 pm
- Location: Joliette, QC, CA
- Contact:
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:
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).
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>');