Biker John
Biker John

Reputation: 2711

Check for empty fields or insert everything?

What is better form data manipulation system?

(in this case when inserting the data into the database).

I usually use the first method, but in this case i have A LOT of fields and really dont want to write code to check every single one.

Upvotes: 0

Views: 86

Answers (2)

By The Way, "" Is not like NULL, "" is Empty String and Null is No Value At All.

You can verify if the Data is present by looping the dataset and verifying each variable if it has "" and then turn into NULL before insert into database.

The only case when a Text from Form will be NULL is if he wasn't into form, so the PHP will treat it like null.

Best pratice is to find a way to programatically loop into data and perform all the verifications needed, instead of write every single piece of data.

$safe_array = array();

foreach($data as $i => $v) {
    if($v=="") {
        $safe_array[$i] = null;
    }
    else if (custom_filter($v)) {
        $safe_array[$i] = null;
    } else {
        $safe_array[$i] = $v;
    }
  }

Upvotes: 1

GolezTrol
GolezTrol

Reputation: 116110

You would have to check them anyway, right? How else would you know whether they should be inserted or not?

Or do you mean that some fields are unused completely by the form, so they are always null? In that case, you don't need to include them in your insert statement.

Upvotes: 0

Related Questions