Wednesday, 21 August 2013

How to insert data into db via form and left some form fields empty?

How to insert data into db via form and left some form fields empty?

I want to enable users to insert only information they want to insert into
table, and some form fields to stay empty (if user doesn't have need to
fill it). But when I try to insert only some data in db table "catalog" in
the following way:
if (!empty($_POST['name'])){ $name = $_POST['name'];}
if (!empty($_POST['city'])){ $city = $_POST['city'];}
if (!empty($_POST['country'])){ $country= $_POST['country'];}
if (!empty($_POST['year'])){ $year = $_POST['year'];}
$sql = "INSERT INTO
catalog(name,city,country,year)VALUES('$name','$city','$country','$year')";
mysql_query($sql);
it returns me error: undefined variable. Of course, it is the same
variable that take it's value from content in html form field I lefted
empty.
For example if I almost complete html form for inserting data in db, and
left only "city" field empty, script returns me error letting me know that
my $city variable is not defined. In other words, how to make fields
emptiness unnecessary for successful inserting data?
Thanks!

No comments:

Post a Comment