1

我无法在此处插入记录。不知道为什么。我的登录有效。我的两个echo陈述有效。但是没有记录被记录。所有以“type”或“subj”开头的变量都是复选框表单元素,因此我在数据库中将这些列设置为“Null”(使用 phpMyAdmin)。不确定这是否正确 - 想法是它们不是必需的。此外,我的数据库中的第一列不是此表单中的条目,它应该是条目的 id,设置为主键,不为空,并设置为自动增量。这是我第一次这样做。我怎样才能测试发生了什么?谢谢!-我的代码块到底发生了什么!!!

    <!DOCTYPE html>
    <head>
    <title>Submit entry to database </title>
    </head>
    <body>  <?php
    //when Submit clicked
    if ( isset($_POST['submit']) ){
    $data_source = 'mysql:host=myHost;dbname=myDatabase';
    $db_user = 'myUserName';
    $db_password = 'myPassword';
    $db = new PDO($data_source, $db_user, $db_password);

    //load the form data into variables
    $reviewer = $_POST['reviewer'];
    $osj = $_POST['osj'];
    $touchDate = $_POST['touchDate'];
    $typeAction = $_POST['typeAction'];
    $typeTech = $_POST['typeTech'];
    $typeReg = $_POST['typeReg'];
    $subjSuit = $_POST['subjSuit'];
    $subjMFtran = $_POST['subjMFtran'];
    $subjConcen = $_POST['subjConcen'];
    $subjOption = $_POST['subjOption'];
    $subjTrade = $_POST['subjTrade'];
    $subjMuni = $_POST['subjMuni'];
    $subjUIT = $_POST['subjUIT'];
    $subjBkRecMFdir = $_POST['subjBkRecMFdir'];
    $subjOBA = $_POST['subjOBA'];
    $subjOther = $_POST['subjOther'];
    $flavor = $_POST['flavor'];
    $notes = $_POST['notes'];
    echo $reviewer;
    //prepare SQL statement
    $statement = "INSERT INTO tblBranchTouches 
    (reviewer, osj, touchDate, typeAction, typeTech, typeReg, subjSuit, 
    subjMFtran, subjConcen, subjOption, subjTrade, subjMuni, subjUIT, subjBkRecMFdir, 
    subjOBA, subjOther, flavor, notes) 
    VALUES ('$reviewer', '$osj', '$touchDate', '$typeAction', '$typeTech', '$typeReg', '$subjSuit', 
    '$subjMFtran', '$subjConcen', '$subjOption', '$subjTrade', '$subjMuni', '$subjUIT', '$subjBkRecMFdir', 
    '$subjOBA', '$subjOther', '$flavor', '$notes') ";
    $db->query($statement);
    //confirm
    echo "<h1>Thank You</h1>";
    }
    ?>
    <form method="post">
    <p>
    <label>
      Reviewer: 
      <input type="text" name="reviewer" list="ARTies">
      <datalist id="ARTies">
        <option value = "MWeber">
        <option value = "JBurchill">
        <option value = "DGlazer">
        <option value = "EKiburz">
        <option value = "CSupak">
        <option value = "MVallery">
        <option value = "CHo">
        <option value = "HPatil">
        <option value = "NVanDoorn">
      </datalist>
    </label>
    <label>
      OSJ: 
      <input type="text" name="osj" maxlength=3 size=3>
    </label>
    <input type="date" name="touchDate" value="<?php echo date('Y-m-d', strtotime(date('Y/m/d'))); ?>">
    </p>
    <fieldset>
    <legend>Type: </legend>
    <p><label><input type="checkbox" name="typeAction"> Action </label>
    <label><input type="checkbox" name="typeTech"> Tech. </label>
    <label><input type="checkbox" name="typeReg"> Reg.</label></p>
    </fieldset>
    <fieldset>
    <legend>Subject: </legend>
    <p>
    <label><input type="checkbox" name="subjSuit"> Suitability </label>
    <label><input type="checkbox" name="subjMFtrans"> MF Transactions </label>
    <label><input type="checkbox" name="subjConcen"> Concentrations </label>
    <label><input type="checkbox" name="subjOption"> Options </label>
    <label><input type="checkbox" name="subjTrade"> Trade Review </label>
    <label><input type="checkbox" name="subjMuni"> Munis </label>
    <label><input type="checkbox" name="subjUIT"> UITs </label>
    <label><input type="checkbox" name="subjBkRecMFdir"> Bks-Recs-MFDirect </label>
    <label><input type="checkbox" name="subjOBA"> OBAs  </label>
    <label><input type="checkbox" name="subjOther"> Other </label>
    </p>
    </fieldset>
    <p>
    <label>
      Flavor: 
      <input type="number" name="flavor" min=1 max=5 value=3>
    </label>
    <label>Notes: <textarea name="notes"></textarea></label>
    </p>
    <p>
    <input type="submit" name="submit" value="ENTER">
    </p>
    </form>
    </body>
    </html>
