使用一个具有不同条件的SQL查询从多个表中删除

时间:2017-05-12 14:33:58

标签: php mysql

我使用多个表存储了一些产品来插入不同的元素(一个表用于标题和描述,另一个表用于图像等)。 我知道如何通过名为$ codrif的变量使用内连接一起选择所有内容,并且它可以工作。

但是现在我必须在每个产品中添加一个删除选项,并使用相同的变量$ codrif删除三个表中的所有记录。

我尝试了这段代码,但它无效:

产品页面:`

    ...
  echo '<td><a href="delete.php?codrif=' . $row['codrif'] . '">Delete</a></td>';
    ...

delete.php

    <?php


// check if the 'codrif' variable is set in URL, and check that it is valid
 if (isset($_GET['codrif']))
 {
 // get codrif value
 $codrif = $_GET['codrif'];


$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

// sql to delete a record
$sql = "DELETE FROM singleimageupload, uploads, exposition
 USING exposition
    INNER JOIN singleimageupload ON singleimageupload.codrif = exposition.codrif
    INNER JOIN uploads ON uploads.codrif = exposition.codrif
    WHERE exposition.codrif=$codrif";

if ($conn->query($sql) === TRUE) {
    echo "Record deleted successfully";
} else {
    echo "Error deleting record: " . $conn->error;
}

 }

$conn->close();
?>

有什么建议吗?

0 个答案:

没有答案