0

I'm sorry for such a basic question but I'm a bit stumped. I've been trying to build a basic website for a database I created that will graph the data. There are two variable lists that the user selects from and the data from this then is supposed to generate the requested information. However, I'm completely lost as to how to get the data selected from the list to appear in the results page. It will search under the 'country' variable, but not goods. I know that I'm probably doing something stupid, but I'm not sure what it is.

The code I'm using for the dropdown menus on the forms is as follows:

<form id="Country" name="Country" method="get" form action="/database/results_page.php"><table border="1">
    <tr>
    <td width="68">Name</td>
    <td width="48"><span id="sprytextfield4">
      <select name="selcountry" id="selcountry" title="<?php echo $row_rsCountrydropdown['']; ?>">
        <?php
    do {  
    ?>
        <option value="<?php echo $row_rsCountrydropdown['country']?>" <?php if($varcountry_rsexportsearch == $row_rsCountrydropdown['country']){echo 'selected';}?>><?php echo $row_rsCountrydropdown['country']?></option>
        <?php
    } while ($row_rsCountrydropdown = mysql_fetch_assoc($rsCountrydropdown));
      $rows = mysql_num_rows($rsCountrydropdown);
      if($rows > 0) {
          mysql_data_seek($rsCountrydropdown, 0);
          $row_rsCountrydropdown = mysql_fetch_assoc($rsCountrydropdown);
      }
    ?>
      </select>
      <select name="selgoods" id="selgoods" title="<?php echo $row_rsGoodsdropdown['']; ?>">
        <?php
    do {  
    ?>
        <option value="<?php echo $row_rsGoodsdropdown['name']?>" <?php if($vargoods_rsexportsearch == $row_rsGoodsdropdown['name']){echo 'selected';}?>><?php echo $row_rsGoodsdropdown['name']?></option>
        <?php
    } while ($row_rsGoodsdropdown = mysql_fetch_assoc($rsGoodsdropdown));
      $rows = mysql_num_rows($rsGoodsdropdown);
      if($rows > 0) {
          mysql_data_seek($rsGoodsdropdown, 0);
          $row_rsGoodsdropdown = mysql_fetch_assoc($rsGoodsdropdown);
      }
    ?>
      </select>
    </tr>
    </table>
      <input type="submit" name="submit" id="submit" value="Submit" onChange="row_rsCountrydropdown.submit()" />
    </form>

and my SQL for the results page is as follows:

mysql_select_db($database_cork_normalised, $cork_normalised);
$query_query = "SELECT exports.trade_year, country_id.country, goods.name, exports.Cork  FROM country_id, goods, exports  WHERE country_id.country_id='varcountry'   and goods.goods_id='vargoods'";
$query = mysql_query($query_query, $cork_normalised) or die(mysql_error());
$row_query = mysql_fetch_assoc($query);
$maxRows_query = 10;
$pageNum_query = 0;
if (isset($_GET['pageNum_query'])) {
  $pageNum_query = $_GET['pageNum_query'];
}
$startRow_query = $pageNum_query * $maxRows_query;

$vargoods_query = "-1";
if (isset($_POST['selgoods'])) {
  $vargoods_query = $_POST['selgoods'];
}
$varcountry_query = "-1";
if (isset($_POST['selcountry'])) {
  $varcountry_query = $_POST['selcountry'];
}

If anyone could help I'd be really grateful, this is my first foray into PHP and I'm a bit lost in it.

  • 2
    Well, the first thing you're doing wrong is using a deprecated API – Strawberry Nov 05 '15 at 21:44
  • your sql code is a total mess. you fetch a row, get a row count, rewind the result, then fetch again. using sprintf() to build a string is also unecessary.php isn't C. you don't have to build strings like that, unless you need some of the formatting options that printf()-and-cousins offer. You're also outtputting variables before they're defined. – Marc B Nov 05 '15 at 21:45
  • oh, and it'll never search under 'goods', because your db code doesn't look for `$_GET['selgoods']` at all... – Marc B Nov 05 '15 at 21:51
  • I'm sorry, I put up the SQL from the wrong page, apologie for that, the SQL for the results page is as follows: – ilovesmybrick Nov 05 '15 at 21:58
  • I'm sorry, I put up old SQL, the SQL for my results page is amended – ilovesmybrick Nov 05 '15 at 21:59
  • I highly recommend reading up on mysqli, as @Strawberry said, you're using a deprecated API. http://php.net/manual/en/book.mysqli.php – visevo Nov 05 '15 at 22:02
  • Or better than mysqli, PDO. – miken32 Nov 05 '15 at 22:03
  • I've been updating older pages with mysqli today, but I didn't think that was the problem passing the query on? – ilovesmybrick Nov 05 '15 at 22:03
  • Well you don't have a variable in your query at all. But seriously, throw this code out and forget anything that a ten-year-old tutorial taught you about `mysql_*` functions. – miken32 Nov 05 '15 at 22:06
  • 'varcountry' and 'vargoods' are the variables, that's just how they came out when I posted the code in here. In dreamweaver they appear as '%s' – ilovesmybrick Nov 05 '15 at 22:15
  • I'm aware that mysqli is the best option, but this is a small database that is only going to be used a handful of times by a couple of people, I was just hoping there was something simple that I left out that would get it working. Might have to go back and look at it so – ilovesmybrick Nov 05 '15 at 22:16
  • @ilovesmybrick even for a small project, I wouldn't use `mysql_*`. [See here](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). I like [PHP the Right Way](http://www.phptherightway.com/#databases) for a quick intro into best practices. If you're just looking to see what's been posted from the form, `var_dump` is your friend. Try using that to see what `$_POST` is returning, then go step by step to see what values you have. – mrberggg Nov 05 '15 at 22:50
  • Turns out it was a simple problem, the lists were posting the names rather than id's. Thanks for your help @mrberggg – ilovesmybrick Nov 06 '15 at 00:25

0 Answers0