Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
ERROR: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 'AND user.password=''' at line 1

The error is in the line
$query = mysqli_query($conn,"SELECT * FROM user WHERE user.mobileno=$mobile AND user.password='$pwd'") or die("ERROR:".mysqli_error($conn));

My code:
<?php include "db.php" ?>
<?php
if(isset($_POST['submit'])){
    $mobile=$_POST["mno"];
    $pwd=$_POST["password"];
    $query = mysqli_query($conn,"SELECT * FROM user WHERE user.mobileno=$mobile 
    AND user.password='$pwd'") or die("ERROR".mysqli_error($conn));
}              


What I have tried:

I have tried a lot of advice online but couldn't resolve this error.
Posted
Updated 9-Jan-21 19:57pm

Quote:

Replace the $query with
PHP
$query = mysqli_query($conn,"select * FROM user WHERE user.mobileno='$mobile' AND user.password='$pwd' ") or die("ERROR:".mysqli_error($conn));

No! 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?
 
Share this answer
 
Comments
Deepak R Ksheerasagar 10-Jan-21 2:37am    
Thank you kind stranger for your advice.
No, I do not take backups. This is part of a mini-project for my university.
So I will keep your advice in my mind if I take up any real-world project.
OriginalGriff 10-Jan-21 3:16am    
No, do it now, and get used to doing it - it's a lot easier to establish a good habit than it is to break a bad one.

And ... your best mate would try that with your code just to see your face. And you'd probably try it on his to see his reaction when he realizes you just deleted his DB by typing a username!

Always use parameters: it's simple to do, makes the code readable, and protects you from "accidents".
Deepak R Ksheerasagar 10-Jan-21 3:35am    
You've got a point, so guess I have stuff to redo, and I'm sure it'll be worth it.
Thank you again.
OriginalGriff 10-Jan-21 3:38am    
You're welcome!
Sorry for the naive question, I'm a dumbwit apparently.
You have to enclose the
$mobile
in the $query within ''.
Replace the $query with
$query = mysqli_query($conn,"select * FROM user WHERE user.mobileno='$mobile' AND user.password='$pwd' ") or die("ERROR:".mysqli_error($conn));
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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