...

View Full Version : update WHERE clause?



boeing747fp
03-23-2004, 10:35 PM
can this be done? i can't get it to work... i want it to update the row with id that equals $id and update all fields... but it doesnt work.


<?php
$title = $_POST['title'];
$body = $_POST['body'];
$date = $_POST['date'];
$id = $_POST['id'];

$con = mysql_connect("localhost","user","pwd");
$db = mysql_select_db("database",$con);
$sql = "UPDATE `table` SET `topic` = '$title', `body` = '$body', `date` = '$date' WHERE `ids` = '$id'";
mysql_query($sql) or die("Error: ".mysql_error());
?>

sweenster
03-23-2004, 10:46 PM
dont like what you've done with all those ' marks in the $sql line.... try this:


<?php
$title = $_POST['title'];
$body = $_POST['body'];
$date = $_POST['date'];
$id = $_POST['id'];

$con = mysql_connect("localhost","user","pwd");
$db = mysql_select_db("database",$con);
$sql = "UPDATE table SET topic = ".$title.", body = ".$body.", date = ".$date." WHERE ids = ".$id."";
mysql_query($sql) or die("Error: ".mysql_error());
?>

boeing747fp
03-23-2004, 11:05 PM
nvm... i fixed it... it was a problem in my form... i didnt have a </form> echoed in my previous php page... sorry.

missing-score
03-23-2004, 11:06 PM
actually the code you just posted sweenster will not work, and his code is more likley to.

You need to put strings in single quotes, and there is nothing wrong with backquotes around fields as far as I know, I usually use them.


<?php
error_reporting(E_ALL);

$title = $_POST['title'];
$body = $_POST['body'];
$date = $_POST['date'];
$id = $_POST['id'];

$con = mysql_connect("localhost","user","pwd");
$db = mysql_select_db("database",$con);
$sql = "UPDATE `table` SET `topic` = '".$title."', `body` = '".$body."', `date` = '".$date."' WHERE `ids` = '".$id."'";

echo $sql; // print your sql statement to make sure it looks ok

mysql_query($sql);
?>

Also ensure your input contains no single quotes, as this will cause it not to work if they are not automatically being escaped (\\')

missing-score
03-23-2004, 11:07 PM
guess my last post was useless then :p, but for debugging, its always a good idea to print your SQL statements if your having problems, to make sure the statement is correct, then check DB connection, and then, like you did, html code.

sweenster
03-23-2004, 11:34 PM
oops! forgot to put the quotes in around the variables:o
(must remember to check it next time).

Apart from that it should work OK without the quotes around the string names...

example:
(from the first but of code i had to hand)



$query = "UPDATE stats SET usernum=\"".$s_userid."\" WHERE id=\"".$statnum."\"";

missing-score
03-23-2004, 11:48 PM
Yes it will work like that, although there is nothing wrong with backquotes :)

Also, i usually use single quotes for string values inside of double quotes, as you dont have to escape them.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum