I'm very new to PHP so please excuse me if I'm asking something which is incredibly obvious!
I'm trying to take the value that a user chooses from a drop-down control and store it in a table in the database. The catch is that the values in the list in the drop-down are themselves queried from a different table within the same database. I can get the drop-down to populate correctly, but am then struggling to get the form submission process to correctly reference the value and then store it in the table. I'm hoping that someone can help me with this.
I've included the code that I'm wrestling with below, which has been culled from a number of different sources on the net, so please don't be surprised if it looks totally wrong from an expert position :)
This is the code that displays the form and the auto-populated drop-down
¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬
$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>
¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬
This is the code that does the submission
$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);