2

Hi i have a table with different fields and i make an insert query and i want to skip the insert query if a value is null in one of the fields. Example

Table
id    name    surname    kids
1     john      doe       2
2     mary      jones     3

and i want if the new insert has no value in the field kids to skip that entry. is there a possible way to check that?` the code i have is this

INSERT INTO `clients`.`kids` (`id`, `name`,`surname`,`kids`) VALUES (NULL,'".$data['name']."','".$data['surname']."','".$data['kids']."');";`
2
  • 1
    Please make kids field NOT NULL in DB Commented Apr 21, 2015 at 6:54
  • Try if(!empty($data['kids'])){//your code} Commented Apr 21, 2015 at 6:54

3 Answers 3

2

Just add a check -

if (!empty($data['kids'])) {
    // run the query
}
Sign up to request clarification or add additional context in comments.

Comments

0

You asked to skip only if there is no Kids entered

if (!empty($data['kids'])) {
    //  query
}

Comments

0

You can check like This :

if(!empty($data['kids']) && isset($data['kids']))
 {
       // Write your code here 
 }

1 Comment

If I'm not wrong then Please Correct me. !empty itself check both conditions is variable set or not and is variable is not empty. So within your code does isset function make any difference

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.