Php 在PDO不工作的情况下插入

Php 在PDO不工作的情况下插入,php,mysql,Php,Mysql,过去一个小时我一直在试着让它工作。我已经检查了所有链接,并且正在引用正确的表。我从documents.php中删除了if语句并进行了检查,但没有将数据插入到表中。我还检查了错误日志,没有收到任何错误 index.php <div class="content"> <?php if (Session::get('user_logged_in') == true AND $admin == 1):?> <form action="../processes/documen

过去一个小时我一直在试着让它工作。我已经检查了所有链接,并且正在引用正确的表。我从documents.php中删除了if语句并进行了检查,但没有将数据插入到表中。我还检查了错误日志,没有收到任何错误

index.php

<div class="content">
<?php if (Session::get('user_logged_in') == true AND $admin == 1):?>
<form action="../processes/documents.php" method="post" class="pure-form pure-form-aligned">
    <legend>Add Document</legend>
      <fieldset>
        <div class="pure-control-group">
              <input type="text" name="documentnumber" placeholder="Document Number">
              </div>                                
              <div class="pure-control-group">  
              <input type="text" name="documentdate" placeholder="Document Date">
              </div>
              <div class="pure-control-group">
              <input type="text" name="expirationdate" placeholder="Document Expiration Date">  
              </div>
              <div class="pure-control-group">  
              <input type="text" name="description" placeholder="Description">
              </div>                            
              <div class="pure-control-group">  
              <select name="artistname">
                  <?php
                      $con=mysqli_connect("$hostname","$username","$password","saintfiv_artists");
                      if (mysqli_connect_errno($con))
                        {
                        echo "Failed to connect to MySQL: " . mysqli_connect_error();
                        }

                      $result2 = mysqli_query($con,"SELECT * FROM artists ORDER BY `artistname`");

                       while($row2 = mysqli_fetch_array($result2))
                        {
                        echo '<option value="' . $row2['artistname'] . '">' . $row2['artistname'] . '</option>';
                        }
                    ?>
              </select>
              </div>
              <div class="pure-control-group">
              <select name="artwork">';
                  <?php
                      $dir    = '../documents/';
                      $files = scandir($dir);
                      foreach ($files as $filename) {
                          if ($filename != '.' AND $filename != ".."){
                          echo "<option value='" . $filename . "'>".$filename."</option>";
                          }
                      }
                  ?>
              </select> 
              </div>                            
              <div class="pure-control-group">              
              <button type="submit" name="adddocument" class="pure-button pure-button-primary">Add</button>
              </div>
      </fieldset>
</form>
<?php endif; ?>
</div>

添加文档
';

“Fred,您的错误报告建议帮助我找到了问题。请添加您的评论作为答案。”

根据OP的要求,在回答问题时发表评论

加:

就在连接打开之后


另外,在打开
Q后立即将其添加到文件的顶部:为什么要对
index.php使用
mysqli\ucode>API,而对另一个使用PDO?添加
$dbh->setAttribute(PDO::ATTR\u ERRMODE,PDO::ERRMODE\u异常)在连接打开后立即执行。另外,在打开
后立即将错误报告添加到文件顶部。这是因为我还没有弄清楚如何使用PDO的select查询,到目前为止,我已经弄清楚了insert、update和delete。
不是POST元素,这就是为什么没有执行任何操作;你的条件语句就是基于它的。将您的按钮更改为
-设置了错误报告后,将发出信号
未定义索引adddocument
Fred,您的错误报告建议帮助我找到了问题。请添加您的评论作为答案。
<?php
if (isset($_POST['adddocument'])) {
include_once('../config/mysql.php');

$document_number = $_POST['documentnumber'];
$document_date = $_POST['documentdate'];
$document_expiration_date = $_POST['expirationdate'];
$description = $_POST['description'];
$artist_name = $_POST['artistname'];
$document_name = $_POST['documentname'];

try {
    $dbh = new PDO("mysql:host=$hostname;dbname=saintfiv_artists", $username, $password);
    $stmt = $dbh->prepare("INSERT INTO documents(`document_number`, `document_date`, `document_expiration_date`, `description`, `artist_name`, `document_name`) VALUES (:document_number, :document_date, :document_expiration_date, :description, :artist_name, :document_name)");
    $stmt->bindParam(':document_number', $document_number, PDO::PARAM_STR);
    $stmt->bindParam(':document_date', $document_date, PDO::PARAM_STR);
    $stmt->bindParam(':document_expiration_date', $document_expiration_date, PDO::PARAM_STR);
    $stmt->bindParam(':description', $description, PDO::PARAM_STR);
    $stmt->bindParam(':artist_name', $artist_name, PDO::PARAM_STR);
    $stmt->bindParam(':document_name', $document_name, PDO::PARAM_STR);
    $stmt->execute();
    }
catch(PDOException $e)
    {
    echo $e->getMessage();
    }
}

header("Location: http://www.saint57records.com/artistreports/documents/index");
?>
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
error_reporting(E_ALL); 
ini_set('display_errors', 1);