-1

Im trying to update a table using question mark placeholders, loosely based on this example link now the problem is i only want to update if the variable has a value. when using named place holders something like this works as expected:

$contactDetails= $db_con->prepare("UPDATE `userData` SET
             `name`= :name,
             `submitDate`= :submitDate,
             `contacts`= IF(:contacts = '', `contacts`, :contacts),
             `email`= :email
              WHERE `submit_id`= :submit_id");

Now in this case I'm using question mark placeholders, based on example referred above, and executing the statement as follows

$this_query= $db_con->prepare($query);
$this_query->execute($values);

so i changed $values[]=$v; to $values[]= 'IF(""="",'.$k.','.$v .')'; and it does not perform as excepted, the entire string is added to the database row including the 'IF….

when looking at the query log it shows UPDATE userData SET email = 'IF( \"\"=\"\",email, [email protected])',… and so on It seems to me that the IF is going as a string not as a conditional statement, is there a way around this? Thanks

7
  • This IF(""="",'.$k.','.$v .')'; is treated as text/data, not SQL. That's why you use prepares, right!?
    – djot
    Sep 23, 2013 at 0:07
  • I think WHERE .. AND contacts = '' would be more appropriate? (I'm not entirely sure how to read the question - as to which value is being checked, etc - but it should be solvable just using a WHERE clause or, if the value to be checked is the one in PHP, then simply don't execute the DML if not appropriate.) Sep 23, 2013 at 0:16
  • @djot how could i get it as SQL not text/data? Becouse everything else works fine other than that... Sep 23, 2013 at 0:55
  • @user2246674 im not sure what u mean? the value being checked is from a php var, but it has to go in because it's in the $possible array, if u look at the link u will see what i mean. Sep 23, 2013 at 0:57
  • So, you only want to update (set new data to) contacts when the given variable (which one is it? $k, $v) is not empty?
    – djot
    Sep 23, 2013 at 1:01

2 Answers 2

0
<?php

  $values = ARRAY();


  $sql_query = "UPDATE `userData` SET `name`= ?, `submitDate`= ?, ";
  $values[] = $variable_name; //:name, now ?
  $values[] = $variable_date; //:submitDate, now ?

  if (!empty($v)) {
    $sql_query .= "`contacts`= ?,";
    $values[] = $v; //:contacts, now ?
  }

  $sql_query .= "`email`= ? WHERE `submit_id`= ?;";
  $values[] = $variable_email; //:email, now ?
  $values[] = $variable_submit_id; //:submit_id, now ?

  $contactDetails= $db_con->prepare($sql_query);
  $this_query->execute($values);

?>
3
  • that will not work in this case, because the number if fields to update is variable, thats why i thought the best bet was to put check in SQL. What i was trying to do was remove those quotes around the IF condition. PS. the loop is in the link Sep 23, 2013 at 1:31
  • You can't compare a stupid "do all - eyes wide shut" loop with special conditions like here. Only this single validation of contacts makes the "example" totally useless. It get's much more complicated as you even see here without the looping functionality.
    – djot
    Sep 23, 2013 at 1:36
  • it's just unfortunate as the query string is almost as i need it to be except for the IF being treated as text not SQL Sep 23, 2013 at 2:31
0

As opposed to creating the 2 arrays, i just create one string with all the variables in place and do the IF conditional check in SQL. As i said before this is based on this example by Kitet.

<?php 

$values=array();

//this will be an array of possible fields that are in your table
$possible=array('field1', 'field2', 'field3');

$i=0;
$len=count($_POST);
$query='update table_name set ';
foreach($_POST as $key => $value){
$k=htmlspecialchars($key);
$v=htmlspecialchars($value);
if(in_array($k, $possible)){
$query .= '`' .$k .'`' .'= IF("'.$v .'"= "", `' .$k .'`, "' .$v .'")'; //changes here!!!

if($i < ($len-1)) $query .= ', ';
$i++;
}
}
$query .= 'where table_id = '.$table_id;

$update_data= $db_con->prepare($query); // I prepare the statement here as a safety net  
$update_data->execute(); // the execute it

?>

And now all works as expected :) I hope this helps someone else out there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.