删除按钮不删除其他表中的值

时间:2016-05-09 13:19:55

标签: php mysql

我的SQL代码遇到了一些麻烦......我创建了一个删除按钮,所以当person_id被删除时,它也应该删除表地址和cv中的值,但是当我点击删除时它只删除了人的值而不是来自地址和简历。 是的我在这段代码中知道ON DELETE ACTION是NO ACTION但是我在sql脚本中添加了ON DELETE CASCADE!

我的SQL:

-- MySQL Script generated by MySQL Workbench
-- 05/09/16 15:12:48
-- Model: New Model    Version: 1.0
-- MySQL Workbench Forward Engineering

SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';

-- -----------------------------------------------------
-- Schema persons
-- -----------------------------------------------------

-- -----------------------------------------------------
-- Schema persons
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `persons` DEFAULT CHARACTER SET utf8 ;
USE `persons` ;

-- -----------------------------------------------------
-- Table `persons`.`address`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `persons`.`address` (
`address_id` INT(11) NOT NULL AUTO_INCREMENT,
`address_street` VARCHAR(45) NULL,
`address_housenumber` VARCHAR(4) NULL,
`address_zipcode` VARCHAR(6) NULL,
`address_city` VARCHAR(45) NULL,
`address_state` VARCHAR(45) NULL,
 PRIMARY KEY (`address_id`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `persons`.`cv`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `persons`.`cv` (
`cv_id` INT NOT NULL AUTO_INCREMENT,
`cv_name` VARCHAR(255) NULL,
`cv_path` VARCHAR(255) NULL,
`cv_type` VARCHAR(255) NULL,
 PRIMARY KEY (`cv_id`))
ENGINE = InnoDB;


 -- -----------------------------------------------------
 -- Table `persons`.`person`
 -- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `persons`.`person` (
`person_id` INT(11) NOT NULL AUTO_INCREMENT,
`person_firstname` VARCHAR(45) NULL,
`person_lastname` VARCHAR(45) NULL,
`person_email` VARCHAR(45) NULL,
`person_phonenumber` INT(10) NULL,
`person_cv` INT(11) NULL,
`person_address` INT(11) NULL,
 PRIMARY KEY (`person_id`),
 INDEX `address_id_idx` (`person_address` ASC),
 INDEX `cv_id_idx` (`person_cv` ASC),
 CONSTRAINT `address_id`
 FOREIGN KEY (`person_address`)
 REFERENCES `persons`.`address` (`address_id`)
 ON DELETE NO ACTION
 ON UPDATE NO ACTION,
 CONSTRAINT `cv_id`
 FOREIGN KEY (`person_cv`)
 REFERENCES `persons`.`cv` (`cv_id`)
 ON DELETE NO ACTION
 ON UPDATE NO ACTION)
 ENGINE = InnoDB;


SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;

我的Delete.php:我添加了address_id和cv_id以便删除它仍然无效!

<?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 address_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 cv_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();
?>

2 个答案:

答案 0 :(得分:0)

如果没有FOREIGN KEY定义,很难看出错误的位置。我试图复制你的设置,并且可以保证这是有效的:

-- --------------------------------------------------------
-- Versione server:              5.5.49-0ubuntu0.14.04.1 - (Ubuntu)
-- S.O. server:                  debian-linux-gnu
-- --------------------------------------------------------

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET NAMES utf8mb4 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;

CREATE TABLE IF NOT EXISTS `cv` (
  `person_id` int(11) DEFAULT NULL,
  `cv_text` varchar(32) DEFAULT NULL,
  KEY `person_id` (`person_id`),
  CONSTRAINT `FK_cv_persons` FOREIGN KEY (`person_id`) REFERENCES `persons` (`person_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `persons` (
  `person_id` int(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`person_id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

/*!40101 SET SQL_MODE=IFNULL(@OLD_SQL_MODE, '') */;
/*!40014 SET FOREIGN_KEY_CHECKS=IF(@OLD_FOREIGN_KEY_CHECKS IS NULL, 1, @OLD_FOREIGN_KEY_CHECKS) */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;

- 现在我插入一些数据:

INSERT INTO `persons` (`person_id`) VALUES
    (1),(2);

- 和链接的cvs:

INSERT INTO `cv` (`person_id`, `cv_text`) VALUES
    (1, 'Rossi'),(2, 'Verdi'),(2, 'Verdini');

- 确认数据在那里:

SELECT * FROM cv;

- 从人中删除一些数据

DELETE FROM persons WHERE person_id = 2;

- 确认数据已从

中删除
SELECT * FROM cv;

...只返回一条记录--Rossi's,id 1. Id 2已经消失。

答案 1 :(得分:0)

我可以在你的表创建代码中看到FOREIGN_KEY_CHECKS的一些编辑。我看到您备份旧值,以便重置它,但是您确定在执行删除时,FOREIGN_KEY_CHECKS值是1吗?