mike j Posted December 16, 2007 Share Posted December 16, 2007 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 More sharing options...
mike j Posted December 16, 2007 Author Share Posted December 16, 2007 Nevermind.... I figured it out. I had my insert statement in the wrong section of my PHP script. I copied the above code and inserted the 'insert' statement as part of an if clause I already was using.... Mike Link to comment https://forums.phpfreaks.com/topic/81862-if-field-is-null-dont-insert/#findComment-416030 Share on other sites More sharing options...
asmith Posted December 16, 2007 Share Posted December 16, 2007 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 https://forums.phpfreaks.com/topic/81862-if-field-is-null-dont-insert/#findComment-416033 Share on other sites More sharing options...
mike j Posted December 16, 2007 Author Share Posted December 16, 2007 Thanks, Asmith. I had something similar already in my PHP Best, M Link to comment https://forums.phpfreaks.com/topic/81862-if-field-is-null-dont-insert/#findComment-416036 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.