表格未提交至数据库

时间:2013-06-07 14:37:52

标签: php sql database forms pdo

我试着让我的表单提交到我的数据库,但我是PHP的新手,我似乎无法弄清楚我哪里出错了。我知道我正在连接数据库,但是一旦我点击提交,我就会收到此页面无法显示且没有错误。我已经研究并阅读了PHP手册,但我仍然陷入困境。

<body><div id="form-container">
<h1 id="form-name">Application for Employment</h1>
<form action="senddata2.php" method="post" id="dynamic-form" class="ui-sortable">

        <div class="row" style="display: block; position: relative;">
        <label class="field" for="date">Date:<div class="rqrd">*</div></label>
        <span class="date"><span class="textField"><input type="text" id="date" name="date" data="{&quot;validate&quot;:{&quot;required&quot;:true,&quot;date&quot;:true,&quot;messages&quot;:{&quot;required&quot;:&quot;This field is required&quot;}}}"></span></div>

        <div class="row" style="display: block; position: relative;">  
        <label for="position">Position Applied For:<div class="rqrd">*</div></label>
        <input id="position" name="position" class="text" data="{&quot;validate&quot;:{&quot;required&quot;:true,&quot;messages&quot;:{&quot;required&quot;:&quot;This field is required&quot;},&quot;maxlength&quot;:&quot;50&quot;}}"></div>      

        <div class="row" style="display: block; position: relative;">  
        <label for="referral">Referral Source:<div class="rqrd">*</div></label>
        <span class="dropDown"><select id="referral" name="referral" data="{&quot;validate&quot;:{&quot;required&quot;:true,&quot;messages&quot;:{&quot;required&quot;:&quot;This field is required&quot;}}}"><option value="Advertisement ">Advertisement </option><option value="Employment Agency">Employment Agency</option><option value="Friend">Friend</option><option value="Walk-In">Walk-In</option><option value="Relative">Relative</option><option value="Other">Other</option></select></span></div>

<input type="submit" name="formSubmit" value="Submit" class="button blue" id="submit-form"> 
          <div class="clr"></div>


    </form>

<?php
$dbh = new pdo( 'sqlsrv:Server=XXXX,1433;database=XXX',
                'XXX',
                'XXX',

$tsql = "INSERT INTO testtable (Date, Position, Referral) VALUES (?, ?, ?)"; 

$stmt = $dbh->prepare($tsql);
if (!$stmt) {
echo "\nPDO::errorInfo():\n";
print_r($db->errorInfo()); 
}
$stmt->execute(array($_REQUEST['Date'], $_REQUEST['Position'], $_REQUEST['Referral']));
if (!$stmt) {
echo "\nPDO::errorInfo():\n";
print_r($stmt->errorInfo()); 
}

/* The following call to closeCursor() may be required by some drivers */
$stmt->closeCursor(); 
// and now we're done; close it
$dbh = null;
?>

2 个答案:

答案 0 :(得分:2)

您有$_REQUEST['Date']name="date"

D不是d

你的其他名字也有类似的问题。

答案 1 :(得分:0)

这里有一些事情......

  1. 您的表单字段名称错误
  2. 你应该更好地发现你的错误
  3. 您的PDO缺少)
  4. 将您的PDO包裹在try {} catch(PDOException $e) {} catch(Exception $e) {}

    周围
    try {
        $dbh = new pdo( 'sqlsrv:Server=xxxx,1433;database=xxxx',
                    'xxxx',
                    'xxxx');
        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
        $tsql = "INSERT INTO testtable (Date, Position, Referral) VALUES (?, ?, ?)"; 
        $stmt = $dbh->prepare($tsql);
        $stmt->execute(array($_REQUEST['date'], $_REQUEST['psition'], $_REQUEST['referral']));
    
        /* The following call to closeCursor() may be required by some drivers */
        $stmt->closeCursor(); 
    
        // and now we're done; close it
        $dbh = null;
    } catch(PDOException $e) {
      die("PDO Exception: " . $e->getMessage());
    } catch(Exception $e) {
      die("Exception: " . $e->getMessage());
    }