代码不会删除多个表中的所有记录

时间:2016-03-30 09:17:24

标签: php mysql

我创建了一个delete.php,我要删除三个名为person,address和cv的表中的记录...通过获取他们的ID我想删除他们的记录,代码只删除人员表中的所有记录和不是来自地址和简历。

tempVals.Add(sf.GetAttributeFieldValues(i)[i2].Trim());

编辑:

我的桌子

l

2 个答案:

答案 0 :(得分:0)

如果我正确理解了您的表结构,您将根据所有3个表中的人员ID删除,您将根据地址ID和cvid从这两个表中删除。

答案 1 :(得分:-1)

根据我从表结构中得到的理解,你应该在地址表和CV表中分别有一个person_id。

这意味着,您应该根据person_id

从地址和CV表中删除
<?php
$servername = "localhost";
$username = "root";
$password = "usbw";
$dbname = "persons";

// CREATE A CONNECTION WITH THE DATABASE
// CONNECTIE MAKEN MET DATABASE
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// GET ID FROM person_id
// PAK ID VAN person_id 
$person_id = (isset($_GET['person_id']) ? $_GET['person_id'] : null);

// CREATE PREPARE STATMENT FOR DELETING RECORDS FROM person_id
// MAAK EEN STATEMENT OM WAARDES TE VERWIJDEREN VAN person_id
$stmt = $conn->prepare('DELETE FROM `person` WHERE person_id = ?'); 
$stmt->bind_param('s', $person_id);                                         

// EXECUTE STATEMENT AND IF RESULT IS FALSE SHOW ERROR
// VOER STATEMENT UIT EN ALS VALS IS GEEF ERROR AAN
$result = $stmt->execute();    
if ($result === FALSE) {
    die("Error: " . $stmt->error);
}

$address_id = (isset($_GET['address_id']) ? $_GET['address_id'] : null);

$stmt = $conn->prepare('DELETE FROM `address` WHERE person_id = ?');



$stmt->bind_param('s', $address_id);                                         

// EXECUTE STATEMENT AND IF RESULT IS FALSE SHOW ERROR
// VOER STATEMENT UIT EN ALS VALS IS GEEF ERROR AAN
$result = $stmt->execute();    
if ($result === FALSE) {
    die("Error: " . $stmt->error);
}

$cv_id = (isset($_GET['cv_id']) ? $_GET['cv_id'] : null);

$stmt = $conn->prepare('DELETE FROM `cv` WHERE person_id = ?'); 

$stmt->bind_param('s', $cv_id);                                         

// EXECUTE STATEMENT AND IF RESULT IS FALSE SHOW ERROR
// VOER STATEMENT UIT EN ALS VALS IS GEEF ERROR AAN
$result = $stmt->execute();    
if ($result === FALSE) {
    die("Error: " . $stmt->error);
}
// AFTER CLICKING DELETE GO TO LINK
// NA HET DRUKKEN VAN DELETE GA NAAR LINK
header("Location: http://localhost:8080/Website/admin.php");

// CLOSE CONNECTION AND STATEMENT
// SLUIT CONNECTIE EN STATEMENT
$stmt->close();
$conn->close();
?>

如果你真的让person_id成为地址和CV表上的外键,那就太棒了。您可能不一定需要为两个表(CV和地址)编写不同的删除