0

I was creating my website so I tried registering users for my website. I created MySQL database and wrote who codes and query. I then first tried to register a sample user, It was successful, then I tried one more but it was not successful. I didn't changed any query or something. My code was as it is but I don't know what happened.

My PHP code in registered.php

$connection = mysql_connect("localhost","root","accessdenied")
  or die ("Couldn't connect to server");

$db = mysql_select_db("hdnesslife",$connection)
  or die ("Couldn't select database");

$fname =$_REQUEST["fname"];
$lname =$_REQUEST['lname'];
$username =$_REQUEST['username'];
$email =$_REQUEST['email'];
$password =$_REQUEST['password'];


$order = "INSERT INTO users(fname,lname,username,email, password)VALUES('$fname','$lname','$username','$email','$password')";
$result = mysql_query($order);

if($result){
echo("<br>Success");
}
else {
    echo("<br>No Success");
}

mysql_close();
?>

My HTML form code in register.php

<form action="registered.php" method="post">
    First Name<input required="required" type="text" name="fname" id="fname">
    <br>
    Last Name<input required="required" type="text" name="lname" id="lname">
    <br>
    Username<input required="required" type="text" name="username" id="username">
    <br>
    Email Id<input required="required" type="text" name="email" id="email">
    <br>
    Password<input required="required" type="password" name="password" id="password" value="password">
    <br>
    <input type="submit" name="submit" value="register">
</form>

Every time I try to register a new user, It says

"No Success"

It seems that the $order query is wrong but I have checked many times. I have checked the code many times and it seems correct I don't know what's the problem it, Please help me.

Rawnawk
  • 1
  • 1
  • What does your table look like ?? – mokk Apr 03 '13 at 16:28
  • You should print `$order` after you set it to have your exact sql query. Then run it in phpmyadmin, it will tell you what is wrong in your query – mokk Apr 03 '13 at 16:31
  • As your starting with this I suggest you read this : http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php to take a good start – DessDess Apr 03 '13 at 16:38
  • Actually, your code is, on the face of it, fine. But it's incredibly vulnerable, so you should definitely take steps to protect it. – Strawberry Apr 03 '13 at 16:44

2 Answers2

0

It is much easier to find problems if you see a proper error message. Either use a debugger (recommended) or put debug statements to your code. You could for example do

echo mysql_error($connection);

right after echo "<br>No success";.

mysql_error will print out the last error that happened on a specific connection, or if no identifier is supplied the last opened connection. Read more at the PHP manual.

Warning: The mysql extension is deprecated. While you have the chance, you should switch library to for example mysqli. Read more at here: Choosing an [MySQL] API.

Community
  • 1
  • 1
Henrik Karlsson
  • 5,559
  • 4
  • 25
  • 42
0

It would help if you outputted the error from MySQL, it will tell you what's wrong!

Change the line:

$result = mysql_query($order);

To:

$result = mysql_query($order) or die(mysql_error());
Brian Kerr
  • 411
  • 3
  • 14