1

I am using PHP to retrieve values from a database but I have struck a problem. When I select a value from the drop-down list and press submit the value which I put in is not selected. All of the other elements in the form work perfectly. I am not sure what I am doing wrong.

When using isset, the end result shows nothing, not what I selected in the dropdown menu.

Here is my code:

<?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>
B. Desai
  • 16,414
  • 5
  • 26
  • 47

3 Answers3

3

It is because you are not selecting ID in your query. change your query to:

$fsql = "SELECT ID,firstName FROM students"; // add ID
$fresult = $conn->query($fsql);
$lsql = "SELECT ID,lastName FROM students";// add ID
$lresult = $conn->query($lsql);
B. Desai
  • 16,414
  • 5
  • 26
  • 47
  • Thank you for your help! – Blatherwick Jan 05 '18 at 12:44
  • But now this has happened, the output is the ID, not the name. How can I get it to become the name. I don't want to change the value, but I need a piece of code which changes the id into the name. Thanks – Blatherwick Jan 05 '18 at 13:04
3
<?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>';
    }
};

Your Query is for select only firstname from table And you use value="'.$row['ID'].'". Change your query to

SELECT ID,firstName FROM students
TarangP
  • 2,711
  • 5
  • 20
  • 41
1

Now it should work

<?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>';
    }
};
Pupil
  • 23,834
  • 6
  • 44
  • 66
Nims Patel
  • 1,048
  • 9
  • 19