【发布时间】:2017-06-12 21:21:40
【问题描述】:
尝试删除项目时出现以下错误。
致命错误:未捕获的 PDOException:SQLSTATE[42S02]:基表或 未找到视图:1146 表 'databaseName.tableName' 不存在 /storage/h1/735/500735/public_html/delete.php:21 堆栈跟踪:#0 /storage/h1/735/500735/public_html/delete.php(21): PDOStatement->execute() #1 {main} 抛出 /storage/h1/735/500735/public_html/delete.php 在第 21 行
注意:tableName 这里是tblcart
文件删除.php
<?php
session_start();
require("dbconfig.php");
if (isset($_GET['delete_id'])) {
$stmt_select = $DB_con->prepare('SELECT * FROM tblcart WHERE productID =:id');
$stmt_select->execute(array(':id'=>$_GET['delete_id']));
$result=$stmt_select->fetch(PDO::FETCH_ASSOC);
$stmt_delete = $DB_con->prepare('DELETE FROM tblCart WHERE productID =:id AND userID =:userID');
$stmt_delete->bindParam(':id', $_GET['delete_id']);
$stmt_delete->bindParam(':userID', $_SESSION['userid']);
$stmt_delete->execute();
header("Location: cart.php");
}
?>
请参阅错误说明。错误发生在$stmt_delete->execute(); 行。如果我通过本地 PC localhost 运行代码,它运行良好,但在 Web 托管服务器上运行时,我收到此错误。
文件 dbconfig.php
<?php
$DB_HOST = 'localhost';
$DB_USER = 'root';
$DB_PASS = 'password';
$DB_NAME = 'databaseName';
try{
$DB_con = new PDO("mysql:host={$DB_HOST};dbname={$DB_NAME}",$DB_USER,$DB_PASS);
$DB_con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e){
echo $e->getMessage();
}
?>
【问题讨论】:
-
Table 'databaseName.tableName' doesn't exist什么不清楚? -
看看
dbconfig.php中的代码可能会有用 -
注意:如果您需要
productID和userID以使删除唯一,那么您可能还需要userID在SELECT中,否则您可能会选择多行通过 SELECT 并删除随机行 -
我假设您可以在代码中的某处找到
tableName,因为它是错误中缺少的表格,而您显示的代码仅参考tblcart。也许你应该从修复它开始。 -
@fpietka
tableName指的是tblcart。所以错误说明应该是databaseName.tblcart,但我故意将databaseName.tblcart替换为databaseName.tableName