将表单中的数据插入SQL Server

时间:2012-11-01 18:21:10

标签: php sql

我有两个文件,csform.php和process.php。 Csform.php是主页面,用户将在数据中输入并点击提交,然后我有process.php和sql连接,我希望用户输入的表格数据插入到sql server中数据库。但是当提交被命中时,未插入输入到表单中的数据,则插入process.php文件的第13行中的数据。我做错了什么,似乎我两个文件之间没有连接。这是我目前的编码:

csform.php:

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CSLog</title>
</head>
<h1> Customer Service Form </h1>
<form method="post" action="process.php"> 
<table width="300" border="0">
<tr>
<td> Forte ID:</td>
<td><select id="forteid" input name="forteid">                  
                <option value="user1">user1</option>
                <option value="user2">user2</option>
                <option value="user3">user3</option>
                <option value="user4">user4</option>                   
    </select></td>
 </tr>
 <tr>
 <td> Disposition</td>
 <td><select id="disposition" input name="disposition">                 
                <option value="Save">--Save--</option>
                <option value="Sale">--Sale--</option>
                <option value="LOC">--LOC--</option>                   
    </select> </td>
</tr>
</table>
<br />
<hr />        
<br /> 
<br /> 
<table width="400" border="0">
<tr>
<td>App Number:</td>
<td></td>
<td><input type="text" name="appnumber"></td>
</tr>
<tr>
<td>Finance Number:</td>
<td></td>
<td><input type="text" name = "Finance_Num"></td>
</tr>
<tr>
<td>Number Payments:</td>
<td></td>
<td><input type="text" name = "num_payments"></td>
</tr>
<tr>
<td>Ach or CC:</td>
<td></td>
<td><input type="text" name = "ach_cc"></td>
</tr>
<tr>
<td>Date:</td>
<td></td>
<td><input type="text" name = "date"></td>
</tr>
</table>
<br />
Notes: 
<br />
<textarea input name="text" id="notes" cols="45" rows="5"></textarea>
</fieldset>
<br />
<br /> 
<hr />
<br />
<br />            
<input type="submit" name="formSubmit" value="Submit">  <input type="Reset"        name="formReset" value="Reset"> 
</form> 
</head>
<body>
</body>
</html>

然后是process.php:

<?php
$serverName = 'SRB-Nick_Desktop\SQLEXPRESS';
$connectionInfo = array('Database'=>'cslogs', 'UID'=>'cslogslogin', 'PWD'=>'123456');
$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) VALUES (?,?,?,?,?,?,?,?)";
$parameters = array( "forteid", "LOC", "NCXXXXXXX4", "SRB-000004", "0", "cc", "2012-11-01", "gave LOC instructions");
$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";
}
/* Free statement and connection resources. */
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $connection);
?>

希望有人能够帮助我或引导我朝着正确的方向指导我。

2 个答案:

答案 0 :(得分:2)

csform.php

csform.php中,<select>不需要input这个词:

更改

<select id="forteid" input name="forteid">

<select id="forteid" name="forteid">

process.php

process.php中,您需要使用$_POST[variable_name]来传递变量。您是POST process.php,但未使用$_POST中的任何内容。

您可以使用print_r($_POST);顶部的process.php来查看传递的变量:

答案 1 :(得分:0)

要建立@njk的答案,您还需要更改第13行以接受参数,这样您就可以这样做:

$parameters = array($_POST[forteid], $_POST[disposition], $_POST[appnumber], $_POST[Finance_Num], $_POST[num_payments], $_POST[ach_cc], $_POST[date], $_POST[notes]);