how to post an input empty into the database?

Dear experts!
I have some 8 input text fields in my signup form, on which i wanted to hav 6 fields required and 2 fields optional (i.e) Phone number and Email address.

When i am done with the form filling and submit, it shows error! It seems without filling these 2 fields, the post cant be made successfully. I want this last two fields be optional for the user, that he may or may not input in that. Please help me with this… :frowning: :frowning: :frowning: :frowning: ???

My form



My signup script


<?php include_once("db.php") ?> <?php $firstname = $_POST['firstname']; $lastname = $_POST['lastname']; $rollno = $_POST['roll']; $username = $_POST['name']; $password = $_POST['pass']; $date = $_POST['date']; $phone = $_POST['phone']; $email = $_POST['email']; $sql = "INSERT into login (firstname, lastname, rollno, username, password, date, phone, email) values('".$firstname."','".$lastname."',".$rollno.",'".$username."','".$password."','".$date."',".$phone.",'".$email."')"; # $sql = "INSERT into login values('".$firstname."','".$lastname."',".$rollno.",'".$username."','".$password."','".$date."',".$phone.",'".$email."')"; $qury = mysql_query($sql); if(!$qury){ header("location:ad_new_user_err.php?");} else{ header("location:ad_new_user_successful.php?");} ?>


You didn’t happen to put a +, a - or any other non-numeric sign (including spaces!) in your phone number by any chance, did you?

No… no… i din not… i just input only the numbers…

I didn’t see it at first: the name date is a reserved keyword. Change your SQL query to this to get around it:
[php]$sql = “INSERT into login (firstname, lastname, rollno, username, password, date, phone, email)

(While you’re at it, you have eight SQL injection vectors there. Please look up how to sanitize your data - or see your site crash and burn the moment someone with bad intentions finds it)

Hey Seb!
i tried… but it din work out. my prob is i wanted to post my last to fields empty… when posting that empty, it shows err. inputing date is working perfect… correct me if i am wrong… i am a beginner only…

I may be wrong with what I’m understanding but if you are asking how to remove the last two fields from being required to have data in them then that would be the “required” tags needing to be removed from those last two input fields on the input form. Has nothing to do with your string in your processing page. Depending on how you made your form, if you did your html from scratch then you should know where the tags are and what you used. If you used something like Dreamweaver that you highlight the input filed and select spry validation then you need to remove that validation.

Dear vegas!
Previously i tried removing required tag from th form. It din work. Thought there might be some other thing i am missing, thats why i am asking… :frowning:

The required tag in your html form is what prevents the form being submitted if the field is empty, nothing else, so you need to make sure the required tag is removed from that input field. I myself would remove all required tags and see what happens, if the form submits without any input then add the required tag back to the fields you want it on and try it again, if you still have the same issue repost the current html of the form.

I actually tried your form out on my local server and it worked fine for me, I was only required to input data in the first 6 fields, phone number and email did not require me to input anything!

i tried as you said, but still the prob continues :frowning: did you check my php script? is that ok? why does it not work with me? will there be any others prob that stops me to post cuz my fields are empty?

If there’s nothing in the text box then it fails because it’s NULL, and you get an “Undefined Index” warning.

You need to check that a $_POST or $_GET contain something before continuing and assign a default value if they don’t, something like this:

[php]if (isset($_POST[‘firstname’])){
$firstname = $_POST[‘firstname’];
$firstname = “”;

Alternatively you can use a terniary:, like this:

[php]$firstname = isset($_POST[‘firstname’]) ? $_POST[‘firstname’] : “”;[/php]

Xerxes! i tried your suggestion… the prob still remains… and now i used mysql_error function to see for the error… it says
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘’’)’ at line 2

OK, I think the problem might be the phone number, which looks to be a numeric field. Does this field accept NULLS?

Also, it’s usual to store phone numbers as text, so that people can space them etc., in addition you rarely search or sort by phone number.

we search our customer database with phone numbers all the time and I set the phone numbers row up as INT (10) and it works fine.

OK, so first check that the field in the DB accepts NULL values, then try this:

$phone = isset($_POST[‘phone’]) ? $_POST[‘phone’] : NULL;

Sponsor our Newsletter | Privacy Policy | Terms of Service