我对PHP还是很陌生,所以如果我要问一些明显的问题,请原谅!
我试图从下拉控件中获取用户选择的值,并将其存储在数据库的表中。问题在于,下拉列表中的值本身是从同一数据库中的不同表中查询的。我可以使用下拉列表来正确填充,但是随后我很难使表单提交过程正确地引用该值,然后将其存储在表中。我希望有人可以帮助我。
我在下面提供了我正在努力处理的代码,这些代码是从网络上的许多不同来源中剔除的,因此,如果从专家的角度来看它完全错误,请不要感到惊讶:)< / p>
这是显示表单和自动填充的下拉菜单的代码
¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬ ¬¬¬¬
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.<br /><br>";
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
$sql = "Select ProductName from ProductTypes";
$dropdown = sqlsrv_query($conn, $sql);
echo "Please select the Product Type ";
echo "<Select Product Type='ProductName'>";
while ($row = sqlsrv_fetch_array($dropdown)) {
echo "<option value='" . $row['ProductName'] . "'>" . $row['ProductName'] ."</option>";
}
echo "</select>";
?>
<form action="insertmssql.php" method="post">
<br>
ProjectID: <input type="text" name="ProjectID" /><br><br>
TestVal1: <input type="number" step="0.01" name="TestVal1" /><br><br>
TestVal2: <input type="number" step="0.01" name="TestVal2" /><br><br>
Result: <input type="text" name="Result" /><br><br>
<input type="submit" />
</form>
</body>
</html>
¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬ ¬¬¬¬p
这是执行提交的代码
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.<br />";
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
$ProductName = $_REQUEST['dropdown'];
$ProjectID = $_REQUEST['ProjectID'];
$TestVal1 = $_REQUEST['TestVal1'];
$TestVal2 = $_REQUEST['TestVal2'];
$Result = $_REQUEST['Result'];
// Attempt insert query execution
$sql = "INSERT INTO testdata (ProductName, ProjectID, TestVal1, TestVal2, Result) VALUES ('$ProductName', '$ProjectID', '$TestVal1', '$TestVal2', '$Result')";
if(sqlsrv_query($conn, $sql)){
echo "Records added successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . sqlsrv_errors($conn);
}
// Close connection
sqlsrv_close($conn);