phpnewbphp Posted January 11, 2012 Share Posted January 11, 2012 Hi, can anyone help me I'm new to coding and have been having trouble getting my code to insert into the database. I have checked the connection and that works fine but it says you're registered but it dosen't input it into the database. This is the code i have in my script: <?php echo "<h1>Register</h1>"; $submit = $_POST ['submit']; //form data $fullname = strip_tags($_POST ['fullname']); $username = strip_tags($_POST ['username']); $password = strip_tags($_POST ['password']); $repeatpassword = strip_tags($_POST ['repeatpassword']); $date = date ("Y-m-d"); if ($submit) { //check for existance if ($fullname&&$username&&$password&&$repeatpassword) { //check passwords match if ($password==$repeatpassword) { //check char length of username and fullname if (strlen($username)>25||strlen($fullname)>25) { echo "Maximum limit for username and fullname is 25 characters!"; } else { //check password length if (strlen($password)>25||strlen($password)<6) { echo "Your password must be between 6 and 25 characters!"; } else { //register the user //encrypt password $password = md5 ($password); $repeatpassword = md5 ($repeatpassword); //open database $connect = mysql_connect ("localhost","","") or die ("couldn't connect!"); mysql_select_db ("phplogin") or die ("couldn't find db!"); mysql_query ("INSERT INTO users VALUES ('','$fullname','$username','$password','$date')"); die ("You've been registered! <a href='index.php'>Click here</a> to return to login page"); } } } else echo "Your passwords do not match!"; } else echo "Please fill in <b>all</b> fields."; } ?> <html> <form action='register.php' method='POST'> <table> <tr> <td>Your Full name:</td> <td><input type='text' name='fullname' value='<?php echo $fullname ?>'></td> </tr> <tr> <td>Your Username:</td> <td><input type='text' name='username' value='<?php echo $username ?>'></td> </tr> <tr> <td>Your Password:</td> <td><input type='password' name='password'></td> </tr> <tr> <td>Repeat your password:</td> <td><input type='password' name='repeatpassword'></td> </tr> </table> <input type='submit' name='submit' value='Register'> </form> </html> Quote Link to comment https://forums.phpfreaks.com/topic/254823-help-with-insert-query/ Share on other sites More sharing options...
phpnewbphp Posted January 11, 2012 Author Share Posted January 11, 2012 Forgot to add it's hosted using MS IIS web server and I am testing using IE8 and Firefox 9 Hi, can anyone help me I'm new to coding and have been having trouble getting my code to insert into the database. I have checked the connection and that works fine but it says you're registered but it dosen't input it into the database. This is the code i have in my script: <?php echo "<h1>Register</h1>"; $submit = $_POST ['submit']; //form data $fullname = strip_tags($_POST ['fullname']); $username = strip_tags($_POST ['username']); $password = strip_tags($_POST ['password']); $repeatpassword = strip_tags($_POST ['repeatpassword']); $date = date ("Y-m-d"); if ($submit) { //check for existance if ($fullname&&$username&&$password&&$repeatpassword) { //check passwords match if ($password==$repeatpassword) { //check char length of username and fullname if (strlen($username)>25||strlen($fullname)>25) { echo "Maximum limit for username and fullname is 25 characters!"; } else { //check password length if (strlen($password)>25||strlen($password)<6) { echo "Your password must be between 6 and 25 characters!"; } else { //register the user //encrypt password $password = md5 ($password); $repeatpassword = md5 ($repeatpassword); //open database $connect = mysql_connect ("localhost","","") or die ("couldn't connect!"); mysql_select_db ("phplogin") or die ("couldn't find db!"); mysql_query ("INSERT INTO users VALUES ('','$fullname','$username','$password','$date')"); die ("You've been registered! <a href='index.php'>Click here</a> to return to login page"); } } } else echo "Your passwords do not match!"; } else echo "Please fill in <b>all</b> fields."; } ?> <html> <form action='register.php' method='POST'> <table> <tr> <td>Your Full name:</td> <td><input type='text' name='fullname' value='<?php echo $fullname ?>'></td> </tr> <tr> <td>Your Username:</td> <td><input type='text' name='username' value='<?php echo $username ?>'></td> </tr> <tr> <td>Your Password:</td> <td><input type='password' name='password'></td> </tr> <tr> <td>Repeat your password:</td> <td><input type='password' name='repeatpassword'></td> </tr> </table> <input type='submit' name='submit' value='Register'> </form> </html> Quote Link to comment https://forums.phpfreaks.com/topic/254823-help-with-insert-query/#findComment-1306642 Share on other sites More sharing options...
cartaysm Posted January 11, 2012 Share Posted January 11, 2012 I could be wrong but shouldn't this; $connect = mysql_connect ("localhost","","") or die ("couldn't connect!"); mysql_select_db ("phplogin") or die ("couldn't find db!"); read more like this; $connect = mysql_connect ("localhost","","") or die ("couldn't connect!"); mysql_select_db ("phplogin", $connect) or die ("couldn't find db!"); Quote Link to comment https://forums.phpfreaks.com/topic/254823-help-with-insert-query/#findComment-1306674 Share on other sites More sharing options...
laffin Posted January 11, 2012 Share Posted January 11, 2012 My bet is on the query itself mysql_query ("INSERT INTO users VALUES ('','$fullname','$username','$password','$date')"); try using the fieldnames mysql_query ("INSERT INTO users (fullname,username,password,addedon) VALUES ('$fullname','$username','$password','$date')"); of course your fieldnames will be different Quote Link to comment https://forums.phpfreaks.com/topic/254823-help-with-insert-query/#findComment-1306675 Share on other sites More sharing options...
phpnewbphp Posted January 12, 2012 Author Share Posted January 12, 2012 Thank you but this didn't do anything different. I could be wrong but shouldn't this; $connect = mysql_connect ("localhost","","") or die ("couldn't connect!"); mysql_select_db ("phplogin") or die ("couldn't find db!"); read more like this; $connect = mysql_connect ("localhost","","") or die ("couldn't connect!"); mysql_select_db ("phplogin", $connect) or die ("couldn't find db!"); Quote Link to comment https://forums.phpfreaks.com/topic/254823-help-with-insert-query/#findComment-1307045 Share on other sites More sharing options...
phpnewbphp Posted January 12, 2012 Author Share Posted January 12, 2012 Hi I had the same problem that i would say registered but not show in the database. My bet is on the query itself mysql_query ("INSERT INTO users VALUES ('','$fullname','$username','$password','$date')"); try using the fieldnames mysql_query ("INSERT INTO users (fullname,username,password,addedon) VALUES ('$fullname','$username','$password','$date')"); of course your fieldnames will be different Quote Link to comment https://forums.phpfreaks.com/topic/254823-help-with-insert-query/#findComment-1307047 Share on other sites More sharing options...
mikosiko Posted January 12, 2012 Share Posted January 12, 2012 I didn't read all your code... post it enclosed in tags in the future (using the # symbol in the editor) for clarity for the rest of us. what have you done to debug your code? a basic step to debug is at least echo your queries and or variables and see if they contain what they are supposed to... per example your code: mysql_query ("INSERT INTO users VALUES ('','$fullname','$username','$password','$date')"); will be much better for debugging purposes to write it in this way: $query = "INSERT INTO users VALUES ('','$fullname','$username','$password','$date')"; // and then echo your raw query and look for errors echo "Query is : " . $query . "<br />"; // and at least use a basic mechanism to trap possibles errors mysql_query( $query) or die('Query Error : ' . mysql_error()); and also will help your debugging if, while in development you enable the error reporting and display; either globally (in php.ini) or locally in each script using this 2 lines at the beginning of your scripts. error_reporting(E_ALL); ini_set("display_errors", 1); Quote Link to comment https://forums.phpfreaks.com/topic/254823-help-with-insert-query/#findComment-1307049 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.