简体   繁体   中英

How to delete a query using mysqli with join tables

What am I doing wrong? Why am I unable to delete the results? My syntax below The Select statement works like a charm but for some reason when I want to delete the specific ID it does not do anything. Only says error.. Any idea what am I doing wrong and if you can please point me in the right direction? thank you

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';
}

QUERY TO DELETE

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

while using JOIN in delete statement we need to specify what to delete so

Use customers. , notes.

Final Query:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM