0
<!DOCTYPE html>
<html>
    <head>
        <title>Data</title>
    </head>

    <body>
        /* POST it here  */
        <form  action="this.php" method="post">
            <input type='text' name='SpouseName' required/>
            <input type='text' name='SpouseAge' /> 
            <input type="submit" name="submit" value="SUBMIT"/>
        </form>
    </body>
</html>

/* Here is the PHP CODE */
<?php   
include 'dbh2.php'; 
$SpouseName = $_POST['SpouseName']; 
$SpouseAge = $_POST['SpouseAge'];  
$sql = "INSERT INTO test (SpouseName, SpouseAge) VALUES ('$SpouseName', '$SpouseAge')";    
$result = $conn->query($sql);   
?>

I want to ignore empty input like spouseage how to do it?

2 Answers 2

1

You can check that the values have been set with isset(), and check that they're not empty with !empty():

$SpouseName = $_POST['SpouseName'];
$SpouseAge = $_POST['SpouseAge'];
if (isset($SpouseName) && !empty($SpouseName) && isset($SpouseAge) && !empty($SpouseAge)) {
  $sql = "INSERT INTO test (SpouseName,SpouseAge) VALUES ('$SpouseName','$SpouseAge')";
} else {
  echo "The values weren't set or were empty!";
}

Though note that it's better to check the $_POST values are set and not empty in and of themselves before assigning to the variables:

if (isset($_POST['SpouseName']) && !empty($_POST['SpouseName'])) {
  $SpouseName = $_POST['SpouseName']
}
if (isset($_POST['SpouseAge']) && !empty($_POST['SpouseAge'])) {
  $SpouseAge = $_POST['SpouseAge'];
}
if ($SpouseName && $SposeAge) {
  $sql = "INSERT INTO test (SpouseName,SpouseAge) VALUES ('$SpouseName','$SpouseAge')";
} else {
  echo "The values weren't set or were empty!";
}

Hope this helps :)

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

4 Comments

Looks like you've spelt the second variable wrong in: if ($SpouseName && $SposeAge) { -- $SpouseAge
@giollianosulit -- Good spotting! Not really sure how that happened haha. I've fixed that now :)
i have to many input i just sample 2 inputs how can i ignore empty inputs when i save to database it says Unidentified index but the input that was filled is saved on database
You do it just as above; simply specify each of the different POSTS. An undefined index is another issue entirely.
0

I Divided the Queries into 2 sections. Now you can do form validation for each separate input and test the data. I also placed isset for the submit to check if it has been clicked. WE then test if it is empty if not insert and we do it for each.

if(isset($_POST['submit'])){
$SpouseName = $_POST['SpouseName'];
$SpouseAge = $_POST['SpouseAge'];
if(!$SpouseName==''){$sql = "INSERT INTO test (SpouseName) VALUES ('$SpouseName')";}
if(!$SpouseAge ==''){$sqlb = "INSERT INTO test (SpouseAge) VALUES ('$SpouseAge')";
} 
}

1 Comment

With this code, two different queries will be executed and will insert 2 different records in db which is different from the OP needs...

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.