christofurr Posted June 23, 2007 Share Posted June 23, 2007 Ok. Been a bit over a month since I started working on this form. I've had at least a dozen problems since day one. Well, here's another for ya. I'm trying to make a script that grabs filtered data from the form and inserts it into the database. The form and the validation work fine, but my database recieves no new information when the form is submitted. I have one bit of PHP script in between the HTML <head> tags. It's this script that's supposed to send the form input to the database. It's also supposed to echo an HTML redirect after the data is sent to the database. There's another bit of script between the <body> tags that echoes the form itself, and all of the error messages if the form is submitted with improper input. All you'll really have to look at is the first bit. I included the rest just in case you need to understand the workings of the form. The code: <html> <head> <?php /* Form Input Variables */ $nicktrim = trim($_POST["nickname"]); $nam1trim = trim($_POST["name1"]); $nam2trim = trim($_POST["name2"]); $pastrim = trim($_POST["pw"]); $vpastrim = trim($_POST["vpw"]); $recqtrim = trim($_POST["recq"]); $now = date("l, F j Y \a\t g:i A T"); /* MySQL Connection Variables */ $hostdbu = "000000"; $userdbu = "111111"; $passdbu = "222222"; $sqldbu = mysql_connect($hostdbu,$userdbu,$passdbu); /* This bit defines certain variables depending on the type of improper input, if any */ if (!empty($_POST)) { if (empty($_POST["name1"])) {$missA = "1A";} else {if (ereg('[^a-zA-Z ]', $nam1trim)) {$missA = "1B";} } if (empty($_POST["name2"])) {$missB = "2A";} else {if (ereg('[^a-zA-Z ]', $nam2trim)) {$missB = "2B";} elseif (strlen($nam2trim)<3) {$missB = "2C";} } if (empty($_POST["nickname"])) {$missC = "3A";} else {if (ereg('[^a-zA-Z0-9 ]', $nicktrim)) {$missC = "3B";} elseif (strlen($nicktrim)<3) {$missC = "3C";} } if (empty($_POST["pw"]) && empty($_POST["vpw"])) {$missD = "4A";} else {if (ereg('[^a-zA-Z0-9]', $pastrim)) {$missD = "4B";} elseif (strlen($pastrim)<6 && !empty($_POST["pw"])) {$missD = "4C";} elseif ($pastrim != $vpastrim && !empty($_POST["pw"]) && !empty($_POST["vpw"])) {$missD = "4E";} } if (!empty($_POST["pw"]) && empty($_POST["vpw"])) {$missH = "8A";} if (!empty($_POST["vpw"]) && empty($_POST["pw"])) {$missI = "9A";} if (empty($_POST["email"])) {$missE = "5A";} else {if (!filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL)) {$missE = "5B";} } if (empty($_POST["recq"])) {$missF = "6A";} if (empty($_POST["reca"])) {$missG = "7A";} /* This bit is supposed to execute a database connection, data insert, and connection close, and redirect, all if there is no inproper input */ if ( $missA != "1A" && $missA != "1B" && $missB != "2A" && $missB != "2B" && $missB != "2C" && $missC != "3A" && $missC != "3B" && $missC != "3C" && $missD != "4A" && $missD != "4B" && $missD != "4C" && $missD != "4E" && $missE != "5A" && $missE != "5B" && $missF != "6A" && $missG != "7A" && $missH != "8A" && $missI != "9A" ) { mysql_connect ($hostdbu, $userdbu, $passdbu); mysql_select_db($userdbu, $sqldbu); mysql_query("INSERT INTO table (First_Name, Last_Name, Email_Address, Nickname, Password, Date_Joined, Secret_Question, Secret_Answer) VALUES ('$nam1trim', '$nam2trim', '$_POST[email]', '$nicktrim', '$pastrim', '$now', '$recqtrim')"); mysql_close ($sqldbu); echo "<meta http-equiv=\"refresh\" content=\"0;url=http://www.froobscape.com/regcomp.htm\">"; } } ?> </head> <body> <div> <div> <?php $nicktrim = trim($_POST["nickname"]); $nam1trim = trim($_POST["name1"]); $nam2trim = trim($_POST["name2"]); $pastrim = trim($_POST["pw"]); $vpastrim = trim($_POST["vpw"]); $recqtrim = trim($_POST["recq"]); if (!empty($_POST)) { if (empty($_POST["name1"])) {$missA = "1A";} else {if (ereg('[^a-zA-Z ]', $nam1trim)) {$missA = "1B";} } if (empty($_POST["name2"])) {$missB = "2A";} else {if (ereg('[^a-zA-Z ]', $nam2trim)) {$missB = "2B";} elseif (strlen($nam2trim)<3) {$missB = "2C";} } if (empty($_POST["nickname"])) {$missC = "3A";} else {if (ereg('[^a-zA-Z0-9 ]', $nicktrim)) {$missC = "3B";} elseif (strlen($nicktrim)<3) {$missC = "3C";} } if (empty($_POST["pw"]) && empty($_POST["vpw"])) {$missD = "4A";} else {if (ereg('[^a-zA-Z0-9]', $pastrim)) {$missD = "4B";} elseif (strlen($pastrim)<6 && !empty($_POST["pw"])) {$missD = "4C";} elseif ($pastrim != $vpastrim && !empty($_POST["pw"]) && !empty($_POST["vpw"])) {$missD = "4E";} } if (!empty($_POST["pw"]) && empty($_POST["vpw"])) {$missH = "8A";} if (!empty($_POST["vpw"]) && empty($_POST["pw"])) {$missI = "9A";} if (empty($_POST["email"])) {$missE = "5A";} else {if (!filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL)) {$missE = "5B";} } if (empty($_POST["recq"])) {$missF = "6A";} if (empty($_POST["reca"])) {$missG = "7A";} } echo " <p> <table border=\"0\"> <tr> <td colspan=\"3\" align=\"center\"><h4>Register</h4></td> </tr> <tr> <td><span>First<br />Name</span></td> <td><form action=\"form13.php5\" method=\"post\"> <input type=\"text\" name=\"name1\" value=\"" . $nam1trim . "\" size=\"35\" maxlength=\"20\" /><br /> </td> <td rowspan=\"2\">Max of 20 letters. If you have a freakishly long name, please abbreviate or... just give us a fake one, but make it short. </td> </tr> <tr> <td></td> <td>"; if ($missA == "1A") {echo "<span>You forgot to tell us your first name.</span><br />"; } if ($missA == "1B") {echo "<span>Your name can only consist of letters and spaces. Please check your input.</span><br />"; } echo " </td> </tr> <tr> <td><span class=\"norm\">Last<br />Name</span></td> <td><input type=\"text\" name=\"name2\" value=\"" . $nam2trim . "\" size=\"35\" maxlength=\"20\" /><br /> </td> <td rowspan=\"2\">Same as above. =P</td> </tr> <tr> <td></td> <td>"; if ($missB == "2A") {echo "<span>You forgot to tell us your last name.</span><br />"; } if ($missB == "2B") {echo "<span>Your name can only consist of letters and spaces. Please check your input.</span><br />"; } echo " </td> </tr> <tr> <td><span>Desired<br />Nickname</span></td> <td><input type=\"text\" name=\"nickname\" value=\"" . $nicktrim . "\" size=\"35\" maxlength=\"32\" /><br /> </td> <td rowspan=\"2\">This is who you'll be known as around here so get creative. You're limited to 32 letters, numbers, and spaces. Nothing else.</td> </tr> <tr> <td></td> <td>"; if ($missC == "3A") {echo "<span>You forgot to tell us what you want your nickname to be.</span><br />"; } if ($missC == "3B") {echo "<span>Your nickname can only consist of alphanumeric characters (A-Z & 0-9) and spaces. Please check your input.</span><br />"; } if ($missC == "3C") {echo "<span>Your nickname must consist of at least three characters.</span><br />"; } echo " </td> </tr> <tr> <td><span class=\"norm\">Password</span></td> <td><input type=\"password\" name=\"pw\" size=\"35\" maxlength=\"20\" /><br /> </td> <td rowspan=\"2\">20 letters and numbers max, case insensitive.</td> </tr> <tr> <td></td> <td>"; if ($missD == "4A") {echo "<span>You forgot to tell us what you want your password to be.</span><br />"; } if ($missD == "4B") {echo "<span>Your password can only consist of alphanumeric characters (A-Z & 0-9). Please check your input.</span><br />"; } if ($missD == "4C") {echo "<span>Your password must consist of at least six characters.</span><br />"; } if ($missD == "4E") {echo "<span>The two password inputs did not match. Please retype your password in both fields.</span><br />"; } if ($missH == "8A" || $missI == "9A") {echo "<span>*</span><br />"; } echo " </td> </tr> <td><span>Verify<br />Password</span></td> <td><input type=\"password\" name=\"vpw\" size=\"35\" maxlength=\"20\" /><br /> </td> <td rowspan=\"2\">Retype the password just to be sure you're not making any typos... b'cause it happens.</td> </tr> <tr> <td></td> <td>"; if ($missD == "4E" || $missD == "4A" || $missD == "4B" || $missD == "4C") {echo "<span>*</span><br />"; } if ($missH == "8A") {echo "<span>You forgot to verify your password. Please retype your password in both fields.</span><br />"; } if ($missI == "9A") {echo "<span>You tried to verify a blank password field. Please retype your password in both fields.</span><br />"; } echo " </td> </tr> <tr> <td><span>Email<br />Address</span></td> <td><input type=\"text\" name=\"email\" value=\"" . $_POST["email"] . "\" size=\"35\" maxlength=\"50\" /><br /> </td> <td rowspan=\"2\">So we can email you if we have to and to verify this registration. Max of 50 characters.</td> </tr> <tr> <td></td> <td>"; if ($missE == "5A") {echo "<span>You forgot to tell us your email address.</span><br />"; } if ($missE == "5B") {echo "<span>The email address <b>" . $_POST["email"] . "</b> does not exist. Please check your input.</span><br />"; } echo " </td> </tr> <tr> <td><span>Recovery<br />Question</span></td> <td><input type=\"text\" name=\"recq\" value=\"" . $recqtrim . "\" size=\"35\" maxlength=\"75\" /><br /> </td> <td rowspan=\"2\">Tell us what to ask you to make sure it's you who's trying to recover your password (if you ever need to). Language and grammer doesn't matter as long as you can recognize your own question. Max of 75 characters.</td> </tr> <tr> <td></td> <td>"; if ($missF == "6A") {echo "<span>You forgot to tell us what you want your recovery question to be.</span><br />"; } echo " </td> </tr> <tr> <td><span>Recovery<br />Answer</span></td> <td><input type=\"text\" name=\"reca\" value=\"" . $_POST["reca"] . "\" size=\"35\" maxlength=\"30\" /><br /> </td> <td rowspan=\"2\"><b>Beware of prying eyes as you fill this one out.</b> Tell us the answer to your secret question and remember what it is <b>EXACTLY!</b></td> </tr> <tr> <td></td> <td>"; if ($missG == "7A") {echo "<span>You forgot to tell us what you want your recovery answer to be.</span><br />"; } echo " </td> </tr> <tr> <td colspan=\"3\" align=\"center\"> <input type=\"submit\" value=\"Create Account\" /> </form> </td> </tr> </table> </p>"; ?> </div> </div> </body> </html> Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted June 23, 2007 Share Posted June 23, 2007 Have you tried catching the error? change this: mysql_query("INSERT INTO table (First_Name, Last_Name, Email_Address, Nickname, Password, Date_Joined, Secret_Question, Secret_Answer) VALUES ('$nam1trim', '$nam2trim', '$_POST[email]', '$nicktrim', '$pastrim', '$now', '$recqtrim')"); To this: <?php mysql_query("INSERT INTO table (First_Name, Last_Name, Email_Address, Nickname, Password, Date_Joined, Secret_Question, Secret_Answer) VALUES ('$nam1trim', '$nam2trim', '$_POST[email]', '$nicktrim', '$pastrim', '$now', '$recqtrim')")or die(mysql_error()); ?> Quote Link to comment Share on other sites More sharing options...
christofurr Posted June 23, 2007 Author Share Posted June 23, 2007 No Database Selected ??? <?php mysql_select_db($userdbu, $sqldbu); ?> Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted June 23, 2007 Share Posted June 23, 2007 Why are you connecting to the database twice? http://www.php.net/mysql_select_db Quote Link to comment Share on other sites More sharing options...
christofurr Posted June 23, 2007 Author Share Posted June 23, 2007 I'm connecting twice? ??? Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted June 23, 2007 Share Posted June 23, 2007 Your connecting on line 20, and line 95. Quote Link to comment Share on other sites More sharing options...
christofurr Posted June 23, 2007 Author Share Posted June 23, 2007 I'm connecting with this? $sqldbu = mysql_connect($hostdbu,$userdbu,$passdbu); I thought I was just defining the $sqldbu variable. Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted June 23, 2007 Share Posted June 23, 2007 Yes, I am pretty sure that connects to the database, even though it's assigned to a variable. Check out this tutorial: http://www.php-mysql-tutorial.com/connect-to-mysql-using-php.php Quote Link to comment Share on other sites More sharing options...
christofurr Posted June 23, 2007 Author Share Posted June 23, 2007 So would this be the only error, the one causing the problem? Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted June 23, 2007 Share Posted June 23, 2007 No, it is saying you have not selected a database....From your code, it looks like the database you are trying to select is called "111111"...although I am sure that is just a place filler. Make sure thats the correct database name. Just follow the tutorial I just gave you, and you should be just fine. If you get a new error, post it here and we can deal with that one. Quote Link to comment Share on other sites More sharing options...
christofurr Posted June 23, 2007 Author Share Posted June 23, 2007 Removed all variables, including $sqldbu and rewrote the connection with the actual host, user, password, and database name. Still getting the No Database Selected error. <?php { mysql_connect ('db936.xxx.xxx','dboxxxxxxxxx','xxxxxxxxxx'); mysql_select_db('dboxxxxxxxxxx'); mysql_query("INSERT INTO table (First_Name, Last_Name, Email_Address, Nickname, Password, Date_Joined, Secret_Question, Secret_Answer) VALUES ('$nam1trim', '$nam2trim', '$_POST[email]', '$nicktrim', '$pastrim', '$now', '$recqtrim')")or die(mysql_error()); mysql_close (); } ?> Quote Link to comment Share on other sites More sharing options...
christofurr Posted June 23, 2007 Author Share Posted June 23, 2007 Same error with: <?php $hostdbu = "db936.xxx.xxx"; $userdbu = "dboxxxxxxxxxxx"; $passdbu = "xxxxxxxxxxx"; $sqldbu = mysql_connect($hostdbu, $userdbu, $passdbu) or die(mysql_error()); mysql_select_db($userdbu); mysql_query("INSERT INTO Froobs (First_Name, Last_Name, Email_Address, Nickname, Password, Date_Joined, Secret_Question, Secret_Answer) VALUES ('$nam1trim', '$nam2trim', '$_POST[email]', '$nicktrim', '$pastrim', '$now', '$recqtrim')")or die(mysql_error()); mysql_close($sqldbu); ?> Quote Link to comment Share on other sites More sharing options...
mmarif4u Posted June 23, 2007 Share Posted June 23, 2007 Same error with: <?php $hostdbu = "db936.xxx.xxx"; $userdbu = "dboxxxxxxxxxxx"; $passdbu = "xxxxxxxxxxx"; $sqldbu = mysql_connect($hostdbu, $userdbu, $passdbu) or die(mysql_error()); mysql_select_db($userdbu); mysql_query("INSERT INTO Froobs (First_Name, Last_Name, Email_Address, Nickname, Password, Date_Joined, Secret_Question, Secret_Answer) VALUES ('$nam1trim', '$nam2trim', '$_POST[email]', '$nicktrim', '$pastrim', '$now', '$recqtrim')")or die(mysql_error()); mysql_close($sqldbu); ?> $sqldbu = mysql_connect($hostdbu, $userdbu, $passdbu) or die(mysql_error()); mysql_select_db($userdbu); Look at the above lines carefully which i cut from ur query. R u sure that ur select db is same name as ur username. Quote Link to comment Share on other sites More sharing options...
christofurr Posted June 23, 2007 Author Share Posted June 23, 2007 I thought they were. I checked again with my host and realized that they differ by a single letter. Editing. Quote Link to comment Share on other sites More sharing options...
christofurr Posted June 23, 2007 Author Share Posted June 23, 2007 That fixed it. Thanks, all. Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted June 23, 2007 Share Posted June 23, 2007 Your query is not balanced. You say you are inserting 8 things, yet you only have 7 variables. <?php (First_Name, Last_Name, Email_Address, Nickname, Password, Date_Joined, Secret_Question, Secret_Answer) VALUES ('$nam1trim', '$nam2trim', '$_POST[email]', '$nicktrim', '$pastrim', '$now', '$recqtrim') ?> I think you are either missing the Secret_Question variable, or the Secret_Answer variable. Quote Link to comment Share on other sites More sharing options...
christofurr Posted June 23, 2007 Author Share Posted June 23, 2007 Yeah, thanks. Also, realized that I had more information to account for during the insert. All done. Quote Link to comment Share on other sites More sharing options...
mmarif4u Posted June 23, 2007 Share Posted June 23, 2007 Glad to hear from u that it is fixed. mark the topic solved. Quote Link to comment Share on other sites More sharing options...
christofurr Posted June 23, 2007 Author Share Posted June 23, 2007 Uh... Already have. 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.