我已经在网上搜索了几个小时,但我不知道为什么如果它正确显示在表单中,为什么它不能正确保存到 mysql 数据库中?
在我的表格上,我有一个显示学生姓名的下拉列表,我使用以下代码获取下拉列表的名字和姓氏:
<?php
$stud_sql = "SELECT CONCAT (First_name,' ', Last_name) AS fullname FROM students";
$stud_results = mysqli_query($db_conx, $stud_sql);
echo "<select name='fullname'>";
while ($row = mysqli_fetch_array($stud_results, MYSQLI_ASSOC)){
$fullname = $row['fullname'];
echo "<option value=".$row['fullname'].">" . $row['fullname'] . "</option>";
}
echo "</select>";
?>
表单上的下拉列表完美显示名字姓氏,但是当我尝试发布到我的 mysql 数据库时,它只保存名字而不是两者?
以下是发布到数据库的代码:
if (isset($_POST['fullname']) && ($_POST['fullname'] !='')){
$studentname = $_POST['fullname'];
if (empty($studentname))
{
$errors[] = "Missing student name.";
}
if (count($errors) > 0)
{
//there were validation errors, prepare error message
$response = "<span style=\"color:red;\">";
$response .= "the following errors occured:\n";
$response .= "<ul>";
$response .= '<li>' .implode('</li><li>', $errors) . '</li>';
$response .= "</ul>\n";
$response .= "</span>\n";
}
else
{
//No validation errors, save the dat to database
$studentname = mysqli_real_escape_string($db_conx, $studentname);
$sql = "INSERT INTO incidentform (`studentname`) value ('$studentname')";
$query = mysqli_query($db_conx, $sql);
所以为了清楚起见, concat 会显示例如 funky monkey 但只有 funky 会保存到数据库中。
好的 marc B 建议了这些更改,这些更改会将全名保存到数据库中,但现在下拉列表中只显示 1 个名称?
Name of Student:
<select name='fullname'>
<option value="$id"><?php echo $fullname; ?></option>
</select>
and
$stud_sql = "SELECT id, CONCAT (First_name,' ', Last_name) AS fullname FROM students";
$stud_results = mysqli_query($db_conx, $stud_sql);
while ($row = mysqli_fetch_array($stud_results, MYSQLI_ASSOC)){
$fullname = $row['fullname'];
}
关于如何取回完整列表的任何建议?