简体   繁体   English

如何修复发送查询到MySQL? PHP错误

[英]How to fix the sending query to MySQL? Error in PHP

I have setting the code and query with database. 我已经设置了代码并向数据库查询。 I think everything is correct with PDO Connection to MySQL but the record did not go to the database. 我认为与MySQL的PDO连接一切都正确,但是记录没有进入数据库。

// Includes start // Connection //包括开始//连接

<?php   
    $DSN = 'mysql:host = localhost; dbname = cms4.2.1';
    $ConnectingDB = new PDO($DSN, 'root', '');
?>

// Redirect function //重定向功能

<?php
    function Redirect_to($New_Location) {
            header("Location:".$New_Location);
        exit;
    }
?>

// Errors handling //错误处理

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

// Includes End //包含结束

// The code // 编码

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

The execute message always keeps "Something went wrong" 执行消息始终保持“发生错误”

When I go to normal MySQL query system everything is ok. 当我使用普通的MySQL查询系统时,一切正常。 But I want to complete my work on PDO. 但是我想完成我在PDO上的工作。

I think you can use pdoexception 我认为你可以使用pdoexception

then you will be able to print your errors and exceptions. 那么您将能够打印您的错误和异常。

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? pdo我应该尝试/捕获每个查询吗?

Hope that help you. 希望对您有所帮助。

Try inserting space before VALUES or after the closing bracket of the first row just like I did below, sometimes the query doesn't work because of bad written syntax. 尝试像我在下面那样在VALUES之前或第一行的右括号后插入空格,有时由于语法错误而导致查询无法正常工作。

 $sql = "INSERT INTO category(title,author,datetime) ";

OR 要么

$sql .= " VALUES(:categoryName,:adminName,:dateTime)";

This might not be the problem but it's good practice. 这可能不是问题,但这是一个好习惯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM