php下拉列表未选择值

vql8enpb  于 2021-06-21  发布在  Mysql
关注(0)|答案(3)|浏览(471)

这个问题在这里已经有答案了

使用php的“notice:未定义变量”、“notice:未定义索引”和“notice:未定义偏移量”(28个答案)
mysqli_fetch_assoc()需要参数/调用成员函数bind_param()时出错。如何获得实际的mysql错误并修复它(1个答案)
两年前关门了。
我使用php从数据库中检索值,但遇到了一个问题。当我从下拉列表中选择一个值并按submit时,我输入的值不会被选中。表单中的所有其他元素都能完美地工作。我不确定我做错了什么。
当使用isset时,最终结果显示为nothing,而不是我在下拉菜单中选择的内容。
这是我的密码:

<?php 
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "timedrun";
$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} else {
echo "Connected successfully";
}
?>

<html>

<head>

<title>Add Run</title>

<link href='https://fonts.googleapis.com/css?family=Roboto' rel='stylesheet'> 
<link rel='stylesheet' type='text/css' href='mystyle.css'>

</head>

<body>
<h1>Timed Run</h1>

<center>
<hr>

<form action='#' method='post'>
Select Student: <select name='studentSelected' class="form">

<?php
$fsql = "SELECT firstName FROM students";
$fresult = $conn->query($fsql);
$lsql = "SELECT lastName FROM students";
$lresult = $conn->query($lsql);

if ($fresult->num_rows > 0) {
    while ($row = $fresult->fetch_assoc()){
        echo '<option value="'.$row['ID'].'">' .$row['firstName'].' </option>';
    }
};

?>
</select>

<br><a class="space" ></a><br>

Time (s): <input type="int" name="time" class="form">

<br><a class="space"></a><br>

Select Date: <input type="date" name="date" class="form">

<br><a class="space"></a><br>

Notes: <input type="text" name="notes" class="form">

<br><a class="space"></a><br>

<input type="submit" class="form" name="submit">

</form>

<?php
if(isset($_POST['submit'])){
$selected_val = $_POST['studentSelected'];
echo "You have selected :" .$selected_val;
}
?>
</center>
</body>
</html>
yfjy0ee7

yfjy0ee71#

<?php
$fsql = "SELECT firstName FROM students";
$fresult = $conn->query($fsql);
$lsql = "SELECT lastName FROM students";
$lresult = $conn->query($lsql);

if ($fresult->num_rows > 0) {
    while ($row = $fresult->fetch_assoc()){
        echo '<option value="'.$row['ID'].'">' .$row['firstName'].' </option>';
    }
};

您的查询只用于从表中选择firstname,并使用 value="'.$row['ID'].'" . 将查询更改为

SELECT ID,firstName FROM students
sr4lhrrt

sr4lhrrt2#

这是因为你没有选择 ID 在您的查询中。将查询更改为:

$fsql = "SELECT ID,firstName FROM students"; // add ID
$fresult = $conn->query($fsql);
$lsql = "SELECT ID,lastName FROM students";// add ID
$lresult = $conn->query($lsql);
vktxenjb

vktxenjb3#

现在应该可以了

<?php
// $fsql = "SELECT * FROM students";
$fsql = "SELECT ID,firstname FROM students";
$fresult = $conn->query($fsql);

if ($fresult->num_rows > 0) {
    while ($row = $fresult->fetch_assoc()){
        echo '<option value="'.$row['ID'].'">' .$row['firstName'].' </option>';
    }
};

相关问题