Hi folks,
So, I'm trying to use a drop down menu to select and display a record from my MySQL database. I'm not receiving any Warnings or errors when I call the page from my browser; however, regardless of which option I choose in the dropdown menu, it always displays the most recently entered record. The form is at http://www.lightsuptheatre.org/test/modify.php
Here is the entirety of the code for that page:
<form action="modify.php" method="post">
<?php
function create_dropdown($identifier,$pairs,$firstentry)
{
// Start the dropdown list with the <select> element and title
$dropdown = "<select name=\"$identifier\">";
$dropdown .= "<option name=\"\">$firstentry</option>";
// Create the dropdown elements
foreach($pairs AS $value => $name)
{
$dropdown .= "<option name=\"$value\">$name</option>";
}
// Conclude the dropdown and return it
echo "</select>";
return $dropdown;
}
include "db_login.php";
$link=mysql_connect($db_host, $db_user, $db_password)
or die ("Can't connect to the database!");
mysql_select_db ("lightsup_test")
or die ("Can't select database!");
$query = "SELECT * FROM staff ORDER BY last_name";
$result=mysql_query($query);
while($row=mysql_fetch_array($result))
{
$value=$row["last_name"];
$name=$row["last_name"].", ".$row["first_name"];
$pairs["$value"] = $name;
}
echo "Choose a record to alter: <br />";
echo create_dropdown("record",$pairs,"Select one:");
echo "<input type=\"submit\" value=\"Go\">";
echo "</form>";
if (isset($_POST['record'])){
$link = mysqli_connect($db_host, $db_user, $db_password, $db_database);
$record = mysqli_real_escape_string($link, $value);
$query = "SELECT * FROM staff WHERE last_name = '$record'";
$result = $link->query($query);
if ($result->num_rows > 0){
while ($row = $result->fetch_object())
echo "<table border=1><th>Name</th><th>Position</th><tr><td>$row->last_name, $row->first_name</td><td>$row->position</td></table><br />";
echo "Searched: $record <br />";
echo "$value";
} else {
echo "No results found.<br />";
echo "Searched:".$record;
}
}
?>
This has been driving me NUTS for hours now. I echoed a few variables back to try to figure out what was going on... so towards the end of the code, that's what you're seeing.
I'm pretty new to this so any help is appreciated and I apologize if the solution is obvious. Sometimes, you just need that second pair of eyes looking over your code. 🙂
Many thanks!