3

I'm building a form to update multiple columns of data. This code has been partially successful for my purposes. The only thing left to do is include (if IS NOT NULL) for the update query. I'm not sure how to go about this.

In other words, I only want the UPDATE to execute if the $_POST value is not empty.

<form>
<input type='text' name='input1' />
<input type='text' name='input2' />
<input type='text' name='input3' />
<input type='submit' value='submit' />
</form>

<?php
//db connect

$1=$_POST['input1'];
$2=$_POST['input2'];
$3=$_POST['input3'];

mysql_query("UPDATE table
             SET a = $1
                 b = $2
                 c = $3
             WHERE row = 'row_id");

);


?>

Thanks in advance for the help.

(To save you from some extra typing, my original code escapes characters so warnings of SQL injections aren't necessary. I'm also in the process of familiarizing myself with "mysqli_query", so no need to comment on that either.)

0

8 Answers 8

23

You can use COALESCE

       UPDATE table
         SET a = COALESCE($1, a),
             b = COALESCE($2, b),
             c = COALESCE($3, c)
         WHERE row = ''
Sign up to request clarification or add additional context in comments.

Comments

2
COALESCE

is the solution. Its like equivalent to Oracle's NVL. You can use it like below in a prepared statement using parameters

UPDATE
    tbl_cccustomerinfo
SET
    customerAddress = COALESCE(?,customerAddress),
    customerName =  COALESCE(?,customerName),
    description =  COALESCE(?,description)
WHERE
    contactNumber=?

Comments

2

COALESCE

COALESCE is the best option.

MEANING: COALESCE(a,b) simply means that PUT a,but if a is NULL(not POSTED/PUT in your case)then replace it with b.

 `mysql_query("UPDATE table
         SET a =COALESCE($1,a)
             b = COALESCE($2,b)
             c = COALESCE($3,c)
         WHERE row = 'row_id")`

Comments

1
if(!empty($_POST['input1'])){ ...mysql query.. }

Comments

1
<?php
$query="UPDATE table SET"
$query.=(!empty($_POST['input1']))? "a=$1,":"";
$query.=(!empty($_POST['input2']))? "b=$2,":"";
$query.=(!empty($_POST['input3']))? "c=$3,":"";
$query=substr($query,0,-1);
$query.="WHERE row = 'row_id'"
mysql_query($query);
?>

Comments

0

Try with this:

$updateList = array();
if(!empty($_POST['input1'])) {
    $updateList[] = 'a = '.$_POST['input1'];
}

//Do it for every input...

if(!empty($updateList)) {
    $sql = 'UPDATE table SET '.implode(', ', $updateList).' WHERE row = 1';
}

Note that I didn't escape/quote anything, since you said you know how to do it.

Comments

0

Just make an array of them. Like so:

if(empty($_POST['input1']) && empty($_POST['input2']) && empty($_POST['input3'])) {
   return false;
  } else {
   $updates = array();

   if(!empty($_POST['input1'])) 
     $updates[] = 'a='".$_POST['input1']."';

   if(!empty($_POST['input2'])) 
    $updates[] = 'b='".$_POST['input2']."';

   if(!empty($_POST['input3'])) 
    $updates[] = 'c='".$_POST['input3']."';


        $query = "UPDATE table SET".$updates."WHERE row_id=''";
        $sth = $sth->connect->prepare($query);
        $sth->execute();

        ));
   }

Comments

-1

Try with a simple codition

$1  =   isset($_POST['input1'])? $_POST['input1'] : 'a';
$2  =   isset($_POST['input2'] ? $_POST['input2'] : 'b';
$3  =   isset($_POST['input3'] ? $_POST['input3'] : 'c';


if(count($_POST) > 0){

    mysql_query("UPDATE table
                 SET a = $1
                     b = $2
                     c = $3
                 WHERE row = 'row_id");

    );
}

1 Comment

Note: It will update if any one value is not null and other are.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.