1

I'm unable to insert a record here. Not sure why. My login works. My two echo statements work. But no record gets recorded. All the variables that begin with 'type' or 'subj' are checkbox form elements, so I set those columns to 'Null' in the db (using phpMyAdmin). Not sure if that's correct - the idea is they're not required. Also, the first column in my db is not an entry from this form, it's supposed to be an id for the entry, set as primary key, not-null, and set to auto-increment. This is my first time doing this. How can I test what's happening? Thank you! - and what the heck happened to my codeblock!!!

    <!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

次の方法でエラーを確認できます。

if($ db-> error)echo $ db-> error;

非常に便利!クエリが成功したと思い込まないでください。常にエラーをチェックしたい。

于 2012-09-23T05:15:02.117 に答える
0

forINSERTの場合、 を使用する必要がありますpdo::exec。これは no を返します。挿入された行の。http://us.php.net/manual/en/pdo.exec.php pdo::querySELECTクエリに使用されます。

だからあなたの声明は、

   $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

これが今私のために働いているものです。フォーム変数を約 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 に答える