-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

user9532828
  • 23
  • 11
  • Sorry, what's your question or problem, exactly? Please read [How to ask](https://stackoverflow.com/help/how-to-ask) and then try and structure your post in a better way so that you get a more positive response. – ADyson May 11 '20 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. – ADyson May 11 '20 at 13:56

1 Answers1

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.

Matthew Turland
  • 763
  • 3
  • 11
  • Thank you for your reply, I'm beginner for php and sql, please help me where I put this code in php – user9532828 May 11 '20 at 13:57
  • I'm using mysqli – user9532828 May 11 '20 at 13:58
  • Here's the [documentation for prepared statements with mysqli](https://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php). – Matthew Turland May 11 '20 at 14:00
  • I can't understand, please help me, my question – user9532828 May 11 '20 at 14:02
  • 1
    https://phpdelusions.net/mysqli also shows simple examples of how to execute SQL queries using mysqli – ADyson May 11 '20 at 14:05
  • Please I need example – user9532828 May 11 '20 at 14:14
  • i know execute sql queries, i need how to apply in drop down list – user9532828 May 11 '20 at 14:41
  • if i select 1st drop down list the result will show second drop down – user9532828 May 11 '20 at 14:43
  • I suspect that part of the solution will require JS interacting with your PHP script using an [`XMLHttpRequest`](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest) or [`fetch()`](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API). I would suggest having your PHP script return JSON using the [`json_encode()`](https://www.php.net/json_encode) function, then using [`JSON.parse()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse) on the JS side to parse that response. – Matthew Turland May 11 '20 at 14:53
  • 1
    @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. – ADyson May 11 '20 at 14:58
  • Please give me related to my project examples – user9532828 May 11 '20 at 15:19
  • 1
    [This answer](https://stackoverflow.com/questions/11255219/use-a-javascript-array-to-fill-up-a-drop-down-select-box) shows how to dynamically create ` – Matthew Turland May 11 '20 at 15:27
  • 1
    @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. – ADyson May 11 '20 at 16:42