PDO Connection问题无法将数据插入mysql DB

时间:2016-03-22 16:53:52

标签: php mysql pdo

我只想创建PDO连接而不是旧连接,但我无法将表单字段插入到MYSQL DB中。我确信我犯的是一个愚蠢的错误,但我无法弄明白究竟是什么。

这是我的database.php文件

<?php
  $server = 'localhost';
  $username = 'root';
  $password = '';
  $database = 'dbaname';

  try{
     $conn = new PDO("mysql:host=$server;dbname=$database;", $username,  $password);
     } catch(PDOException $e){
     die( "Connection failed: " . $e->getMessage());
  }

在提交行动发生在我的process.php

之后
<?php 

if (!isset($_SESSION)) session_start(); 
if(!$_POST) exit;

require 'database.php';

include dirname(__FILE__).'/settings/settings.php';
include dirname(__FILE__).'/functions/emailValidation.php';


$TechName = strip_tags(trim($_POST["TechName"]));
$Date = strip_tags(trim($_POST["Date"]));   
$ToolSerial = strip_tags(trim($_POST["ToolSerial"]));
$CartridgeSerial = strip_tags(trim($_POST["CartridgeSerial"]));
$TorqueSerial = strip_tags(trim($_POST["TorqueSerial"]));
$LastCalibration = strip_tags(trim($_POST["LastCalibration"]));
$ThreadCond = strip_tags(trim($_POST["ThreadCond"]));
$HardfacingCond = strip_tags(trim($_POST["HardfacingCond"]));
$PocketCond = strip_tags(trim($_POST["PocketCond"]));
//$emailaddress = strip_tags(trim($_POST["emailaddress"]));


/*------------------ STEP 2 ------------------*/
$TorquedOEM = strip_tags(trim($_POST["TorquedOEM"]));
$FullAssembly = strip_tags(trim($_POST["FullAssembly"]));
//$file_url = strip_tags(trim($_POST["file_url"]));
$Notes = strip_tags(trim($_POST["Notes"]));
/*------------------ STEP 3 ------------------*/
//$Signature = strip_tags(trim($_POST["Signature"]));
$SignedDate = strip_tags(trim($_POST["SignedDate"]));




      try {



                    $q = "INSERT INTO tportal (TechName, Date, ToolSerial, CartridgeSerial, TorqueSerial, LastCalibration, ThreadCond, HardfacingCond, PocketCond, TorquedOEM, FullAssembly, Notes, SignedDate)

                    VALUES (:TechName, :Date, :ToolSerial, :CartridgeSerial, :TorqueSerial, :LastCalibration, :ThreadCond, :HardfacingCond, :PocketCond, :TorquedOEM, :FullAssembly, :Notes, :SignedDate)";


                     $query = $conn -> prepare($q);

                     $results = $query -> execute(array(

                      ":TechName" => $TechName,
                      ":Date" => $Date,
                      ":ToolSerial" => $ToolSerial,
                      ":CartridgeSerial" => $CartridgeSerial,
                      ":TorqueSerial" => $TorqueSerial,
                      ":LastCalibration" => $LastCalibration,
                      ":ThreadCond" => $ThreadCond,
                      ":HardfacingCond" => $HardfacingCond,
                      ":PocketCond" => $PocketCond,
                      ":TorquedOEM" => $TorquedOEM,
                      ":TorqueSerial" => $TorqueSerial,
                      ":FullAssembly" => $FullAssembly,
                      ":Notes" => $Notes,
                      ":SignedDate" => $SignedDate,

                      ));

                    if ($conn->query($q)) {
         echo "<script type= 'text/javascript'>alert('New Record Inserted Successfully');</script>";
      }
         else{
        echo "<script type= 'text/javascript'>alert('Data not successfully Inserted. $PocketCond');</script>";
       }

       $conn = null;
       }
       catch(PDOException $e)
        {
        echo $e->getMessage();
      }

       ?>

点击提交按钮后,我得到一个&#34;数据未成功插入通行证&#34; pass:是来自$ PocketCondvariable的可变结果我放置测试数据。 感谢您的时间。

1 个答案:

答案 0 :(得分:0)

显然在刷新数据库后,我意识到我的条目已添加到数据库中,但是我的错误脚本正在创建问题。

  if ($conn->query($q)) {
     echo "<script type= 'text/javascript'>alert('New Record Inserted Successfully');</script>";
  }
     else{
    echo "<script type= 'text/javascript'>alert('Data not successfully Inserted. $PocketCond');</script>";
   }