Stumped programming newbie here.
I started learning Php a couple of months ago (with a little HTML).
Been trying to create a dropdown select form populated from Mysql. As you will see from my code below, I think I’ve achieved that (please let me know if you notice any “mistakes” in the code).
My problem is showing/echoing the selected option/value. No matter what I select and submit from the dropdown list, only the first item shows (in this case: Alabama — with stateid 1).
I’ve tried all sorts of stuff that I’ve seen online but nothing seems to work.
There are a lot of suggestions that invoke javascript, but like I said, I am totally new to programming, so– one language at a time. I’d like to get a good grasp of php before jumping to other languages.
Surely this can be solved with just php, correct?
By the way, the ultimate goal is to create a multiple –about 3 or four– progressive dropdown forms (State, County, City). So, if you would be so kind as to indulge me, I’d also like to learn how to pass the selected value (e.g. State) to the next query/dropdown.
I’m ok with a bunch of dropdowns with submit buttons for now. I’ll eventually learn javascript.
But trying to figure out how to show the selected State is driving me nuts.
Your help/information is greatly appreciated.
“STATES” TABLE
CREATE TABLE States (
stateid int NOT NULL AUTO_INCREMENT PRIMARY KEY,
state_name varchar(25) NOT NULL
)
ENGINE=INNODB,
DEFAULT CHARACTER SET utf8;
SELECT/OPTION POPULATED FROM DATABSE — “pick_state.php”:
$con = mysql_connect("localhost", "testx", "testx") or die('Could not connect to server');
mysql_select_db("US", $con) or die('Could not connect to database');
$stateid = $_GET['stateid'];
$state_name = $GET['state_name'];
$query = "SELECT stateid,state_name FROM States";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC) or die(mysql_error());
$stateid = $row['stateid'];
$state_name = $row['state_name'];
echo "Chosen state is: $state_name";
PROCESSING FILE — “show_state.php” :
<?php
$con = mysql_connect("localhost", "test", "test") or die('Could not connect to server');
mysql_select_db("US", $con) or die('Could not connect to database');
echo "<form action=\"show_state.php\" method=\"get\">\n";
echo "<select name=\"state_name\">\n";
$query="SELECT stateid,state_name FROM States";
// ALSO : $query = "SELECT * FROM States "; --Does not work either//
$result=mysql_query($query);
while($row=mysql_fetch_array($result,MYSQL_ASSOC))
{
$stateid = $row['stateid'];
$state_name= $row['state_name'];
echo "<option value=\"$stateid\">$state_name</option>";
}
echo "</select>\n";
echo "<input name=\"submit\" type=\"submit\" id=\"stateid\" value=\"submit\" />\n";
echo "</form> \n";
?>
No matter what I chose and submit, the following page always show only the FIRST item on the table: Alabama
PAGE SHOWS: “Chosen state is: Alabama”
URL When I select and submit ARKANSAS:
/show_state.php?state_name=4&submit=submit
Does this mean that it is retrieving the correct, selected/submitted stateid (in this case: 4 –Arkansas’ stated) but just not “echoing” the state name?
TRIED the following on show_state.php
$query = "SELECT stateid,state_name FROM States WHERE state_name = $state_name";
PAGE SHOWS: Blank
$query = "SELECT stateid,state_name FROM States WHERE stateid= $stateid";
PAGE SHOWS: “You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ” at line 1″
if(isset($makeid))
PAGE SHOWS: "Query was empty"
if(isset ($_GET['makeid']))
PAGE SHOWS: "Your Car is a "
This line of your code will always prints the first state returned by the database :
What you need is change your query like this:
This is showing a blank page for you right , because you have written this :
This is
$_GETnot$GET. So change this line to :Now it should run fine 🙂
Note: