I made table called location. I store locations in it. I made another table called prices. I store prices in ti based on inputs of locations. So if I add New York, Paris and Berlin to locations table I have option to store these location in prices table and give each of them value. Only locations stored in table locations can be assigned value and stored into prices. I made list of all locations stored in location table. Next to each location I have delete button. I passed unique ID of location to delete.php and remove entire row based on ID. Now I want to find that location based on ID and delete it from prices table, too.
Here is delete code:
//request id from main page
$ID =$_REQUEST['ID'];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//find exact name of location passed by id
$select = $conn->query("SELECT Location FROM locations WHERE ID=$ID");
$location = $select->fetch_object()->Location;
// sql to delete a record
// delete from location based on id
$sql = "DELETE FROM locations WHERE ID=$ID";
// delete from prices based on location name
$sql2 = "DELETE FROM prices WHERE Location2 = $location";
if ($conn->query($sql) === TRUE) {
echo "Location deleted successfully";
} else {
echo "Error deleting location: " . $conn->error;
}
if ($conn->query($sql2) === TRUE) {
echo "Price deleted successfully";
} else {
echo "Error deleting location: " . $conn->error;
}
$conn->close();
But it deletes location from locations and for prices it throws error: Error deleting location: Unknown column 'Location which I want to delete' in 'where clause'.
Any help ?