PDA

View Full Version : Db wont update?!


Tidus
08-04-2002, 06:52 AM
Can Anybody tell my whats wrong with this? It just wont update the database?


<?php


## CONNECT TO DB HERE

mysql_select_db("news",$db);

$result = mysql_query("SELECT * FROM newsdb",$db);

if (id) {

$sql = "UPDATE `newsdb` SET id='$id',displaytitle='$displaytitle',news='$news',displaydate='$displaydate',time='$time',by='$by', realtitle='$realtitle' WHERE id='$id'";

// run SQL against the DB

$result = mysql_query($sql);

printf("<font face='Verdana' size=1 color=#667AA6>Thankyou - The Selected News Item has been Edited.! <br> To Return to the Admin Center - Click <a href=index.php3>Here</a>\n");

}


?>


Thanks

firepages
08-04-2002, 08:01 AM
if (id) {
should be
if ($id) {

but as thats probably just a typo... so get MySQL to tell you whats wrong...




<?php


## CONNECT TO DB HERE

mysql_select_db("news",$db)or die(mysql_error());

$result = mysql_query("SELECT * FROM newsdb",$db)or die(mysql_error());

if ($id) {

$sql = "UPDATE `newsdb` SET id='$id',displaytitle='$displaytitle',news='$news' ,displaydate='$displaydate',time='$time',by='$by',realtitle='$realtitle' WHERE id='$id'";

// run SQL against the DB

$result = mysql_query($sql)or die(mysql_error());

printf("<font face='Verdana' size=1 color=#667AA6>Thankyou - The Selected News Item has been Edited.! <br> To Return to the Admin Center - Click <a href=index.php3>Here</a>\n");

}


?>


hopefully the error messages will answer your question

Tidus
08-04-2002, 08:10 AM
this was the message i got..

You have an error in your SQL syntax near 'by='Josh',realtitle='Josh has become smart' WHERE id='13'' at line 1

i used this code:


mysql_select_db("news",$db)or die(mysql_error());

$result = mysql_query("SELECT * FROM newsdb",$db)or die(mysql_error());

if ($submit) {

$sql = "UPDATE `newsdb` SET id='$id',displaytitle='$displaytitle',news='$news' ,displaydate='$displaydate',time='$time',by='$by',realtitle='$realtitle' WHERE id='$id'";

// run SQL against the DB

$result = mysql_query($sql)or die(mysql_error());

printf("<font face='Verdana' size=1 color=#667AA6>Thankyou - The Selected News Item has been Edited.! <br> To Return to the Admin Center - Click <a href=index.php3>Here</a>\n");

}


?>



Any more suggestions?

firepages
08-04-2002, 08:18 AM
hi if 'id' is an integer field (INT,SMALLINT) etc you need to lose the sngle quotes >'< , also no need to reupdate the id....


$sql = "UPDATE `newsdb` SET displaytitle='$displaytitle',news='$news' ,displaydate='$displaydate',time='$time',by='$by',realtitle='$realtitle' WHERE id=$id";

Spookster
08-04-2002, 08:22 AM
Do not create two topics with the same general question.

http://www.codingforums.com/showthread.php?s=&threadid=3268

Tidus
08-04-2002, 08:25 AM
Hmm, this is the error -

You have an error in your SQL syntax near 'by='Josh',realtitle='Josh has become smart' WHERE id=13 ' at line 1

this is the code:



$sql = "UPDATE newsdb SET displaytitle='$displaytitle',news='$news',displaydate='$displaydate',time='$time',by='$by',realtitle ='$realtitle' WHERE id=$id ";


Any more suggestions again?
Thanks for your help!

mordred
08-04-2002, 02:20 PM
Well, this is the PHP code, but that does not tell us how the query string really looked like, with all these PHP variables interpolated.
It's generally helpful for debugging SQL activities to echo out the query string with sth. like echo $sql in your case, so you can see what really was in the string that made the SQL interpreter choke.

Also, make sure that the format of the single table fields match the format of the contents of the PHP variables. In your case, perhaps you've forgot to run an addslashes() over some critical fields.