PHP update

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
jordansoltman
Forum Newbie
Posts: 1
Joined: Fri Aug 11, 2006 7:57 pm

PHP update

Post by jordansoltman »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


This is my code which won't work for some reason. It says record updated but it doesn't actually do it.

Code: Select all

<?
$id=$_POST['id'];
$type=$_POST['type'];
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$problem=$_POST['problem'];
$helped=$_POST['helped'];
$date=date(Y-m-d);

$username="root";
$password="1247133182";
$database="customers";
mysql_connect(localhost,$username,$password);

mysql_query ("UPDATE customerdata SET type='$type',firstname='$firstname',lastname='$lastname',email='$email',phone='$phone',problem='$problem',helped='$helped',date='$date' WHERE id='$id'");
echo "Record Updated";
mysql_close();
?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
waradmin
Forum Contributor
Posts: 240
Joined: Fri Nov 04, 2005 2:57 pm

Post by waradmin »

I may be way off, but I thought it needed to be:

Code: Select all

mysql_query ("UPDATE customerdata SET(type, firstname, lastname, email, phone, problem, helped, date) VALUES('$type', '$firstname', '$lastname', '$email', '$phone', '$problem', '$helped', '$date') WHERE id='$id'");
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

It says record updated but it doesn't actually do it.
Yep that's because you are just echoing 'Record Updated' unconditionally. You might want to do this instead:

Code: Select all

$q = 'your query';
$result = mysql_query($q);
if (!$result) {
    echo 'Error with query<br />';
    echo mysql_error();
    exit;
}
blackbeard
Forum Contributor
Posts: 123
Joined: Thu Aug 03, 2006 6:20 pm

Re: PHP update

Post by blackbeard »

You have the date statement wrong, you need to wrap it in quotes.

Code: Select all

<?
$id=$_POST['id'];
$type=$_POST['type'];
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$problem=$_POST['problem'];
$helped=$_POST['helped'];

/*  Change this:
$date=date(Y-m-d);

To this:*/

$date = date('Y-m-d');

$username="root";
$password="1247133182";
$database="customers";
mysql_connect(localhost,$username,$password);

mysql_query ("UPDATE customerdata SET type='$type',firstname='$firstname',lastname='$lastname',email='$email',phone='$phone',problem='$problem',helped='$helped',date='$date' WHERE id='$id'");
echo "Record Updated";
mysql_close();
?>
The update syntax looks right, the syntax that waradmin showd is for an Insert/Replace statement.

At the end of the mysql_query, you can add in a die statement, to get an idea of where things are failing, like so:

Code: Select all

mysql_query ("UPDATE customerdata SET type='$type',firstname='$firstname',lastname='$lastname',email='$email',phone='$phone',problem='$problem',helped='$helped',date='$date' WHERE id='$id'")or die(mysql_error());
Hope this helps.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

escape your input.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Jenk wrote:escape your input.
That means mysql_real_escape_string(), not addslashes()
Post Reply