LostKID Posted September 13, 2009 Share Posted September 13, 2009 Hi everyone im having a bit of a problem i cant seem to connect to my database.. well i cant seem to insert any information to be precise. im ok pulling information back from the database which leads me to believe there may be something wrong with my coding.. could you lend me some time to show me where i have gone wrong? PHP Processing code after registering details. <?php // CONNECT TO THE DATABASE mysql_connect ("localhost", "xxx", "xxx") or die ('Error: ' . mysql_error()); mysql_select_db ("xxx"); // CALL IN VARIABLES $fname = $_POST['fname']; $lname = $_POST['lname']; // THE QUERY $query = "INSERT INTO user (fname,lname) VALUES ('$fname', '$lname')"; // UPDLOAD $result = mysql_query($query) or die ('Error updating database'); if($results){ echo "Account createed Successfully, please login with your details."; } ?> The details are correct it just keeps giving me the error "'Error updating database". The table exists in the database too.. its doing my head in. Quote Link to comment https://forums.phpfreaks.com/topic/174126-solved-somethign-wrong-with-my-code/ Share on other sites More sharing options...
mikesta707 Posted September 13, 2009 Share Posted September 13, 2009 echo the mysql_error() and see what happens Quote Link to comment https://forums.phpfreaks.com/topic/174126-solved-somethign-wrong-with-my-code/#findComment-917861 Share on other sites More sharing options...
peranha Posted September 13, 2009 Share Posted September 13, 2009 Change this // UPDLOAD $result = mysql_query($query) or die ('Error updating database'); to // UPDLOAD $result = mysql_query($query) or die ('Error updating database'.mysql_error()); See if you get any error messages. Quote Link to comment https://forums.phpfreaks.com/topic/174126-solved-somethign-wrong-with-my-code/#findComment-917864 Share on other sites More sharing options...
AviNahum Posted September 13, 2009 Share Posted September 13, 2009 i'm not sure, but try this: <?php // CONNECT TO THE DATABASE mysql_connect ("localhost", "xxx", "xxx") or die ('Error: ' . mysql_error()); mysql_select_db ("xxx"); // CALL IN VARIABLES $fname = $_POST['fname']; $lname = $_POST['lname']; // THE QUERY $query = "INSERT INTO user ('fname' , 'lname') VALUES ($fname, $lname)"; // UPDLOAD $result = mysql_query($query) or die ('Error updating database'); if($results){ echo "Account createed Successfully, please login with your details."; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/174126-solved-somethign-wrong-with-my-code/#findComment-917866 Share on other sites More sharing options...
LostKID Posted September 13, 2009 Author Share Posted September 13, 2009 this is the link btw.. sorry www.brokenbox.co.uk/MY i have added the first 2 fixes will try the 3rd Quote Link to comment https://forums.phpfreaks.com/topic/174126-solved-somethign-wrong-with-my-code/#findComment-917870 Share on other sites More sharing options...
cbolson Posted September 13, 2009 Share Posted September 13, 2009 Are you sure that it isn't inserting into the database? - ie have you checked your tables directly? You have a small error here: if($results){ echo "Account createed Successfully, please login with your details."; } You have defined the mysql_query as $result no $results so your message will not be shown. Chris Quote Link to comment https://forums.phpfreaks.com/topic/174126-solved-somethign-wrong-with-my-code/#findComment-917871 Share on other sites More sharing options...
LostKID Posted September 13, 2009 Author Share Posted September 13, 2009 okay those last 2 fixes done it, its adding data to my database now i can go back to adding the rest of the code back to how it was lol thanks you guys your the best. Quote Link to comment https://forums.phpfreaks.com/topic/174126-solved-somethign-wrong-with-my-code/#findComment-917874 Share on other sites More sharing options...
LostKID Posted September 13, 2009 Author Share Posted September 13, 2009 Hi again, i decided to reopen this thread up as not solved again instead of making another topic to fill up the forum. i been adding the rest of my code. ie validation and what not and my code to see if the email address is already in use doesnt work. any ideas? <?php // CONNECT TO THE DATABASE mysql_connect ("localhost", "xxx", "xxx") or die ('Error: ' . mysql_error()); mysql_select_db ("xx"); // CALL IN VARIABLES $username = $_POST['username']; $password = $_POST['password']; $fname = $_POST['fname']; $lname = $_POST['lname']; $email = $_POST['email']; $gamert = $_POST['gamert']; // VALIDATE if($fname == "" || $lname == ""){ echo("you didnt enter your full name"); exit(); } if($password == ""){ echo("you didnt enter anything for your password, please try again"); exit(); } if($email == ""){ echo("you didnt enter anything for your email, please try again"); exit(); } if(!ereg("^.+@.+\\..+$", $email)){ echo("the email you entered was not valid, please try again"); exit(); } // CHECK IF EMAIL EXISTS $sql = "SELECT email FROM user WHERE email='$_POST[email]'"; $result = mysql_query($sql) or die('couldnt execute query'.mysql_error()); $num = mysql_num_rows($result); if($num == 1){ echo("This email address is already in use, please use another email address"); exit(); } // THE QUERY $query = "INSERT INTO user (username,password,fname,lname,email,gamert) VALUES ('$username','$password','$fname','$lname','$email','$gamert')"; // UPDLOAD $result2 = mysql_query($query) or die ('Error updating database'.mysql_error()); if($result2){ echo "Account createed Successfully, please login with your details. you will now be redirected.."; } ?> <script type="text/javascript"> <!-- setTimeout('Redirect()',2000); function Redirect() { location.href='index.php'; } //--> </script> Quote Link to comment https://forums.phpfreaks.com/topic/174126-solved-somethign-wrong-with-my-code/#findComment-917889 Share on other sites More sharing options...
LostKID Posted September 13, 2009 Author Share Posted September 13, 2009 and yes anyone looking over the code and seeing i havent used mp5 for passwords, i will put that in after i know everything is working okay. its just for my debugging. cos im not very good at any of this lol Quote Link to comment https://forums.phpfreaks.com/topic/174126-solved-somethign-wrong-with-my-code/#findComment-917892 Share on other sites More sharing options...
cbolson Posted September 13, 2009 Share Posted September 13, 2009 You have this: if($num == 1){ That is rather limiting as it only returns true if there is exactly one result, what if the email is in the db more than once? You should use this if($num <>0){ or if($num >0){ Chris Quote Link to comment https://forums.phpfreaks.com/topic/174126-solved-somethign-wrong-with-my-code/#findComment-917896 Share on other sites More sharing options...
LostKID Posted September 13, 2009 Author Share Posted September 13, 2009 You have this: if($num == 1){ That is rather limiting as it only returns true if there is exactly one result, what if the email is in the db more than once? You should use this if($num <>0){ or if($num >0){ Chris ding ding we have a winner... damn cant believe that slipped by me lol i shouldnt cleaned out the database.. thats the reason it wasnt working at all. thanks your right ill use the more than expression. thanks! me so tired the little things get passed me. good job there are great people on this forum to lend a helping hand! Quote Link to comment https://forums.phpfreaks.com/topic/174126-solved-somethign-wrong-with-my-code/#findComment-917897 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.