only partial mysql 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
miramichiphoto
Forum Newbie
Posts: 14
Joined: Thu Mar 17, 2011 1:12 pm

only partial mysql UPDATE

Post 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' ; 
			
			} 
divedj
Forum Commoner
Posts: 47
Joined: Wed Dec 29, 2010 4:32 am
Location: Malta

Re: only partial mysql UPDATE

Post 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.
litebearer
Forum Contributor
Posts: 194
Joined: Sat Mar 27, 2004 5:54 am

Re: only partial mysql UPDATE

Post 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' ; 
	} 
}

Post Reply