2

Is there anyway to ignore the inputs if they are null and still succeed to update the values that are entered? For example if I only give itemID1 an value and itemID2 is null.

$upd = "UPDATE booking SET status='$status', CalDate='$CalDate', DueDate='$DueDate' WHERE itemID IN ('$itemID1', '$itemID2')";
1
  • 1
    @Sadikhasan: thanks for all your editing! In this case, since the columns you have emboldened are essentially code, backticks would be better - bold is essentially for spoken emphasis in ordinary writing. I wouldn't normally mention this, but you've made a lot of this kind of edit, and people may end up changing them later on. (Some of your edits are probably also too minor: "I'm" is an acceptable contraction, and it does not need expanding to "I am"). Commented Jan 12, 2015 at 12:01

2 Answers 2

2

Just use isset function in php

if(isset($itemID1) && isset($itemID2)){
$upd = "UPDATE booking SET status='$status', CalDate='$CalDate', DueDate='$DueDate' WHERE itemID IN ('$itemID1', '$itemID2')";
}

Or check it using empty function

if(!empty($itemID1) && !empty($itemID2)){
$upd = "UPDATE booking SET status='$status', CalDate='$CalDate', DueDate='$DueDate' WHERE itemID IN ('$itemID1', '$itemID2')";
}

Hope this helps you

Sign up to request clarification or add additional context in comments.

Comments

0

I assume you are not using PDO or active records, So in your case following might help.

<?php

$items = array(
    'itemID1' => "Hello",
    'itemID2' => "",
    'itemID3' => "World"
    );

$item = "'".implode("', '", array_filter($items))."'";
$upd = "UPDATE booking SET status='status', CalDate='CalDate', DueDate='DueDate' WHERE itemID IN ($item)";

echo $upd;
?>

hope this helps.

Also you have to make sure you change these status='status', CalDate='CalDate', DueDate='DueDate' (I removed the $ to make them string from variable), if you plan to copy paste the above piece of code.

1 Comment

Yeah I just use plain up php code, but my problem is fixed now. Thanks anyway :)

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.