if(isset($_POST['delete'])){
$mysqli->query("DELETE FROM cdata WHERE VALUES
('John', 'London'),
('Monica', 'Manchester'),
('Tina', 'Delhi'),
('Mona', 'Boston'),
('Simpos', 'New York'),
('Alexandra', 'Las Vegas'),
('Alexander', 'Berlin'),
('Winston', 'London'),
('Franklin', 'Boston'),
('Abraham', 'Paris'); ") or
die($mysqli->error);
$_SESSION['message'] = "Record has been deleted";
$_SESSION['msg_type'] = "danger";
header("location: index.php");
}
Please help me to correct the syntax error:
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 '‘John’, ‘London’), (‘Monica’, ‘Manchester’), (‘Tina’, ‘Delhi’), (‘Mona’ at line 2
$mysqli->query("DELETE FROM cdata WHERE ( name , location ) IN
('John', 'London'),
('Monica', 'Manchester'),
('Tina', 'Delhi'),
('Mona', 'Boston'),
('Simpos', 'New York'),
('Alexandra', 'Las Vegas'),
('Alexander', 'Berlin'),
('Winston', 'London'),
('Franklin', 'Boston'),
('Abraham', 'Paris'); ") or
die($mysqli->error);
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 ’ (‘Monica’, ‘Manchester’), (‘Tina’, ‘Delhi’), (‘Mona’, ‘Boston’), (’ at line 2
He’s given you the most compact format he can give you without knowing every piece of data in the table, or without using multiple queries.
For example; if John is the only person who’s been to london, you can delete his row by deleting WHERE location = “London”
But from the information you have provided (a very specific set of rows based on two column conditions each), that is the most compact delete statement you can make that will delete all of those rows in one query.
Deleting records based on specific criteria is perfectly valid. Or perhaps the combination of name and location IS the unique ID. You don’t HAVE to have a unique identifier identity field to have normalized data.
But as the OP was looking for the most compact query, a query that DELETE’d WHERE id IN(201,202,…) is considerably more compact that the offering that had to be made based upon a tuple of non-key columns.
ID’s will keep on changing as the sample data once inserted can be deleted one by one, and next time when sample data will be inserted will have different ID’s.