我是php的新手,我遇到了一些问题。我试图从数据库中检索数据,我已设法做到并将其显示为下拉列表,但是,我无法使用我选择的变量将其插回数据库。请帮忙。我可以知道我发布到下一页的变量是什么? (这是processstaffb.php)
<form action="processstaffb.php" method="post">
<table id="t01">
<td width="1%">
<?php
$mysqli = new mysqli(spf, dbuser, dbpw, db);
$sql="Select sbranch_name from branches";
$result = $mysqli->query($sql);
if ($result->num_rows > 0) {
echo "<select name='sbranch_name'>";
while($row = $result->fetch_assoc()) {
echo "<option value='" . $row['sbranch_name'] . "'>" .$row['sbranch_name'] . "</option>";
}
echo "</select>";
}
$mysqli->close();
?>
</td>
这是processstaffb.php的代码
if (!empty($_POST['sbranch_name']))
{
$sbranch_name =$_POST["$sbranch_name"];
echo "if pass";
echo "$sbranch_name";
}
else
{
$sbranch_name = null;
echo '<p><font color="red">You forgot to enter the branch of the officer!</font></p>';
echo "if fail";
echo "$sbranch_name";
}
答案 0 :(得分:2)
由于您为select
提供了名称"sbranch_name"
,因此您可以使用$_POST['sbranch_name']
访问 processstaffb.php 中所选选项的值。
由于您计划在数据库中插入值,因此最好先了解PDO prepared statements或MySQLi prepared statements和SQL injections。