1

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 ?

KondukterCRO
  • 543
  • 2
  • 16
  • 31
  • What is the name of the location column in `prices` table? – Ashish Choudhary Dec 02 '15 at 10:42
  • $sql2 = "DELETE FROM prices WHERE Location2 = $location"; check this – Parth Chavda Dec 02 '15 at 10:46
  • In prices I have Location1 and Location2. I have New York in table prices, column Location2 and I'm deleting New York from locations table. That is locations that I'm sure I have in both tables and that's why I want to delete Location2 from table prices. – KondukterCRO Dec 02 '15 at 11:32
  • This might help: http://stackoverflow.com/questions/4839905/mysql-delete-from-multiple-tables-with-one-query – brandelizer Dec 02 '15 at 11:40
  • Thanks for link. As I can see it's based on delete from multiple databases with same reference but I need it with differente references... – KondukterCRO Dec 02 '15 at 12:11

0 Answers0