4

3 回答 3

0

对于INSERT,您需要使用pdo::exec,这将返回否。插入的行数。http://us.php.net/manual/en/pdo.exec.php pdo::query用于SELECT查询。

所以你的陈述是,

   $statement = "INSERT INTO tblBranchTouches 
(reviewer, osj, touchDate, typeAction, typeTech, typeReg, subjSuit, 
subjMFtran, subjConcen, subjOption, subjTrade, subjMuni, subjUIT, subjBkRecMFdir, 
subjOBA, subjOther, flavor, notes) 
VALUES ('$reviewer', '$osj', '$touchDate', '$typeAction', '$typeTech', '$typeReg', '$subjSuit', 
'$subjMFtran', '$subjConcen', '$subjOption', '$subjTrade', '$subjMuni', '$subjUIT', '$subjBkRecMFdir', 
'$subjOBA', '$subjOther', '$flavor', '$notes') ";
$count = $db->exec($statement);
if($count > 0)
  echo "<h1>Thank You</h1>";
else //else statement if you need it
  echo "<h1>No records were inserted.</h1>"; 
于 2012-09-23T03:19:37.187 回答
0

您可以使用以下方法检查错误:

if ($db->e​​rror) 回显 $db->e​​rror;

很有用!永远不要假设查询是成功的;你总是想检查错误。

于 2012-09-23T05:15:02.117 回答
0

这就是现在对我有用的东西。我只是希望我不必编写大约 7 次不同的表单变量。再次感谢...

<?php
//when Submit clicked
if ( isset($_POST['submit']) ){
  $data_source = 'mysql:host=myHost;dbname=myDB';
  $db_user = 'myUsername';
  $db_password = 'myPass';
  //create a new PDO object - constructor takes at most 4 parameters, DSN, username, password, 
  //and an array of driver options
  //a DSN is basically a string of options that tell PDO which driver to use, and the connection details
  $conn = new PDO($data_source, $db_user, $db_password, 
                array(PDO::ATTR_EMULATE_PREPARES => false, 
                      PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
  //first driver option turns off prepare emulation which is enabled in MySQL driver by default, but really should be turned off to use PDO safely
  //second driver option puts PDO into exception mode
  //
  //load the form data into variables
  $reviewer = $_POST['reviewer'];
  //(more)      
  //prepare SQL statement
  $sql="INSERT INTO tblBranchTouches 
    (reviewer, osj, touchDate, typeAction, typeTech, typeReg, subjSuit, 
    subjMFtrans, subjConcen, subjOption, subjTrade, subjMuni, subjUIT, subjBkRecMFdir, 
    subjOBA, subjOther, flavor, notes) 
    VALUES (:reviewer, :osj, :touchDate, :typeAction, :typeTech, :typeReg, :subjSuit, 
    :subjMFtrans, :subjConcen, :subjOption, :subjTrade, :subjMuni, :subjUIT, :subjBkRecMFdir, 
    :subjOBA, :subjOther, :flavor, :notes)";
  $q = $conn->prepare($sql);  
  $q->execute(array(':reviewer'=>$reviewer, ':osj'=>$osj, ':touchDate'=>$touchDate, ':typeAction'=>$typeAction, ':typeTech'=>$typeTech, 
                   ':typeReg'=>$typeReg, ':subjSuit'=>$subjSuit, ':subjMFtrans'=>$subjMFtrans, ':subjConcen'=>$subjConcen, ':subjOption'=>$subjOption, 
                   ':subjTrade'=>$subjTrade, ':subjMuni'=>$subjMuni, ':subjUIT'=>$subjUIT, ':subjBkRecMFdir'=>$subjBkRecMFdir, ':subjOBA'=>$subjOBA, 
                   ':subjOther'=>$subjOther, ':flavor'=>$flavor, ':notes'=>$notes));
}

?>

于 2012-09-24T18:15:22.593 回答