MySQL删除故障排除

时间:2013-01-03 14:49:31

标签: php mysql

我重新启动了MySQL服务,并尝试使用我的PHP程序删除功能删除现有行,但我发现虽然删除了查询,但行未被删除。我尝试将删除级联应用于子表的外键,但似乎没有效果。我想知道为什么删除什么都不做。

CREATE TABLE `customers` (
  `idcustomers` int(11) NOT NULL AUTO_INCREMENT,
  `firstname` varchar(45) DEFAULT NULL,
  `lastname` varchar(45) DEFAULT NULL,
  `address1` varchar(45) DEFAULT NULL,
  `address2` varchar(45) DEFAULT NULL,
  `city` varchar(45) DEFAULT NULL,
  `state` varchar(45) DEFAULT NULL,
  `zip` varchar(45) DEFAULT NULL,
  `phone` varchar(45) DEFAULT NULL,
  `email` varchar(45) DEFAULT NULL,
  `cell` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`idcustomers`),
  UNIQUE KEY `idcustomers_UNIQUE` (`idcustomers`)
) ENGINE=InnoDB AUTO_INCREMENT=54 DEFAULT CHARSET=latin1


CREATE TABLE `events` (
  `idevents` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(250) DEFAULT NULL,
  `start` datetime DEFAULT NULL,
  `end` datetime DEFAULT NULL,
  `allday` varchar(50) DEFAULT NULL,
  `url` varchar(1000) DEFAULT NULL,
  `customerid` int(11) NOT NULL,
  `memo` longtext,
  `dispatchstatus` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`idevents`),
  KEY `FK_events` (`customerid`),
  CONSTRAINT `FK_events` FOREIGN KEY (`customerid`) REFERENCES `customers` (`idcustomers`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=latin1

Com_delete 2

PHP看起来像这样:

<?php
session_start();

date_default_timezone_set("America/Los_Angeles");

if($_SESSION['loggedin'] != TRUE)
{
header("Location: index.php");
}

require_once('../php.securelogin/include.securelogin.php');
$mysqli = new mysqli($ad_host, $ad_user, $ad_password, "samedaycrm");

if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}


$customerid = $_SESSION['customer_id'];

$tSQL = "delete from events where customerid = \"$customerid\"";
$result = $mysqli->query($tSQL);

$tSQL = "delete from customers where idcustomers = \"$customerid\"";
$result = $mysqli->query($tSQL);



echo $mysqli->error;
?>

1 个答案:

答案 0 :(得分:1)

假设customerididcustomers列都是数字,那么它应该没问题。您不应该在btw中引用这些查询中的变量,然后您不需要转义它们。你可以尝试:

$tSQL = "delete from events where customerid = $customerid";

但它应该与你已经使用的不同。当然,如果您不确定可以使用的列类型:

$tSQL = "delete from events where customerid = '".$customerid."'";

或者你可以逃脱:

$tSQL = "delete from events where customerid = '$customerid'";

但出于某种原因,我一直很讨厌。

如果所有这些都无法通过吐出$customerid(甚至整个$tSQL)变量进行故障排除,然后在phpmyadmin或toad或您使用的任何数据库客户端中手动尝试查询,并查看它告诉你。如果它只是说0 rows affected,那么就像select一样运行它。量身定做。