tatty27 tatty27 - 1 year ago 118
PHP Question

Empty fields causes mysql error

I have a form that has several fields that are optional. When the mysql query saves the data from the form it fails if any of the fields are are left blank.

What is the best way of getting around this? I would prefer the field in the database to remain empty rather than having to enter data just to keep the query happy.

$sql = "INSERT INTO s_num_serialz (id, name, date_entered, created_by, account_name, type, supply_type, covered_by_support, install_date, job_number, replacement_for) VALUES ('$id', '$serial_num', NOW(), '$diary_id', '$account', '$type', '$supply_type', $support_cover, '$dispatch_date', '$job_num', '$old_serial_num')";
$result = $mysqli->query($sql) or die($mysqli->error.__LINE__);

It's a straight forward insert but if one of the values, say $job_number, is empty the query will fail

Answer Source

Well check if a field is empty after recieving

  //code goes here    

If you are using the query of form

Insert into table (..,..,...) values (...,...,) 

try switching to

Insert into table set column1='', column2=''

And add some code to help us answer because it should work.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download