-2

i have a two drop down list 1st one is make and 2nd one is model in one mysql table name= (car)

if i select 1st drop down list - make = honda means,

it will show 2nd drop down list - all model = city, crv. like that

i was working in php and mysql and here i attached mysql data table screenshot, please help me anyone..

Mysql Data table screenshot

2
  • Sorry, what's your question or problem, exactly? Please read How to ask and then try and structure your post in a better way so that you get a more positive response. Commented May 11, 2020 at 13:53
  • 1
    P.S. Really this should not be your only table - you should have "make" and "model" tables, and the car table should just have foreign keys to the "model" table. Then the "model" table would have a foreign key to the "make" table. That way it would be easy to get a unique list of makes, and a unique list of models (and if necessary a unique list of models associated with one specific make), and also to link them all together to get the full details of the car. That is how a correctly structured relational database would work in this situation. Commented May 11, 2020 at 13:56

1 Answer 1

1

This query will get you a list of unique values from the make column. I suggest adding an index to that column on your table so that this query runs optimally.

SELECT DISTINCT make FROM car ORDER BY make

Given a specific make value, this query will get you a list of the model values associated with that make value. Again, index the make column so this runs optimally.

SELECT model FROM car WHERE make = ? ORDER BY model

In your PHP code, you'll need to use a prepared statement to specify the make value selected by the user as the value of the ? marker in the above query.

If you specify which extension you're using to access your database (e.g. PDO, mysqli, etc.) I can provide more information on where to look in the PHP documentation regarding prepared statements.

Sign up to request clarification or add additional context in comments.

13 Comments

Thank you for your reply, I'm beginner for php and sql, please help me where I put this code in php
phpdelusions.net/mysqli also shows simple examples of how to execute SQL queries using mysqli
@user9532828 "if i select 1st drop down list the result will show second drop down" ...ok that wasn't what you asked for previously. Anyway that's really something you'd mostly do using Javascript. PHP/SQL code would be used only to select data (e.g. you would send the selected value from 1st dropdown to the server when user makes a selection, then SQL would run and select values for the second dropdown, return then to javascript which then displays them). This pattern is known as "cascading dropdowns", if you google for that you'll find implementation examples online already.
This answer shows how to dynamically create <select> elements using JS. Beyond that, you should be able to use the various documentation links we've provided to construct the code you're wanting. To be blunt, I don't think this is a situation where we can easily write code for you that will work with your codebase and environment. You're going to have to learn how to do that yourself.
@user9532828 "Please give me related to my project examples " ...that's what Google is for. We are not a free do-my-research service, sorry. Search for PHP cascading dropdown examples. Then try something, by attempting to adapt the examples to your needs. You can also use the PHP documentation, and other online tutorials, and previous questions on this site and others, to help you. This is by far the best way to learn. If you get stuck on a specific issue once you have started writing your code, then that would be a good time to ask another question here on StackOverflow.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.