php - 如何修复向 MySQL 发送查询的问题? PHP 中的错误

标签 php mysql

我已经设置了代码并使用数据库进行查询。我认为 PDO 连接到 MySQL 的一切都是正确的,但记录没有进入数据库。

//包括开始 //连接

<?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");
       }
   }
}
?>

执行消息始终保留“出现问题”

当我进入正常的 MySQL 查询系统时,一切正常。但我想完成 PDO 方面的工作。

最佳答案

我认为你可以使用pdoexception

然后您将能够打印您的错误和异常。

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();
}

pdo should i try/catch every query?

希望对您有帮助。

关于php - 如何修复向 MySQL 发送查询的问题? PHP 中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55454678/

相关文章:

php - 带重音字符的 Json 编码

php - jQuery 动态改变 MYSQL 表的列名

php - 如何用 MySQL 和 PHP 求和相同的值?

php - 用 PHP 编写的简单/快速的文件上传脚本是什么?

php - 如何在 PHP 中解析 OFX(版本 1.0.2)文件?

php - 获取woocommerce购物车总量

php - 是否有最大 PHP session 数?

php - 如何解决尝试在 Laravel 中获取非对象的属性?

php - 我可以在 PHP 中使用 Hadoop 吗?

mysql - MYSQL 引擎在查询中如何工作