4

I have an UPDATE query and using Ajax, I wanted to know if any value is empty can I only update the values that not empty in the database. I don't know if this is possible to have a if statement or something to check to skip the empty values. I know I can just add another form element but just wanted to know if there was another solution.

Only if the data is POST from front end form. If data not POST don't update this Title = '.$title .',

$id = $_POST['id'];
$title = "";
$description = $_POST['Description'];
$date = $_POST['Date'];

 $query = 'UPDATE user SET

  `id` = '.$id.', 
  `Title` = '.$title .', 
  `Description` = '.$description.', 
  `Date` = '.$date =.' 
  WHERE `id` = '.$id;

 $result = mysql_query($query) or die("<b>A fatal MySQL error occured</b>.<br />Query:  ".$query."<br />Error: (".mysql_errno().") ".mysql_error());

Update: This is what worked for me. Thanks Karim Daraf

$query = " UPDATE user SET Title = Coalesce($title,Title ) etc...

3
  • Please ensure you validate your inputs. Don't do it on JS side, do it on server side. Commented Sep 15, 2014 at 22:38
  • Your above code is a extremely security risk. 1) Do not use mysql_ functions, they are deprecated. Instead use mysql i or pdo, prepared statements. If you don't use prepared statements make sure to use a escape string. Commented Sep 15, 2014 at 23:08
  • The code above is an example I'm only looking to see if there's away to update only the value that not empty. I know about client side validation the example are different variable from what I'm using. I think COALESCE is what I'm looking for but got to understand how to use it with my codes. Commented Sep 15, 2014 at 23:14

3 Answers 3

8

Try it with Coalesce .

   $query = " UPDATE user 
   SET 
 `Title`       = CASE WHEN `Title`='' or `Title` IS NULL THEN '$title' END, 
 `Description` = CASE WHEN `Description`='' Or `Description` IS NULL THEN '$description' END, 
  `Date`       = CASE WHEN `Date`='' Or Date` IS NULL THEN '$date' END
    WHERE `id` = '".$id."' ";

or :

  $query = " UPDATE user 
  SET 
 `id`         = Coalesce('$id''".$id."' , NULLIF(`id`,'')), 
`Title`       = Coalesce('$title''".$title."',NULLIF(`Title`,'') ) , 
`Description` = Coalesce('$description''".$description."' , NULLIF(`Description`,'') ) , 
 `Date`       = Coalesce('$date''".$date."',NULLIF(`Date`,'')) 
 WHERE `id` = '$id''".$id."' ";
Sign up to request clarification or add additional context in comments.

7 Comments

I seen Coalesce before but didn't understand how to use this function with my query. I'm trying it out now but getting syntax errors so probably need to fix my quotes.
This is working syntax no errors! Now re-writing to my query to see if I can get the Coalesce to work. Will keep you posted. Getting this error on page "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near" Thanks.
The query is similar to above just different names and I'm using $_POST[''] so I need to make some variables. The other part I'm doing now is writing the Ajax to send POST data.
did the columns have empty values or null values in your table ?
Okay, I tried to UPDATE only "Description" field worked but the other fields are now empty. I added content in the database and yes it still UPDATES all columns.
|
1
$query = 'UPDATE user SET

  `id` = '.$id.', 
  `Title` = COALESCE(NULLIF("'.$title.'", ""),`Title`), 
  `Description` = "'.$description.'", 
  `Date` = "'.$date.'" 

  WHERE `id` = "'.$id.'"';

Comments

0

Not sure to understand: you have data and want to update, but only if some fied in the DB are empty? In the case perfom only a where:

 $query = 'UPDATE user SET
  `id` = '.$id.', 
  `Title` = '.$title .', 
  `Description` = '.$description.', 
 `Date` = '.$date =.' 
  WHERE `id` = '.$id.' AND Title = '';

for example

1 Comment

Only if the data is POST from front end form. If data not POST don't update this Title = '.$title .',

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.