Click here to Skip to main content
15,303,572 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Unable to import the db_user dump:
Unable to restore database 'db_user'
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '

This is the error coming when I'm uploading the my database in a big rock website.

Here is the code in which I'm getting error:

PHP
<?php

    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "db_user";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);

    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 

    //only insert if all data valid!
    if(isset($_POST['fname']) && isset($_POST['lname']) && isset($_POST['number']) && isset($_POST['address']) && isset($_POST['email']) && isset($_POST['password'])) {
            $fname = $_POST['fname'];
            $lname = $_POST['lname'];
            $number = $_POST['number'];
            $address = $_POST['address'];
            $email = $_POST['email'];   
            $password = $_POST['password']; 

        $sql = "INSERT INTO registration (fname ,lname, number, address, email, password)
        VALUES (\"$fname\", \"$lname\", \"$number\", \"$address\",  \"$email\", \"$password\")";

        if ($conn->query($sql) === TRUE) {
            echo "New record created successfully";
        } else {
            echo "Error: " . $sql . $conn->error;
        }
    } else {
        echo "Error: data not valid";
    }

    $conn->close();
?>

Please help me. I'm not understanding what I have done wrong.

What I have tried:

I have changed the servername, but the same error shows up.
Posted
Updated 28-Jan-22 2:05am
v3
Comments
Richard Deeming 28-Jan-22 5:39am
   
In addition to the critical SQL Injection vulnerability in your code, you are also storing your users' passwords insecurely.
Secure Password Authentication Explained Simply[^]
Salted Password Hashing - Doing it Right[^]

PHP even has built-in functions to help you do the right thing:
PHP: password_hash[^]
PHP: password_verify[^]

1 solution

Don;t do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

Fix that through your whole app, and the problem you have noticed will disappear at the same time.

Why? Because SQL doesn't use double quotes as string delimiters - it uses single quotes, and parameterised queries don't need either ... as well as being a lot sefer!
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900