Jump to content

If field is null, don't insert!


mike j

Recommended Posts

Hi PHP coders,

 

I am very new to PHP and am doing my best to figure out something. What I would like to do is insert the form info I've created but only if  the fields are NOT empty. Here's what I have so far:

 

$con = mysql_connect("my.host.com", "user", "pass");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("my_db", $con);

mysql_query("INSERT INTO table_name (full_name, email, age,)
VALUES('$name', '$email', '$age')");

mysql_close($con);

 

And it works! That is to say, the insert works. But it also works if there is nothing in the fields $name, $email and/or $age.

 

Is there a way I can make sure the user is forced to enter something in these three fields before my MySQL database accepts the insert statement?

 

I apologize if this question is really junior! I'm a total newbie.

 

Thank you for your time,

Mike

 

:)

 

Link to comment
https://forums.phpfreaks.com/topic/81862-if-field-is-null-dont-insert/
Share on other sites

btw

for forcing user to type anything at least , you've got nothing to do with mysql.

you must not let him do that using php .

the php code can look like this :

 

if (empty($_POST['name']))

    {

      echo "You must enter your name";

    }

else

    {

      //add it to mysql

    }

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.