Click here to Skip to main content
15,898,374 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '','')' at line 1


What I have tried:

!DOCTYPE html>
<html>

<head>
	<title>Insert Page page</title>
</head>

<body>

<?php
// $servername ="localhost";
// $username ="root";
// $passwordname ="";
// $dbname ="college_1";
$conn=mysqli_connect("localhost","root","" ,"college_1");
if($conn)
{
    echo "connection OK";//
}
else
{
   echo "connection Failed".mysqli_connect_error();
}

    $rn = $_GET['name'];
    $em = $_GET['Email'];
    $phn = $_GET['PhoneNumber'];
    $msg = $_GET['Message'];


    echo "$rn";
    echo "$phn";
    echo "$msg";
    echo "$em";


    $sql = "INSERT INTO personal VALUES('$rn','$em',$phn','$msg')";
    // $data = mysqli_query($conn, $query);
    // if ($data) {
    //     echo "data inserted successfully";
    // } else {
    //     echo "data not inserted successfully";
    // }
    // 
    if(mysqli_query($conn, $sql)) {
        echo "data inserted successfully";
    }
    else {
        echo "data not inserted successfully."
        . mysqli_error($conn);
    }
   mysqli_close($conn)

?>
</body>

</html>
Posted
Updated 3-May-21 12:53pm
v2

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 throughout your app, and your problem will go away at the same time ...

And do yourself a favour: always list the columns you are INSERTing into - if you don't then SQL starts with the "first column" and assumes that's where you want your data to go. Since most tables start with an ID column and they are generally IDENTITY or UNIQUEIDENTIFER datatype, the INSERT operation generally fails.
 
Share this answer
 
SQL
$sql = "INSERT INTO personal VALUES('$rn','$em',$phn','$msg')";
                                                ^-----you are missing a quote character here

But more importantly, you should follow the guidance posted above by OriginalGriff.
 
Share this answer
 
To add to other solutions, just few links About SQL Injection and how to avoid.

Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
 
Share this answer
 

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