Jump to content

database update problem


davidjones1990

Recommended Posts

Hello everybody, I have a script that allows a user to set themselves a target and a script that allows them to edit the target if they decide to.

 

Now everything works as it should until the data from the form (on the edit target page) should be updated in the database. I require that the user must update at least one field. For example they may want to extend the completion date so I would only want to update the completion date field in the database and the fields the user left blank should not be updated in the database.

 

The problem I am having is regardless of if the user enter a value into the field it updates the database so if the user leaves a field blank it updates the database will a blank value.

 

Here is my code:

 

//If the user clicks the update button on a WEIGHT LOSS target run the following code to update the database
if(isset($_POST['target_weight_loss_update'])){
$target_weight_loss_update = $_POST['target_weight_loss_update'];
$weight_loss_complete_update = $_POST['weight_loss_complete_update'];
$weight_loss_comment_update = $_POST['weight_loss_comment_update'];
$weight_loss_user_id_update = $_POST['weight_loss_user_id_update'];
//Check that at least one value has been entered into the form
if(empty($target_weight_loss_update) && empty($weight_loss_complete_update) && empty($weight_loss_comment_update)){
	$error_msg = 'Please enter at least one value to update your '.$target_type.' target';
}
//Check to see what variables have values in them so we know what fields to update in datebase
//If the user has entered a value for their TARGET WEIGHT LOSS update that value in the database 
if(isset($target_weight_loss_update) || !empty($target_weight_loss_update)){
	$target_weight_loss_update_sql = mysql_query("UPDATE user_targets SET target_weight='$target_weight_loss_update' WHERE id='$target_id' LIMIT 1") or die (mysql_error());
	//Check that the update was successful
	if($target_weight_loss_update_sql){
		$error_msg = 'Target '.$target_type.' updated successfully';
	} else {
		$error_msg = 'Could not update target. Please try again.';
	}
}
//If the user has entered a value for their TARTGET WEIGHT LOSS DATE update that value in the database
if(isset($weight_loss_complete_update) || !empty($weight_loss_complete_update)){
	$weight_loss_completion_date_sql = mysql_query("UPDATE user_targets SET target_date='$weight_loss_complete_update' WHERE id='$target_id' LIMIT 1") or die (mysql_error());
	//Check that the update was successful
	if($weight_loss_completion_date_sql){
		$error_msg = 'Target '.$target_type.' updated successfully';
	} else {
		$error_msg = 'Could not update target. Please try again.';
	}
}
//If the user has entered a value for their TARGET WEIGHT LOSS COMMENT update that value in the database
if(isset($weight_loss_comment_update) || !empty($weight_loss_comment_update)){
	$weight_loss_comment_update_sql = mysql_query("UPDATE user_targets SET extra_info='$weight_loss_comment_update' WHERE id='$target_id' LIMIT 1") or die (mysql_error());
	//Check that the update was successful
	if($weight_loss_comment_update_sql){
		$error_msg = 'Target '.$target_type.' updated successfully';
	} else {
		$error_msg = 'Could not update target. Please try again.';
	}
}
}

 

So the question I ask is how can I stop entering blank values into the database and only update the information the user enters into the form?

 

Also I was thinking of having one mysql query at the end and using the IF statements to build the query. Thoughts?

 

Thanks for any advice.

Link to comment
Share on other sites

presuming you want no fields empty...

 

1. populate the form field values with the existing data

 

2. upon submission of the form, verify that no fields are empty

    if empty redirect to form and repopulate the fields with warning message

 

3. update the database using ALL fields

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.