Quantcast
Channel: User andrewsi - Stack Overflow
Viewing all articles
Browse latest Browse all 51

Answer by andrewsi for Getting an error of SQl Syntax

$
0
0
$sql = mysqli_query($conn,"INSERT INTO forgot(userid,email,mobile)VALUES ('$userid','$email','$mobile')");

if (mysqli_query($conn,$sql)) 
{
    echo "We will Contact you Soon.<br>";
} 

You've got two calls here to mysqli_query. The first time, you're making the query and assigning the return value to $sql; the second time, you're running $sql as a query.

To fix the immediate problem, do something along the lines of:

$sql = "INSERT INTO forgot(userid,email,mobile)VALUES ('$userid','$email','$mobile')";

if (mysqli_query($conn,$sql)) 
{
    echo "We will Contact you Soon.<br>";
} 

You're assigning your query to a string, and then using that in your query. This makes debugging things easier, as you can now output your generated query to check what you're producing.

However

You're also passing user-generated data directly into an SQL query, without escaping it. This is very bad - at best, you're going to have a problem if some of the data contains apostrophes. At worst, your database will get hacked. One solution here is to use escaping, as Fred suggested, using mysqli_real_escape_string:

$userid = mysqli_real_escape_string($conn, $_POST['userid']);
$email  = mysqli_real_escape_string($conn, $_POST['email']);
$mobile = mysqli_real_escape_string($conn, $_POST['mobile']);

I'd suggest also looking at using bound parameters and a prepared statement instead, for added extra security.



Viewing all articles
Browse latest Browse all 51

Trending Articles