douya5194 2019-03-22 18:04
浏览 196

php pdo插入语句失败,我没有得到任何错误

i have what should be a very simple pdo insert statement which is failing. im not getting any php errors although i do not know how to integrate pdo error handling into this script

i have confirmed that the data is being recieved from the form. and if you look at my code everything is working including the basic validation. it goes wrong when i try to insert. i have been on the computer for the last two hours trying various pdo error reporting techniques that i have found online and none of them are working

<?php

$DSN = "mysql:host = localHost; dbname=cms";
$connect = new PDO($DSN, 'root', '');
?>


<?php
ini_set('display_errors', true); // set to false in production
error_reporting(E_ALL);
?>

<?php


  if(isset($_POST["submit"])){
    $postTitle = $_POST["postTitle"];
    $category  = $_POST["category"];
    $image     = $_FILES["image"]["name"];
    $target    = "uploads/".basename($_FILES["image"]["name"]);
    $postDescription = $_POST["postDescription"];
    $admin = "cole";
    date_default_timezone_set("Europe/Dublin");
    $currenttime = time();
    $datetime= strftime("%B-%d-%Y %H:%M:%S", $currenttime);

    if(empty($postTitle)){
      $_SESSION["error"] = "please add a post title";
      redirect("addnewpost.php");
    }elseif(strlen($postTitle)<5){
      $_SESSION["error"] = "post title must be longer than 5 charachters";
      redirect("addnewpost.php");
    }elseif(strlen($postDescription)>999){
      $_SESSION["error"] = "post must be less than 1000 charachters";
      redirect("addnewpost.php");
    }else {
      $sql = "INSERT INTO posts(datetime,title,category,author,image,post) VALUES(:entrytime,:postTitle,:postCategory,:adminName,:pic,:postText)";


      $stmt = $connect->prepare($sql);
      $stmt->bindValue(':entrytime',$datetime);
      $stmt->bindValue(':postTitle',$postTitle);
      $stmt->bindValue(':postCategory',$category);
      $stmt->bindValue(':admiNname',$admin);
      $stmt->bindValue(':pic',$image);
      $stmt->bindValue(':postText',$postDescription);
      $Execute = $stmt->execute();



      if($Execute){
        $_SESSION["success"] = "data added successfully";
        redirect("addnewpost.php");
      }else {
    print_r($connect->errorInfo());
        $_SESSION["error"] = "something went wrong. data not added to table";
        redirect("addnewpost.php");
      }
    }
  }
  • 写回答

2条回答 默认 最新

  • dpepbjp126917 2019-03-22 21:56
    关注

    Can you add the following when you create the PDO statement.

    $connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    

    Then you can put your query in a try/catch block and see the actual error. Either catch Exception or PDOException

    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大