Jump to content

[SOLVED] Hi - Please help its quite urgent


938660

Recommended Posts

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>

 

Link to comment
https://forums.phpfreaks.com/topic/73699-solved-hi-please-help-its-quite-urgent/
Share on other sites

('', '$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.

''$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.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.