Jump to content

Running multiple queries on a page


eMonk

Recommended Posts

The code below works but the second query is being displayed at the very top of the page. It should be inside the html table. What am I doing wrong?

 

<?php

include("../includes/connect.php");

$id = $_GET['id'];

$query_1 = "SELECT * FROM model WHERE id = '$id'  ";
if ($result_1 = $db->query($query_1)) {

    while ($row = $result_1->fetch_assoc()) {

$name = $row['name'];
...
?>

<html>
<body>
<form action="updated.php" method="post">
  <table width="600" border="0" cellspacing="0" cellpadding="0">
    <tr> 
      <td width="95">Name</td>
      <td><input type="text" name="name" value="<?=$name?>" maxlength="50"></td>
    </tr>
...
<?php

include("../includes/connect.php");

$sel=array();

$query="select a.city_id, a.city_name, b.city_display from city a ";
$query  .="left outer join model_in_city b on a.city_id=b.city_id and b.city_display>0 and b.model_id=" . $id . " ";
$query .="order by a.city_name";
...
?>

 

Note: I left most of the code out so it isn't too lengthy. Hopefully you can see the logic behind it that I'm try to do.

Link to comment
Share on other sites

Here's the second query:

 

<?php

include("../includes/connect.php");

$sel=array();

$query="select a.city_id, a.city_name, b.city_display from city a ";
$query  .="left outer join model_in_city b on a.city_id=b.city_id and b.city_display>0 and b.model_id=" . $id . " ";
$query .="order by a.city_name";

$city_result=$db->query($query);
if ($city_result) 
{
    for($i=1;$i<=4;$i++)
    {
      $sel[$i]="City " . $i . ":<select name=select[" . $i . "]>\n";
    }
    while ($cityrow = mysqli_fetch_array($city_result)) 
    {
      for($i=1;$i<=4;$i++)
      {
        $sel[$i].="<option value=" . $cityrow['city_id']; 
        if($cityrow['city_display']==$i)
        {
          $sel[$i] .=" selected ";
        }
        $sel[$i].=">" . $cityrow['city_name'] . "</option>\n";
      }
    }
    for($i=1;$i<=4;$i++)
    {
      $sel[$i].="</select>";
    }
}
else
{
  echo "no city result mysql error=" . mysql_error() . "<br>\n";
}
for($i=1;$i<=4;$i++)
{
  echo $sel[$i] . "<br>\n<br>\n";
}
?>

 

Note: I still have to add in the table tags (tr, td, etc)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.