Skip to content
Advertisement

Ignore Empty Inputs when inserting data using a PHP form

I seem to be having an issue with PHP Contact Form. In my form, the only field that is NOT required to be filled up, is the Phone Number. When I fill out the form with the phone number, the data gets inserted into the database, but when I leave that input blank, it doesn’t work. It shows me the “success” page but no data is inserted. Is there anyway for the cf.inc.php to ignore the Phone Number field if its empty?

I have added the HTML Form, cf.inc.php and the table structure below.

HTML FORM

JavaScript

cf.inc.php

JavaScript

Table Structure

JavaScript

Advertisement

Answer

Don’t store the phone number as INT. Even though we call it a “number”, it’s not really one, and converting it to an integer can cause problems. In this case, the problem is that converting an empty string to an integer may be causing an error. Also, phone numbers are often written with punctuation and spaces; converting to an integer will only parse up to the punctuation and ignore the rest.

You should declare it as VARCHAR.

You also should check whether mysqli_stmt_execute() succeeds. Error checking would have told you why the INSERT was failing.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement