Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8652809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:23:38+00:00 2026-06-12T14:23:38+00:00

Stumped programming newbie here. I started learning Php a couple of months ago (with

  • 0

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 "

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T14:23:39+00:00Added an answer on June 12, 2026 at 2:23 pm

    This line of your code will always prints the first state returned by the database :

    $state_name = $row['state_name'];
    

    What you need is change your query like this:

    $query = "SELECT stateid,state_name FROM States where stateid=$state_name";
    

    This is showing a blank page for you right , because you have written this :

    $state_name = $GET['state_name'];
    

    This is $_GET not $GET. So change this line to :

    $state_name = $_GET['state_name'];
    

    Now it should run fine 🙂

    Note:

    1. Always sanitize user input using mysqli_real_escape_string
    2. Do not use *mysql*family function as suggested in the php doc.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have been stumped on this for quite a while now. I am trying
I have been stumped by this problem. I need to create a pattern such
I'm kind of stumped here. I'm relatively new to Java/Programming in general. I want
I am trying to revise for my functional programming exam, and am stumped on
I have been trying to find a solution to this everywhere but am stumped.
Totally stumped here. Trying something pretty easy, but it's not working: $(input.input1, textarea.input1).focus(function(){ $(this).addClass(input2).removeClass(input1);
I've been trying to use Python for a few sample programming competition questions, but
I'm a little new to web crawlers and such, though I've been programming for
I am studying More programming pearls (Addison-Wesley, 1988) by Bentley J. and am stumped
I have been stumped all day on this problem. Basically I want to check

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.