如何修复发送到MySQL的查询?PHP中的错误
我已经设置了代码和数据库查询。我认为PDO连接到MySQL的一切都是正确的,但是记录没有进入数据库 //包括开始 //联系如何修复发送到MySQL的查询?PHP中的错误,php,mysql,Php,Mysql,我已经设置了代码和数据库查询。我认为PDO连接到MySQL的一切都是正确的,但是记录没有进入数据库 //包括开始 //联系 <?php $DSN = 'mysql:host = localhost; dbname = cms4.2.1'; $ConnectingDB = new PDO($DSN, 'root', ''); ?> //重定向功能 <?php function Redirect_to($New_Location) {
<?php
$DSN = 'mysql:host = localhost; dbname = cms4.2.1';
$ConnectingDB = new PDO($DSN, 'root', '');
?>
//重定向功能
<?php
function Redirect_to($New_Location) {
header("Location:".$New_Location);
exit;
}
?>
//错误处理
<?php
session_start();
function ErrorMessage(){
if (isset($_SESSION["ErrorMessage"])){
$Output = "<div class=\"alert alert-danger\">";
$Output .= htmlentities($_SESSION["ErrorMessage"]);
$Output .= "</div>";
$_SESSION["ErrorMessage"] = null;
return $Output;
}
}
function SuccessMessage() {
if (isset($_SESSION["SuccessMessage"])) {
$Output = "<div class=\"alert alert-success\">";
$Output .= htmlentities($_SESSION["SuccessMessage"]);
$Output .= "</div>";
$_SESSION["SuccessMessage"] = null;
return $Output;
}
}
?>
//包括结束
//代码
<?php require_once("Includes/DB.php"); ?>
<?php require_once("Includes/Functions.php"); ?>
<?php require_once("Includes/Sessions.php"); ?>
<?php
if (isset($_POST["Submit"])) {
$Category = $_POST["CategoryTitle"];
$Admin = "Abdullah";
date_default_timezone_set("Asia/Riyadh");
$CurrentTime = time();
$DateTime = strftime("%B-%d-%Y %H:%M:%S", $CurrentTime);
if (empty($Category)) {
$_SESSION["ErrorMessage"] = "All fields must be filled out";
Redirect_to("Categories.php");
} else if (strlen($Category) < 3) {
$_SESSION["ErrorMessage"] = "Category is short";
Redirect_to("Categories.php");
} else if (strlen($Category) > 49) {
$_SESSION["ErrorMessage"] = "Category is too long";
Redirect_to("Categories.php");
} else {
// Insert to database
// The problem starts here I think
global $ConnectingDB;
$sql = "INSERT INTO category(title,author,datetime)";
$sql .= "VALUES(:categoryName,:adminName,:dateTime)";
$stmt = $ConnectingDB->prepare($sql);
$stmt -> bindValue(':categoryName',$Category);
$stmt -> bindValue(':adminName',$Admin);
$stmt -> bindValue(':dateTime',$DateTime);
$Execute = $stmt -> execute();
if ($Execute) {
$_SESSION["SuccessMessage"] = "Category Added Successfully";
Redirect_to("Categories.php");
} else {
$_SESSION["ErrorMessage"] = "Something went wrong";
Redirect_to("Categories.php");
}
}
}
?>
execute消息始终保留“出错”
当我使用普通的MySQL查询系统时,一切都正常。但我想完成我在PDO上的工作 我想你可以用
然后,您将能够打印错误和异常
try{
$stmt = $ConnectingDB->prepare($sql);
$stmt -> bindValue(':categoryName',$Category);
$stmt -> bindValue(':adminName',$Admin);
$stmt -> bindValue(':dateTime',$DateTime);
$Execute = $stmt -> execute();
}catch( PDOException $e ) {
print $e->getMessage();
}
希望能对您有所帮助。尝试在第一行的值之前或结束括号之后插入空格,就像我在下面所做的那样,有时由于编写的语法错误,查询无法工作
$sql = "INSERT INTO category(title,author,datetime) ";
或
这可能不是问题,但这是一个很好的做法。然后打印出PDO的errorInfo表中的数据类型是什么
datetime
?@kuh chan我尝试了errorInfo,得到了这个数组:array([0]=>00000[1]=>[2]=>)@Cid它是VARCHAR 50这是一个注释而不是答案。如果你需要澄清的话,试着获得你需要评论的几个声望点。这不是问题,因为他用括号来定义字段。
$sql .= " VALUES(:categoryName,:adminName,:dateTime)";