Reaper0167 Posted December 21, 2008 Share Posted December 21, 2008 still having a small problem. even with all three fields filled out i am still getting the message "Please enter a username" I'm stumped. oh yeh, how do you get the code to be color coded when you paste it in here for a post. // define variables from form $username = $_POST["username"]; $password = $_POST["password"]; $email = $_POST["email"]; $encrypted_password = md5($password); // inserting data into your database $sql = "INSERT INTO $tbl_name(username, password, email)VALUES('$username','$encrypted_password','$email')"; // display message if field is empty if (empty($username)) { echo "Please enter a username."; } elseif (empty($password)) { echo "Please enter a password."; } elseif (empty($email)) { echo "Please enter a valid email."; } else { $result = mysql_query($sql); // take user back to a page header("location:register.php"); } // closes your connection mysql_close(); ?> Quote Link to comment Share on other sites More sharing options...
asmith Posted December 21, 2008 Share Posted December 21, 2008 try this to see if it helps, intead of using empty : <?php // display message if field is empty if ($username == '') { echo "Please enter a username."; } elseif ($password == '') { echo "Please enter a password."; } elseif ($email == '') { echo "Please enter a valid email."; } else { $result = mysql_query($sql); ?> and you need to put php tags for your code if you want it to be displayed by colors like the way i did. Quote Link to comment Share on other sites More sharing options...
Reaper0167 Posted December 21, 2008 Author Share Posted December 21, 2008 still having the same problem <?php // connects to server and database mysql_connect("$host", "$username", "$password") or die("Could not connect."); mysql_select_db("$db_name") or die("Could not find database"); // define variables from form $username = $_POST["username"]; $password = $_POST["password"]; $email = $_POST["email"]; $encrypted_password = md5($password); // inserting data into your database $sql = "INSERT INTO $tbl_name(username, password, email)VALUES('$username','$encrypted_password','$email')"; // display message if field is empty if ($username == '') { echo "Please enter a username."; } elseif ($password == '') { echo "Please enter a password."; } elseif ($email == '') { echo "Please enter a valid email."; } else { $result = mysql_query($sql); // take user back to a page header("location:register.php"); } // closes your connection mysql_close(); ?> Quote Link to comment Share on other sites More sharing options...
Reaper0167 Posted December 21, 2008 Author Share Posted December 21, 2008 think i just found the prob no, i didn't, sorry Quote Link to comment Share on other sites More sharing options...
redarrow Posted December 21, 2008 Share Posted December 21, 2008 Try separating the conditions. <?php // display message if field is empty if (empty($username)) { echo "Please enter a username."; } if (empty($password)) { echo "Please enter a password."; } if (empty($email)) { echo "Please enter a valid email."; } ?> another way. <?php if( (empty($username)) || (empty($password))|| (empty($email))) { echo "Please enter all fields"; } ?> last one for fun <?php // display message if field is empty if (empty($username)) { $error="Please enter a username."; } if (empty($password)) { $error= "Please enter a password."; } if (empty($email)) { $error= "Please enter a valid email."; } if($error){ echo $error; } ?> there plenty more just ask. Quote Link to comment Share on other sites More sharing options...
redarrow Posted December 21, 2008 Share Posted December 21, 2008 Another way just posted to another user. <?php // display message if field is empty if (empty($username)) { $error="Please enter a username.<br><br> <a href='whatever.com/start_agin.php'>Try agin</s>"; echo $error; exit; } if (empty($password)) { $error= "Please enter a password.<br><br> <a href='whatever.com/start_agin.php'>Try agin</s>"; echo $error; exit; } if (empty($email)) { $error= "Please enter a valid email<br><br> <a href='whatever.com/start_agin.php'>Try agin</s>"; echo $error; exit; } ?> Quote Link to comment Share on other sites More sharing options...
sun55 Posted December 21, 2008 Share Posted December 21, 2008 I took your code only, and made some cosmetic changes ( with respect to your basic requiremnt ) and it works. I say, your code does work. Whats the problem then? <?php $username = "root"; $password = "asdf"; $email = "zxcv"; mysql_connect("localhost", "$username", "$password") or die("Could not connect."); mysql_select_db("test") or die("Could not find database"); $sql = "INSERT INTO users (first_name, last_name, email, pass, registration_date) VALUES('$username','qwerty','$email', '$password', now())"; // display message if field is empty if ($username == '') { echo "Please enter a username."; } elseif ($password == '') { echo "Please enter a password."; } elseif ($email == '') { echo "Please enter a valid email."; } else { $result = mysql_query($sql) or die("Query failed: " . mysql_error()); } ?> Quote Link to comment Share on other sites More sharing options...
blueman378 Posted December 21, 2008 Share Posted December 21, 2008 btw guys you dont need the php tags, you can also do [ code=php ]code[ /code ] anyway is it possible that one of your form elements is named incorrectly? Quote Link to comment Share on other sites More sharing options...
Reaper0167 Posted December 21, 2008 Author Share Posted December 21, 2008 still, nothing works. keeps saying to enter a username even when all fields are filled in. i haven't tried setting it up using an array yet. i will use that as my last resource. this way just seems a little more simple for being a beginner to PHP. <?php // server and database variable information $host = "-----------"; $username = "----------"; $password = "----------"; $db_name = "-----------"; $tbl_name = "------------"; // connects to server and database mysql_connect("$host", "$username", "$password") or die("Could not connect."); mysql_select_db("$db_name") or die("Could not find database"); // define variables from form $username = $_POST["username"]; $password = $_POST["password"]; $email = $_POST["email"]; $encrypted_password = md5($password); // inserting data into your database $sql = "INSERT INTO $tbl_name(username, password, email)VALUES('$username','$encrypted_password','$email')"; // display message if field is empty if ($username == '') { echo "Please enter a username."; } elseif ($password == '') { echo "Please enter a password."; } elseif ($email == '') { echo "Please enter a valid email."; } else { $result = mysql_query($sql); // take user back to a page header("location:register.php"); } // closes your connection mysql_close(); ?> Quote Link to comment Share on other sites More sharing options...
UpcomingPhpDev Posted December 21, 2008 Share Posted December 21, 2008 how about something like if (empty($username)) { echo "Please enter a username."; } elseif (empty($password)) { echo "Please enter a password."; } elseif (empty($email)) { echo "Please enter a valid email."; } else { $result = mysql_query($sql); EDIT - I see redarrow has already posted this, Sorry. Quote Link to comment Share on other sites More sharing options...
redarrow Posted December 21, 2008 Share Posted December 21, 2008 name your submit button to submit. <?php if(isset($_POST['submit'])){ // do it } ?> Quote Link to comment Share on other sites More sharing options...
Reaper0167 Posted December 21, 2008 Author Share Posted December 21, 2008 this is driving me crazy. still, all three fields filled out, and its saying to enter a username <?php // server and database variable information $host = "--------"; $username = "--------"; $password = "---------"; $db_name = "-----------"; $tbl_name = "------------"; // connects to server and database mysql_connect("$host", "$username", "$password") or die("Could not connect."); mysql_select_db("$db_name") or die("Could not find database"); // define variables from form $username = $_POST["username"]; $password = $_POST["password"]; $email = $_POST["email"]; $encrypted_password = md5($password); // inserting data into your database $sql = "INSERT INTO $tbl_name(username, password, email)VALUES('$username','$encrypted_password','$email')"; // display message if field is empty if (empty($username)) { echo "Please enter a username."; } elseif (empty($password)) { echo "Please enter a password."; } elseif (empty($email)) { echo "Please enter a valid email."; } else { $result = mysql_query($sql); // take user back to a page header("location:register.php"); } // closes your connection mysql_close(); ?> Quote Link to comment Share on other sites More sharing options...
redarrow Posted December 21, 2008 Share Posted December 21, 2008 Post your form please we need it to make sure it correct please. Quote Link to comment Share on other sites More sharing options...
UpcomingPhpDev Posted December 21, 2008 Share Posted December 21, 2008 You could try replacing empty for isset. Quote Link to comment Share on other sites More sharing options...
redarrow Posted December 21, 2008 Share Posted December 21, 2008 name your submit button name="submit" know post your form every think in order now. <?php // server and database variable information $host = "--------"; $username = "--------"; $password = "---------"; $db_name = "-----------"; $tbl_name = "------------"; // connects to server and database mysql_connect("$host", "$username", "$password") or die("Could not connect."); mysql_select_db("$db_name") or die("Could not find database"); // define variables from form $username =mysql_real_escape_string($_POST["username"]); $password =mysql_real_escape_string($_POST["password"]); $email = mysql_real_escape_string($_POST["email"]); $encrypted_password = md5($password); // display message if field is empty if (empty($username)) { echo "Please enter a username."; } elseif (empty($password)) { echo "Please enter a password."; } elseif (empty($email)) { echo "Please enter a valid email."; } else{ // take user back to a page header("location:register.php"); } if(isset($_POST['submit'])){ // inserting data into your database $sql = "INSERT INTO $tbl_name(username, password, email)VALUES('$username','$encrypted_password','$email')"; $res=mysql_query($sql) or die (mysql_error()); // closes your connection mysql_close(); } ?> Quote Link to comment Share on other sites More sharing options...
Reaper0167 Posted December 21, 2008 Author Share Posted December 21, 2008 here is the form <?php <style type="text/css"> <!-- .style1 {font-size: 24px} .underline { border-bottom-width: thin; border-bottom-style: solid; border-bottom-color: #666666; } .style2 { color: #0000FF; font-weight: bold; font-style: italic; } .style3 {color: #FF0000} --> </style> <table width="100%" height="28" border="0" cellpadding="0" cellspacing="0" class="underline"> <tr> <td height="26" bgcolor="#CCCCCC"><div align="center"><strong><span class="style1">Justin's Simple Registration Script</span></strong></div></td> </tr> </table> <p align="center"> <label for="username"></label> </p> <form id="register" name="register" method="post" action="register.php"> <table width="100%" border="0" cellspacing="5" cellpadding="0"> <tr> <td width="45%"><div align="right"> <label for="label3">Username</label> </div></td> <td width="55%"><input name="username" type="text" id="label3" size="35" maxlength="20" /></td> </tr> <tr> <td><div align="right">Password</div></td> <td><input name="password" type="password" id="label4" size="35" maxlength="20" /></td> </tr> <tr> <td height="22"><div align="right"> <label for="label4"></label> Email</div></td> <td><input name="email" type="text" id="label5" size="35" maxlength="65" /></td> </tr> <tr> <td height="22"> </td> <td><label for="submit"></label> <input type="submit" name="submit" id="submit" value="Register" /></td> </tr> </table> </form> ?> Quote Link to comment Share on other sites More sharing options...
Reaper0167 Posted December 21, 2008 Author Share Posted December 21, 2008 keeping my form the way it was,,, this worked thanks redarrow <?php // server and database variable information $host = "10.6.186.33"; $username = "mylahstone"; $password = "******"; $db_name = "mylahstone"; $tbl_name = "members"; // connects to server and database mysql_connect("$host", "$username", "$password") or die("Could not connect."); mysql_select_db("$db_name") or die("Could not find database"); // define variables from form $username = mysql_real_escape_string($_POST["username"]); $password = mysql_real_escape_string($_POST["password"]); $email = mysql_real_escape_string($_POST["email"]); $encrypted_password = md5($password); // display message if field is empty if (empty($username)) { echo "Please enter a username."; } elseif (empty($password)) { echo "Please enter a password."; } elseif (empty($email)) { echo "Please enter a valid email."; } else { // take user to a page after registration header("location:thanks.php"); } if (isset($_POST['submit'])) { // inserting data into your database $sql = "INSERT INTO $tbl_name(username, password, email)VALUES('$username','$encrypted_password','$email')"; $res = mysql_query($sql) or die(mysql_error()); // closes your connection mysql_close(); } ?> Quote Link to comment Share on other sites More sharing options...
UpcomingPhpDev Posted December 21, 2008 Share Posted December 21, 2008 Lol keeping my form the way it was,,, this worked thanks redarrow <?php // server and database variable information $host = "10.6.186.33"; $username = "mylahstone"; $password = "******"; $db_name = "mylahstone"; $tbl_name = "members"; // connects to server and database mysql_connect("$host", "$username", "$password") or die("Could not connect."); mysql_select_db("$db_name") or die("Could not find database"); // define variables from form $username = mysql_real_escape_string($_POST["username"]); $password = mysql_real_escape_string($_POST["password"]); $email = mysql_real_escape_string($_POST["email"]); $encrypted_password = md5($password); // display message if field is empty if (empty($username)) { echo "Please enter a username."; } elseif (empty($password)) { echo "Please enter a password."; } elseif (empty($email)) { echo "Please enter a valid email."; } else { // take user to a page after registration header("location:thanks.php"); } if (isset($_POST['submit'])) { // inserting data into your database $sql = "INSERT INTO $tbl_name(username, password, email)VALUES('$username','$encrypted_password','$email')"; $res = mysql_query($sql) or die(mysql_error()); // closes your connection mysql_close(); } ?> Lol, Nice db password (pwd removed...) Quote Link to comment 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.