如何使用带有连接表的mysqli删除查询

时间:2016-01-09 05:02:15

标签: php mysql mysqli

我做错了什么? 为什么我无法删除结果? 我的语法如下 Select语句就像魅力一样,但出于某种原因,当我想删除特定ID时,它不会做任何事情。只说错误.. 知道我做错了什么,如果可以,请指出我正确的方向?谢谢

require_once 'includes/mysqli_connect.php';

if(isset($_GET['id']) && is_numeric($_GET['id'])) { 

$sql = "SELECT n.entry, FROM `customers` AS c INNER JOIN `notes` as n ON c.user_id = n.user_id WHERE c.user_id={$_GET['id']}";

$result_db = $dbc->query($sql) or die ("Error!");

if ($result_db->num_rows > 0) {

    while($row = $result_db->fetch_assoc()) {

echo    '<form action="select_delete.php" method="post">

' . $row['registration_date'] . '<br/>' . $row['entry'] .'
<input type="hidden" name="id" value="' . $_GET['id'] . '"/>
<input id="click" type="submit" name="submit" value="Delete this Entry" /></form>';
}
} else{
echo 'Information was not retrieved';
}

要删除的

} elseif(isset($_POST['id']) && is_numeric($_POST['id'])) {

$results = $dbc->query("DELETE `customers`, `notes` FROM `customers` INNER JOIN `notes` ON `notes`.entry = `notes`.entry WHERE `customers`.user_id={$_POST['id']}";

if($results){
echo 'This entry has been deleted!';
} else {
'Error : ('. $mysqli->errno .') '. $mysqli->error;
echo 'NOT DELETED';
}
} else {
echo 'Page accessed in error.. Login if administrator.';
}

$dbc->close();

1 个答案:

答案 0 :(得分:1)

在delete语句中使用JOIN时,我们需要指定要删除的内容,以便

使用 客户。,注意。

最终查询:

"DELETE `customers`, `notes` FROM `customers` INNER JOIN `notes` ON `notes`.entry = `notes`.entry WHERE `customers`.user_id='".$_POST['id']."'"