0

I have been having trouble populating a select element with PHP. Currently, I create an array with information pulled from a database. This is used to edit or insert an entry into a logbook for my record keeping.

The variables used in the code deal with customer numbers and customer names, if that helps give any extra relevance.

The method outlined below works perfectly in the case that it is not a new entry (editing an existing entry), but the options do not populate in the case of a new entry (where $ID == 0).

$ID = $_GET['ID'];
connectDB();
$mons = "SELECT * FROM mons ORDER BY mName ASC";
$mons = queryHashArray($mons);
disconnectDB();

<tr><td><select name="mID"> <?php
foreach($mons as $m) { 
  if($ID == 0) { ?>
      <option value="<?= $m['cusNum'] ?>"><?= $m['mName'] ?></option> <?php }
  else { ?>
      <option value="<?= $m['cusNum'] ?>" <?php if($record['mID'] == $m['cusNum']) echo "selected=\"selected\""; ?> ><?= $m['mName'] ?></option> <?php 
      } 
  }?> 
  </select></td>

Any help would be appreciated. I could not find an example online that was specific enough to my issue. I hope it is something small and simple that I have overlooked.

UPDATE:

Below is the page source from editing an existing entry and from a new entry.

Existing Entry: Due to privacy concerns, option text has been changed to "Name Omitted".

<tr><td><select name="mID">         <option value="3" selected="selected" >Name Omitted</option>        <option value="0"  >Name Omitted</option>           <option value="0"  >Name Omitted</option>

New Entry:

<tr><td><select name="mID"> <br />
<b>Warning</b>:  Invalid argument supplied for foreach() in <b>/home/folder/public_html/subfolder/file.php</b> on line <b>265</b><br />

</select></td>

$record is defined elsewhere in the code and is the record which is being edited. If it is a new record, all values are set to ''.

Does this help?

  • Is this the whole piece of code? You never dropped out of php (before the tr/td section), but you drop in and out afterwards. So it feels like you left a chunk of code out. – MJB Jul 03 '14 at 16:41
  • after the line `disconnectDB();` put this in: `var_dump($mons)` and tell us what it says –  Jul 03 '14 at 16:41
  • You don't get any option displayed in the pull down when clicking on it? – didierc Jul 03 '14 at 16:48
  • What do you see when you look at the HTML in View Source? – Barmar Jul 03 '14 at 16:48
  • @MJB - I left out the intro code, but I do indeed switch into and out of php mode. – user2631533 Jul 03 '14 at 16:56
  • I see `$_GET['ID']` then I see `$record['mID']` - Probable mismatch? Plus, do you have any form tags? I don't see any. – Funk Forty Niner Jul 03 '14 at 16:57
  • Where is `$record` being populated from? As it stands, it's currently undefined. Add error reporting to the top of your file(s) `error_reporting(E_ALL); ini_set('display_errors', 1);` – Funk Forty Niner Jul 03 '14 at 17:01
  • @Fred-ii- - $record is instantiated elsewhere in the code. The select element is within a form. $record['mID'] is a foreign key, ID is the primary key. mID checks the customer ID (cusNum) for equivalence. – user2631533 Jul 03 '14 at 17:14
  • Clearly `$mons = queryHashArray($mons);` does not work when `$_GET['ID']` is not defined. Something related to login credentials? – didierc Jul 03 '14 at 17:33

1 Answers1

0

It looks like your queryHashArray function isn't returning an array when the ID is 0. Make sure that the ID or the $_GET['ID'] variable isn't overwriting one used in your function.

Try print_r($mons) just after you run the function. That will show you what the function is returning.

Duncan Cowan
  • 495
  • 3
  • 12