Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-23-2004, 09:35 PM   PM User | #1
boeing747fp
Regular Coder

 
Join Date: Oct 2003
Posts: 599
Thanks: 1
Thanked 1 Time in 1 Post
boeing747fp is an unknown quantity at this point
update WHERE clause?

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 Code:
<?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 is offline   Reply With Quote
Old 03-23-2004, 09:46 PM   PM User | #2
sweenster
Regular Coder

 
Join Date: Sep 2002
Location: Scotland
Posts: 407
Thanks: 0
Thanked 0 Times in 0 Posts
sweenster is an unknown quantity at this point
Re: update WHERE clause?

dont like what you've done with all those ' marks in the $sql line.... try this:

PHP Code:
<?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());
?>
__________________
My body's a temple... and like those ancient Greek ones it's a ruin
sweenster.co.uk
sweenster is offline   Reply With Quote
Old 03-23-2004, 10:05 PM   PM User | #3
boeing747fp
Regular Coder

 
Join Date: Oct 2003
Posts: 599
Thanks: 1
Thanked 1 Time in 1 Post
boeing747fp is an unknown quantity at this point
nvm... i fixed it... it was a problem in my form... i didnt have a </form> echoed in my previous php page... sorry.
boeing747fp is offline   Reply With Quote
Old 03-23-2004, 10:06 PM   PM User | #4
missing-score
Senior Coder


 
missing-score's Avatar
 
Join Date: Jan 2003
Location: UK
Posts: 2,194
Thanks: 0
Thanked 0 Times in 0 Posts
missing-score is on a distinguished road
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 Code:
<?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 is offline   Reply With Quote
Old 03-23-2004, 10:07 PM   PM User | #5
missing-score
Senior Coder


 
missing-score's Avatar
 
Join Date: Jan 2003
Location: UK
Posts: 2,194
Thanks: 0
Thanked 0 Times in 0 Posts
missing-score is on a distinguished road
guess my last post was useless then , 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.
missing-score is offline   Reply With Quote
Old 03-23-2004, 10:34 PM   PM User | #6
sweenster
Regular Coder

 
Join Date: Sep 2002
Location: Scotland
Posts: 407
Thanks: 0
Thanked 0 Times in 0 Posts
sweenster is an unknown quantity at this point
oops! forgot to put the quotes in around the variables
(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)

Code:
$query = "UPDATE stats SET usernum=\"".$s_userid."\" WHERE id=\"".$statnum."\"";
__________________
My body's a temple... and like those ancient Greek ones it's a ruin
sweenster.co.uk
sweenster is offline   Reply With Quote
Old 03-23-2004, 10:48 PM   PM User | #7
missing-score
Senior Coder


 
missing-score's Avatar
 
Join Date: Jan 2003
Location: UK
Posts: 2,194
Thanks: 0
Thanked 0 Times in 0 Posts
missing-score is on a distinguished road
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.
missing-score is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:12 AM.


Advertisement
Log in to turn off these ads.