serverman Posted May 12, 2008 Share Posted May 12, 2008 i want to make this have the first/last name and comment be required to post the message to the sql database ok heres my script for the form <form action="../Beta/leaveacomment/insert.php" method="post"> <h3 align="center">Give us some feedback</h3><table width="250" border="1" align="center"> <tr> <td>Firstname:</td> <td><input name="firstname" type="text" maxlength="15" /></td> </tr> <tr> <td>Lastname:</td> <td><input name="lastname" type="text" maxlength="15" /></td> </tr> <tr> <td>Email : </td> <td><input name="email" type="text" id="email" /></td> </tr> <tr> <td>Comment</td> <td><label> <textarea name="comment" id="comment"></textarea> </label></td> </tr> <tr> <td colspan="2"><input type="reset" name="Reset" value="Reset" /> <input name="submit" type="submit" /> <label></label></td> </tr> </table> </form> here is code for the processer <?php $login = mysql_connect("---","---","---"); if (!$login) { die('Could not connect: ' . mysql_error()); }mysql_select_db("website_stuff", $login);$sql="INSERT INTO comment (FirstName, LastName, Email, Comment) VALUES ('$_POST[firstname]','$_POST[lastname]','$_POST[email]','$_POST[comment]')";if (!mysql_query($sql,$login)) { die('Error: ' . mysql_error()); } echo "thank you for leaving a comment."."<a href='../../home.PHP'>Back to Home</a>";mysql_close($login) ?> Quote Link to comment https://forums.phpfreaks.com/topic/105201-keeping-people-from-leaving-blank-section/ Share on other sites More sharing options...
DarkWater Posted May 12, 2008 Share Posted May 12, 2008 1) Clean up your code. It's REALLY sloppy. Make it nicely indented and stuff. 2) Use if (isset()) {} structures. Quote Link to comment https://forums.phpfreaks.com/topic/105201-keeping-people-from-leaving-blank-section/#findComment-538659 Share on other sites More sharing options...
andrewgarn Posted May 12, 2008 Share Posted May 12, 2008 EDITED Put before the query. You need to set the variables from the post data first by: $username = $_POST['username']; etc, then you can do this: if(!$firstname or !$lastname or !$comment) { echo ("Please complete all fields"); } else { //do sql stuff } And yes as the other person said, put paragraphs in your code, and indent Quote Link to comment https://forums.phpfreaks.com/topic/105201-keeping-people-from-leaving-blank-section/#findComment-538663 Share on other sites More sharing options...
serverman Posted May 12, 2008 Author Share Posted May 12, 2008 Put before the query. if(!firstname or !lastname or !comment) { echo ("Please complete all fields"); } else { //do sql stuff } And yes as the other person said, put paragraphs in your code, and indent sweet thank... that was an easy one... i'm really new to this stuff thanks for the help Quote Link to comment https://forums.phpfreaks.com/topic/105201-keeping-people-from-leaving-blank-section/#findComment-538664 Share on other sites More sharing options...
DarkWater Posted May 12, 2008 Share Posted May 12, 2008 No. That's not good enough. Use if (!empty($_POST['firstname']) && !empty($_POST['lastname']) && !empty($_POST['comments'])) { echo "Please fill in first name, last name, and comments!'; } else { //do query } And use '' on associative array indexes. I.E: $_POST['element'] instead of $_POST[element]. Quote Link to comment https://forums.phpfreaks.com/topic/105201-keeping-people-from-leaving-blank-section/#findComment-538665 Share on other sites More sharing options...
serverman Posted May 12, 2008 Author Share Posted May 12, 2008 ok heres what i did $firstname = $_POST['firstname']; $comment = $_POST['comment']; if(!$firstname or !$comment) { die("Please complete all fields"); } it works... but is that a correct way to do it? Quote Link to comment https://forums.phpfreaks.com/topic/105201-keeping-people-from-leaving-blank-section/#findComment-538682 Share on other sites More sharing options...
DarkWater Posted May 12, 2008 Share Posted May 12, 2008 if (empty($firstname) || empty($comments)) { } Quote Link to comment https://forums.phpfreaks.com/topic/105201-keeping-people-from-leaving-blank-section/#findComment-538684 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.