default value for $_POST[];

Issue

I have a problem with default value for $_POST[];
So i have a html form with textboxes and the informations is sent to a php script. The php script has a sql query that is being sent to my database. But if my textbox in html form is empty the query doesnt have a value. So i want to set my post to a default value 0 so it returns a value atleast.

So here is an example of html form (This is not my actuall script. Just an example.

<form action="testscript.php" method="POST">
    <input type="id" name="ID"/>
    <input type="text" name="test"/>
    <input type="submit" value="Send"/>
</form>

Ok so this script will send both id and test textboxes will always have a number value. And it sends the information to testscript.php

Here is testscript.php example

    $conn = mysqli_connect('host', 'dbuser', 'dbpass', 'dbname');
    $id = $_POST['id'];
    $test = $_POST['test'];
    $sql = "INSERT INTO test_table (id, test) VALUES ($id, $test)";

    if (mysqli_query($conn, $query)) {
        echo "Success";
    } else {
        echo "Failed" . mysqli_error($conn);
    }

Alright so now if i submit my html form to php script without inserting any text to the textboxes the query will look like this

INSERT INTO test_table (id, test) VALUES ( , )

But the query should be like this

INSERT INTO test_table (id, test) VALUES (0, 0)

So. I know i can use value attribute in the html tag but then the value will be visible in the textbox and i dont want that.

And i know i can do an if statment to make a default value like this

if (isset($_POST['test'])) {
    $test = $_POST['test'];
} else {
    $test = 0;
}

But now the problem is that i would have to do that if statment for every textbox and my html form have more than 100 textboxes. So i dont want to make an if statment for every textbox because then my script will be way to big and it will take hours.

So is there any way to set a default value for all the textboxes without using if statment in php or value attribute in html form?

Solution

I know it seems like a pain but you MUST check that all inputs are valid. You can simplify the amount of code by using a ternary operator like this.

$id = isset($_POST['id']) ? $_POST['id'] : 0;
$test = isset($_POST['test']) ? $_POST['test'] : 0;

....

And no, it won’t take hours even with hundreds of them.

To make this slightly less painful to code you can use the power of looping with PHP’s variable variables

The most painful part will be creating an array with all your field names

$fields = array('id', 'test', 'extra', 'more', ..., 'one_hundred');

Then loop through that array creating variable names and at the same time escaping the strings – if they are there – otherwise set a value of 0 (zero). You might want/need to set this to “” (empty string)

foreach($fields as $field_name)
{
   ${$field_name} = isset($_POST[$field_name]) ? mysqli_real_escape_string($conn, $_POST[$field_name]) : 0;
}

You now have the variables $id, $test, $extra, $more, …., $one_hundred available for your use.

Answered By – DFriend

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published