Please help with UPDATING DATABASE from php form.

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
urb
Forum Newbie
Posts: 21
Joined: Fri Nov 08, 2002 1:08 am
Location: Los Angeles, California

Please help with UPDATING DATABASE from php form.

Post 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=-
oldtimer
Forum Contributor
Posts: 204
Joined: Sun Nov 03, 2002 8:21 pm
Location: Washington State

Post by oldtimer »

Try the following

Code: Select all

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


?>
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Where does $id come from and is it numeric or does it contain other characters?

Mac
User avatar
puckeye
Forum Contributor
Posts: 105
Joined: Fri Dec 06, 2002 7:26 pm
Location: Joliette, QC, CA
Contact:

Post 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).
Post Reply