Page 1 of 1

only partial mysql UPDATE

Posted: Tue Mar 22, 2011 10:45 pm
by miramichiphoto
The code below is only successful in updating `phone1`, `phone2`, and `email` fields in the db.
The `payment` and `payment_date` fields do not update, and do not throw an error. Any ideas?

Code: Select all

if (isset($_POST['submit'])) 

{
$x = $_POST['payment'];

		if (   is_int($x)   ) 
							   
		{
			$y = date("Y-m-d");				   
			$payment = trim($_POST['payment']);
			$display_name = trim($_POST['display_name']);
			$phone1 = trim($_POST['phone1']);
			$phone2 = trim($_POST['phone2']);
			$email = trim($_POST['email']);
			$query = "UPDATE `users` SET `payment_date`= '".$y ."'";
			$query .= " `payment`='".$payment."', `phone1`='".$phone1."',`phone2`='".$phone2."',`email`='".$email."'";
			$query .= " WHERE `display_name`='".$display_name."'";
			 mysql_query($query, $connection) or die(mysql_error());
			$message = 'User was edited successfully' ; 
			
			} 

Re: only partial mysql UPDATE

Posted: Wed Mar 23, 2011 6:40 am
by divedj
Since your parts to be updated have to be all "," seperated you have to start with a "," after your first element.
if your second part of the update query is optional, I would start with a "," in your second part of the query like:

Code: Select all

$query .= ",  `payment`='".$payment."', `phone1`='".$phone1."',`phone2`='".$phone2."',`email`='".$email."'";//check "," at the beginning of your querypart
otherwise you could do it also in the first part of the query like:

Code: Select all

$query = "UPDATE `users` SET `payment_date`= '".$y ."',"; //here "," at the end of the query.

Re: only partial mysql UPDATE

Posted: Wed Mar 23, 2011 6:42 am
by litebearer
1. have you echoed out the variables to ascertain they contain what you expect? (this means also echo your query)

2. touched up your code - nothing major - but see if it helps...

Code: Select all

if (isset($_POST['submit'])) {
	$x = $_POST['payment'];
	if ( is_int($x) ) {
		$y = date("Y-m-d"); 
		$payment = trim($_POST['payment']);
		$display_name = trim($_POST['display_name']);
		$phone1 = trim($_POST['phone1']);
		$phone2 = trim($_POST['phone2']);
		$email = trim($_POST['email']);
		$query = "UPDATE users SET payment_date= '$y' , ";
		$query .= "payment = '$payment', phone1 = '$phone1', phone2 = '$phone2', email = '$email', ";
		$query .= " WHERE display_name = '$display_name'";
		mysql_query($query, $connection) or die(mysql_error());
		$message = 'User was edited successfully' ; 
	} 
}