938660 Posted October 17, 2007 Share Posted October 17, 2007 Below is my code for a 'user registration' form where the user enters information into the form and the data gets stored in the database. My connection info is correct etc....below is the code. Anything wrong? The problem is, the data doesnt store into the database!! if ($_SESSION['logged_in']==1){ echo "You are already logged in!"; // Check that the form was submitted and check the user } else if (isset($_POST['submit'])){ // do user database checking $title=$_POST["title"]; $firstname=$_POST["firstname"]; $lastname=$_POST["lastname"]; $address1=$_POST["address1"]; $address2=$_POST["address2"]; $city=$_POST["city"]; $postcode=$_POST["postcode"]; $country=$_POST["country"]; $email=$_POST["email"]; $username=$_POST["username"]; $password=$_POST["password"]; $sql = "SELECT * FROM users WHERE username = '$username'"; // Database checks $result = mysql_query($sql) or die(mysql_error()); $num=mysql_num_rows($result); if ($num){ $errors .="That username is already in use<Br>";} if (!$password){ $errors .="You must input a password!<Br>";} if (!$email){ $errors .="You must provide an email address<br>";} if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) { $errors .= "Please provide us with a valid email address<br>";} if ($errors){ echo "<b>The following errors occurred:</b><br><br>"; echo $errors; } else { // insert a new user to the database $sql = "INSERT INTO users (id, title, firstname, lastname, address1, address2, city, postcode, country, email, username, password) VALUES ('', '$title', '$firstname', '$lastname', '$address1', '$address2', '$city', '$postcode', '$country', '$email', '$username', '$password' )"; $result = mysql_query($sql); echo "You have now signed up. <a href = \"login.php\">Please login</a>"; } } else { ?> </p> <form method="post" action="<? echo $PHP_SELF?>"> <h3>Enter Details Below</h3> <fieldset> <label> <span><b>Title:</b></span> <span class="price"><input name="title" type="text" id="title" size="24" /> </span> <br><br> <span><b>First Name:</b></span> <span class="price"><input name="firstname" type="text" id="firstname" size="24" /> </span> <br><br> <span><b>Last Name:</b></span> <span class="price"><input name="lastname" type="text" id="lastname" size="24" /> </span> <br><br> <span><b>Address Line 1:</b></span> <span class="price"><input name="address1" type="text" id="address1" size="24" /> </span> <br><br> <span><b>Address Line 2:</b></span> <span class="price"><input name="address2" type="text" id="address2" size="24" /> </span> <br><br> <span><b>City/Town:</b></span> <span class="price"><input name="city" type="text" id="city" size="24" /> </span> <br><br> <span><b>Post Code:</b></span> <span class="price"><input name="postcode" type="text" id="postcode" size="24" /> </span> <br><br> <span><b>Country:</b></span> <span class="price"><input name="country" type="text" id="country" size="24" /> </span> <br><br> <span><b>Email:</b></span> <span class="price"> <input name="email" type="text" id="email" size="24" /> </span> <br><br><br><br><br> <span><b><h3>Login Details</b></h3></span> <br><br><br><br><br> <span><b>Username:</b></span> <span class="price"><input name="username" type="text" id="username" size="24" /> </span> <br><br><br> <span><b>Password:</b></span> <span class="price"><input name="password" type="password" id="password" size="24" /> </span> <br><br><br><br> <input name="submit" type="submit" id="submit" value="Register" /> </fieldset> </form> <? } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/73699-solved-hi-please-help-its-quite-urgent/ Share on other sites More sharing options...
kernelgpf Posted October 17, 2007 Share Posted October 17, 2007 What error are you getting? What's the problem with the script? Quote Link to comment https://forums.phpfreaks.com/topic/73699-solved-hi-please-help-its-quite-urgent/#findComment-371852 Share on other sites More sharing options...
938660 Posted October 17, 2007 Author Share Posted October 17, 2007 oh dear y does no1 help me EVER!....maybe its my username :S Quote Link to comment https://forums.phpfreaks.com/topic/73699-solved-hi-please-help-its-quite-urgent/#findComment-371854 Share on other sites More sharing options...
SirChick Posted October 17, 2007 Share Posted October 17, 2007 its not your username its the fact that your code is so long and you don't even put it into tags to make our job easier..... surround your entire script in: tags so its easier for us to read. Quote Link to comment https://forums.phpfreaks.com/topic/73699-solved-hi-please-help-its-quite-urgent/#findComment-371873 Share on other sites More sharing options...
SirChick Posted October 17, 2007 Share Posted October 17, 2007 ('', '$title', '$firstname', '$lastname', '$address1', '$address2', '$city', '$postcode', '$country', '$email', '$username', '$password' )"; notice u have at the start (", and at the end you have )"; it should be starting like ('$title', and the end should just be ); like so ('$title', '$firstname', '$lastname', '$address1', '$address2', '$city', '$postcode', '$country', '$email', '$username', '$password'); on a side note i would add string escape on the $_POST to avoid problems with hackers breaking into your form submission. Quote Link to comment https://forums.phpfreaks.com/topic/73699-solved-hi-please-help-its-quite-urgent/#findComment-371875 Share on other sites More sharing options...
corbin Posted October 17, 2007 Share Posted October 17, 2007 ''$title' That will syntax error (MySQL side, not PHP). The syntax was valid before, although I personally would just leave the ID field out of the insert command entirely. Try $result = mysql_query($sql) or die(mysql_error()); That way if mysql is erroring, it'll tell you the error. Quote Link to comment https://forums.phpfreaks.com/topic/73699-solved-hi-please-help-its-quite-urgent/#findComment-371881 Share on other sites More sharing options...
938660 Posted October 17, 2007 Author Share Posted October 17, 2007 Thankyou very much corbin. Your advice to add that piece of code to show error helped me solve the problem. The problem was very basic - the fields i specified in the script to store data to, didnt exist ie i had 'first_name' in table yet told it to store to a field called 'firstname'. much appreciated Quote Link to comment https://forums.phpfreaks.com/topic/73699-solved-hi-please-help-its-quite-urgent/#findComment-371915 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.