...

View Full Version : Has problem for update database



kamkam
01-07-2008, 05:15 AM
Hi;
I am trying to update my database, but the problem for me is the old value replace with the value of the " " (empty). my code as following;

$subject=$_POST['subject'];
$content=$_POST['content'];

echo $subject;
echo $content;

$result=mysql_query("UPDATE Thread SET Subject='$subject', Content='$content' where Id='$threadId'");

there are not problem for the "echo $subject" and "
echo $content;"

but if rewrite as following, it does update the values from database.

$result=mysql_query("UPDATE Thread SET Subject='subject', Content='content' where Id='$threadId'");

johnnyb
01-07-2008, 02:22 PM
Try:


$result=mysql_query("UPDATE Thread SET Subject='".$subject."', Content='".$content."' where Id='".$threadId."'");


Maybe the single quotes are messing up the values getting into the query. This way there's no room for interpretation.

Inigoesdr
01-07-2008, 11:14 PM
Escape your user input with mysql_real_escape_string(), and your original query should work.

$subject = mysql_real_escape_string($_POST['subject']);
$content = mysql_real_escape_string($_POST['content']);
$result=mysql_query("UPDATE `Thread` SET `Subject` = '$subject', `Content` = '$content' WHERE `Id` = '$threadId'");
If `Id` is an integer you don't need, and shouldn't use, quotes around the value.

kamkam
01-08-2008, 03:42 AM
Thanks for both;
it does work now.:thumbsup::thumbsup:

aedrin
01-08-2008, 03:38 PM
Maybe the single quotes are messing up the values getting into the query. This way there's no room for interpretation.

This is why one should always use the following syntax:



$sql = "SELECT * FROM threads where threadId = {$threadId}";

johnnyb
01-08-2008, 11:41 PM
This is why one should always use the following syntax:



$sql = "SELECT * FROM threads where threadId = {$threadId}";


I've never seen, (or at least if I have I haven't really paid attention to), the curly-bracket syntax. Is it in the manual, or explained somewhere else? (I'm going to check for myself right now - but if there are more hints to be had I'm listening).



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum