2

I have a database called message, with a table called user in it. The table, 'user' has five columns, user_id which is the primary key and auto increments. Then is the first name last name username and password. But when i press the submit button, it shows me error connecting. Cannot find the error!

What could it be?

<!DOCTYPE html>
<html>
<head>
<script src="jquery.js"></script>
<title> Create Account
</title>

</head>

<body>
<?php

$dbc=mysqli_connect('localhost', 'root', '', 'message') or die("Error");
if(isset($_POST['submit']))
{

    if($_POST['password']==$_POST['repassword'])
    {
        $fname=$_POST['fname'];
        $lname=$_POST['lname'];
        $username=$_POST['username'];
        $password=$_POST['password'];
        $password_hash=sha1($password);
        $query="INSERT INTO user (fname, lname, username, password)"."VALUES('$fname', '$lname', '$username', '$password_hash')";
        $result=mysqli_query($dbc, $query) or die("Error Connecting");
        echo "Account Created!";
        mysqli_close($dbc);


    }
    else
    {
        echo "Passwords do not match";
    }
}

?>
6
  • Have you selected table user via mysqli_select_db? I couldn't see it ya! Commented Sep 11, 2013 at 8:57
  • 1
    Not directly related to the problem, but this is very misleading for users reporting bugs and you debugging/asking for help as well: If mysqli_query() fails you should report it as "Query failed" or "Couldn't create account". It should never state "Error Connecting", because at that point in time you're either connected already or it failed above ("Errror"). Commented Sep 11, 2013 at 8:58
  • 1
    Make sure your database server is running (and if it is using default port), try using mysqli_connect_error() to see if there is something more informative. Src: php.net/manual/en/mysqli.construct.php Commented Sep 11, 2013 at 9:02
  • 1
    try print_r($result); die(); to see what returns Commented Sep 11, 2013 at 9:03
  • put the insert query inside try catch statment to see whats the real php exception ... i think that will give u the specific problem Commented Sep 11, 2013 at 9:04

2 Answers 2

1

You need to add "" around the fields you are about to insert.

You presently have:

$query="INSERT INTO user (fname, lname, username, password)"."VALUES('$fname', '$lname', '$username', '$password_hash')";

Do this instead:

$query='INSERT INTO user (fname, lname, username, password) '.
   'VALUES("'.$fname.'", "'.$lname.'", "'.$username.'", "'.$password_hash.'")';
Sign up to request clarification or add additional context in comments.

5 Comments

They are interpolated in nested quotes
Better yet, use backticks and there is no need for "."VALUES remove the "." and backtick".$fname."+backtick, and for debug, echo $query, copy paste the query in your phpMyAdmin and see what error it gives you.
It worked thanks! I just need to clear my firm after the account is created! How can I do that?
gr8! Your firm ..? Dont quite follow.
Clear the form** Sorry!
1

change your insert query to

$query="INSERT INTO user (fname, lname, username, password) VALUES('".$fname."', '".$lname."', '".$username."', '".$password_hash."')";

1 Comment

Thanks it worked! I would like to know why do we have to write in this format? Cause I guess the earlier one was correct as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.