Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to pull data from mysql using php

Status
Not open for further replies.

rhodesiascout

Programmer
Oct 27, 2000
7
0
0
GR

Hi guys this is my problem. I am making a database using mysql where i have 2 tables, one for car manufacturers and one for car models.I've already done that.
I have made a php page where i have 2 drop down menus.

The first menu shows car makers(manufacturers) and the 2nd nothing. Now when a user opens the 1st menu and selects a maker , the 2nd automatically fills up with models from that car maker only and it gets its information from the database.That is every time i decide to add a new model to my database the 2nd menu (if selected) must go and search the database for all the models for that maker and show them.

Here's the code:The first menu works. It goes into the database and selects all the cars from the makers table.However the 2nd menu shows me only CHOOSE MODEL and ---------- . It seems to have a problem in the second query when i say WHERE MAKER_ID=$ID.




<?php
$count++;
setcookie(&quot;count&quot;, $count);
?>

<?php
echo ($count . ($count == 1 ? &quot;visit&quot; : &quot; visits&quot;));
echo(&quot; <br>&quot;);

include (&quot;pw.inc&quot;);
include (&quot;functions.php3&quot;);
include (&quot;info.inc&quot;);


$table=&quot;auto_makers&quot;;

$field_name=&quot;auto_maker&quot;;


function createSelect ( $field_name, $table, $Select = 0, $select_id=&quot;&quot; ) {
global $db_name;


$conn = db_connect($db_host,$db_user,$db_pass,$db_name);
$query = &quot;SELECT model_id, auto_maker FROM auto_makers&quot;;
$res= db_query($query,$conn);

printf(&quot;<select name=\&quot;$field_name\&quot;>\n&quot;);
if ($Select == &quot;0&quot;) { printf(&quot;<option \&quot;\&quot;>Choose maker\n&quot;); printf(&quot;<option \&quot;\&quot;>--------\n&quot;);}

while ($row = db_fetch_array($res,$nr) ):

$id = $row[&quot;model_id&quot;];
$name = $row[&quot;auto_maker&quot;];
if ($id == $select_id) {
printf(&quot;<option SELECTED value=\&quot;$id\&quot;>$name\n&quot;);
} else {
printf(&quot;<option value=\&quot;$id\&quot;>$name\n&quot;);
}
endwhile;
printf(&quot;</select>\n&quot;);

###########################################################


$table2=&quot;auto_models&quot;;
$field_name2=&quot;model&quot;;





echo (&quot;<br>&quot;);
echo (&quot;<br>&quot;);



function createSelect2 ( $field_name2, $table2, $Select = 0, $select_id=&quot;&quot; ) {
global $db_name;

$conn = db_connect($db_host,$db_user,$db_pass,$db_name);
$query = &quot;SELECT maker_id, model FROM auto_models where maker_id='$id' &quot;;
$res= db_query($query,$conn);

printf(&quot;<select name=\&quot;$field_name2\&quot;>\n&quot;);
if ($Select == &quot;0&quot;) { printf(&quot;<option \&quot;\&quot;>Choose Model&quot;); printf(&quot;<option \&quot;\&quot;>--------\n&quot;);}

while ($row= db_fetch_array($res,$nr) ):
$maker_id = $row[&quot;maker_id&quot;];
$field_name2 = $row[&quot;model&quot;];
if ($maker_id == $select_id) {
printf(&quot;<option SELECTED value=\&quot;$maker_id\&quot;>$name\n&quot;);
} else {
printf(&quot;<option value=\&quot;$maker_id\&quot;>$field_name2\n&quot;);
}
endwhile;
printf(&quot;</select>\n&quot;);

}}


?>


<?php

#createSelect(&quot;FirstName&quot;,&quot;namedbase&quot;,1);

createSelect(&quot;auto_maker&quot;,&quot;&quot;);

createSelect2(&quot;model&quot;,&quot;&quot;);




?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top