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
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

    }

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.