使用一个查询从2个mysql表中删除

时间:2015-12-02 10:40:30

标签: php mysqli delete-row

我做了一个名为location的表。我在其中存储位置。我做了另一张叫价格的桌子。我根据地点的输入将价格存储在ti中。因此,如果我将纽约,巴黎和柏林添加到位置表,我可以选择将这些位置存储在价格表中,并为每个位置赋予价值。只有存储在表位置的位置才能被赋值并存储到价格中。我列出了位置表中存储的所有位置。在每个位置旁边,我有删除按钮。我将唯一ID位置传递给delete.php并根据ID删除整行。现在我想根据ID找到该位置,并从价格表中删除它。

这是删除代码:

//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();

但是它从位置删除位置,并且它会抛出错误的价格: 删除位置时出错:'where子句'中的未知列'我要删除的位置'。

任何帮助?

0 个答案:

没有答案