插入到pdo运行但不在数据库中插入数据也不显示错误

时间:2017-01-20 22:24:26

标签: php pdo sql-insert

我相信在来到这个论坛之前我已做过尽职调查。我看到了一些类似的问题。其中一个接近我的问题,但它涉及交易。我的代码更短更简单。我插入到语句中但它不会将数据插入数据库。它没有显示错误。所以我看不出错误是什么。当它运行时它重定向到标题(“location:home.php”);我在xampp中手动插入浏览器输出数据。如果有人能通过看到我错过的东西来帮助我,我会很感激。这是代码`session_start();

  require_once 'debase.php';
  error_reporting(E_ALL);
  ini_set('display_errors', 1);
  if (isset($_POST['user_name']) && !empty($_POST['user_name'])) {
  try{
  $conn = new PDO($dsn,$username,$password);
  $stmt = $conn->prepare("INSERT INTO  name(user_name,password)VALUES(:user_name,:password");
  $stmt->bindValue(1,':user_name');;
  $stmt->bindValue(2,':password');
  $stmt->execute();
  if (!$stmt) {
  echo "\nPDO::errorInfo():\n";
  print_r($dbh->errorInfo());
  }
  $stmt->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  header("location: home.php");

  }catch(PDOException $e){
  //echo $error_message = $e->getMessage();
  echo("Error description: " . mysqli_error($stmt));
  }
  }`

enter image description here

1 个答案:

答案 0 :(得分:0)

试试这个:

try{
    $conn = new PDO($dsn,$username,$password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $conn->prepare("INSERT INTO  name(user_name,password)VALUES(:user_name,:password");
    $stmt->bindValue(1,':user_name');;
    $stmt->bindValue(2,':password');
    $stmt->execute();
    if (!$stmt) {
        echo "\nPDO::errorInfo():\n";
        print_r($dbh->errorInfo());
    }else{
        header("location: home.php");
    }
  }catch(PDOException $e){
      echo $e->getMessage();
  }`
相关问题