Page 1 of 1

PHP update

Posted: Fri Aug 11, 2006 8:00 pm
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]

Posted: Fri Aug 11, 2006 9:43 pm
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'");

Posted: Fri Aug 11, 2006 9:55 pm
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;
}

Re: PHP update

Posted: Sat Aug 12, 2006 6:17 am
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.

Posted: Sat Aug 12, 2006 7:26 am
by Jenk
escape your input.

Posted: Sat Aug 12, 2006 9:05 am
by feyd
Jenk wrote:escape your input.
That means mysql_real_escape_string(), not addslashes()