将表单发布到SQL数据库中

时间:2012-12-13 22:04:44

标签: php sql forms

无论我选择何种选择,名为Forte ID的第一个字段都会发布值0。这是我的两个文件:

的index.php(表格)

</head>
<h1> Customer Service Log Form </h1>
<form method="post" action="insert.php"> 
<table width="625" border="0">
  <tr>
<td> Forte ID:</td>
<td><select id="ForteID" name="ForteID">    
                <option value="0">Select Your ID</option>   
                <option value="as7326">as7326</option>
                <option value="aw8743">aw8743</option>
                <option value="bj0920">bj0920</option> 
                <option value="bs1441">bs1441</option> 
                <option value="dk7017">dk7017</option> 
                <option value="dl7686">dl7686</option> 
                <option value="dm2940">dm2940</option> 
                <option value="jn2468">jn2468</option> 
                <option value="jw9598">jw9598</option> 
                <option value="kp4945">kp4945</option> 
                <option value="nl2589">nl2589</option> 
                <option value="rp7021">rp7021</option> 
                <option value="sh1346">sh1346</option>                 
    </select></td>
  </tr>
  <tr>
<td> Disposition</td>
<td><select id="disposition" name="disposition">                    
                <option value="0">Select a Disposition</option>
                <option value="Save">Save</option>
                <option value="Sale">Sale</option>
                <option value="LOC">LOC</option>
                <option value="Backout">Backout</option> 
                <option value="Revision">Revision</option> 
                <option value="Revision">Revision/Save</option> 
    </select> 
</td>
  </tr> 
</table>
<hr />
 <br />
 <table width="400" border="0">
  <tr>
<td>App Number:</td>
<td></td>
<td><input name="appnumber" type="text" required="required"></td>
  </tr>
  <tr>
<td>Finance Number:</td>
<td></td>
<td><input type="text" name = "Finance_Num"></td>
  </tr>
  <tr>
<td>Phone Number:</td>
<td></td>
<td><input type="text" name = "Phone_Num"></td>
  </tr>
  <tr>
<td># of Payments Collected:</td>
<td></td>
<td><input type="text" name = "num_payments"></td>
  </tr>
  <tr>
<td>ACH/CC</td>
<td></td>
<td><select id="ForteID" name="ForteID">    
                <option value="0">Select Payment</option>   
                <option value="ach">Checking</option>
                <option value="cc">Credit Card</option>                
    </select></td>
  </tr>
  <tr>
<td>Date:</td>
<td></td>
<td><input name = "date" type="text" id="datepicker" autocomplete="off" required="required"></td>
  </tr>
</table>
 <br /> 
 Notes: 
 <br />
 <textarea name="notes" id="notes" cols="45" rows="5"></textarea>
  </fieldset>
 <hr />       
<input type="Submit" name="formSubmit" value="Submit"> 
<input type="Reset" name="formReset" value="Reset"> 
<input type="button" value="View Logs" onClick="window.location.href='logs.php';">
 </form> 
</head>

Insert.php(用于将数据从表单插入SQL Server数据库的PHP文件):

$serverName = 'Server\SQLEXPRESS';
$connectionInfo = array('Database'=>'database', 'UID'=>'username', 'PWD'=>'password');
$connection = sqlsrv_connect($serverName, $connectionInfo);
if( $connection === false )
{
 echo "Connection could not be established.\n";
 die( print_r( sqlsrv_errors(), true));
}
$tsql = "INSERT INTO logs(ForteID, disposition, appnumber, Finance_Num, num_payments, ach_cc, date, notes, Phone_Num) VALUES (?,?,?,?,?,?,?,?,?)";
$parameters = array( $_POST[ForteID], $_POST[disposition], $_POST[appnumber], $_POST[Finance_Num], $_POST[num_payments], $_POST[ach_cc], $_POST[date], $_POST[notes], $_POST[Phone_Num]);
$stmt = sqlsrv_query($connection, $tsql, $parameters);
if( $stmt === false ){
echo "Statement could not be executed.\n";
 die( print_r( sqlsrv_errors(), true));
} else {
echo "Rows affected: ".sqlsrv_rows_affected( $stmt )."\n";
}

无论在index.php字段Forte ID中选择了什么选项,它都会发布值0.错误。在添加名为Phone Number的字段之前,它正在工作。但是没有理由为什么会搞砸选择。

请告诉我是否需要澄清任何内容并提前感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

您必须使用名为ForteID的字段

<select id="ForteID" name="ForteID">    
<option value="0">Select Payment</option>   
<option value="ach">Checking</option>
<option value="cc">Credit Card</option>

是第二个