0

I am still relatively new to php so bear with me. I have looked a number of times at other examples, but still cannot figure this out.

I have an add page where I have a drop down that is populated by a query. This works as needed. My issue is now on an edit form, getting the dropdown box to show the value from the database field.

<select name="lvmID" id="lvmID">

<option value="">--Select--</option>
<?php
include ('../datalogin.php');
$list=mysql_query("select luchtvaartmaatschappijID, luchtvaartmaatschappij from  
tbl_luchtvaartmaatschappij WHERE inactive='0' Order by luchtvaartmaatschappij ASC");
while($row_list=mysql_fetch_assoc($list)){    ?>

<option value="<?echo $row_list['luchtvaartmaatschappijID']; ?>">
<?echo $row_list['luchtvaartmaatschappij']; ?>          
</option>

         <?
         }
         ?>
</select>

This works as expected on the Add form. But when I get to the Edit form to change the information for that page. The Select dropdown still populates, but I can't figure out how to get it so that the value from the database table where the values are stored (not the values from the dropdown) does not become selected.

So maybe the value should be '1747' 'KLM Royal Dutch Airlines'. I can't figure out to get it so when I go to bijwerk.php this value does not already become selected in the dropdown.

bijwerk.php - pulls information from tbl_vluchtgegevens table.

<body>
<?    include "datalogin.php";//database connection

$order = "select vg.*, lh.luchthavencode as vertrekluchthavencode, lh2.luchthavencode 
AS aankomstluchthavencode, lvm.luchtvaartmaatschappij AS lvmnaam, 
lvm.luchtvaartmaatschappijID, t.toestel AS toestelnaam, k.reisklass, r.reizen, 
k.reisklass, vt.vluchttype AS revenue

from tbl_vluchtgegevens vg

left join tbl_luchthaven lh
on vg.vertrekluchthaven = lh.luchthavenID

left join tbl_reizen r
on vg.reisID = r.reizenID  

left join tbl_luchthaven lh2
on vg.aankomstluchthaven = lh2.luchthavenID

left join tbl_toestel t
on vg.toestel = t.toestelID

left join tbl_klass k
on vg.reisklasse = k.klassID

left join tbl_vluchttype vt
on vg.vluchttype = vt.vluchttypeID

left join tbl_luchtvaartmaatschappij lvm
on vg.luchtvaartmaatschappij = lvm.luchtvaartmaatschappijID

WHERE gegevenID='$id'";
  $result = mysql_query($order);
  $row = mysql_fetch_array($result);
?>

<table border=1>
<tr>
<td width="646" align=center>bijwerk vluchtgegevens: <br>
ID = <? echo "$row[gegevenID]"?></td>
<td width="505" align=center>&nbsp;</td>
</tr>

<td><select name="lvmID" id="lvmID">

<option value="">--Select--</option>
<?php
include ('../datalogin.php');
$list=mysql_query("select luchtvaartmaatschappijID, luchtvaartmaatschappij from  
tbl_luchtvaartmaatschappij WHERE inactive='0' Order by luchtvaartmaatschappij ASC");
while($row_list=mysql_fetch_array($list)){    ?>

<option value="<?echo $row_list['luchtvaartmaatschappijID']; ?>" <?php echo    
($row_list['luchtvaartmaatschappijID'] == $_POST['lvmID']) ?    
'selected="selected"' : '' ?>>
<?echo $row_list['luchtvaartmaatschappij']; ?>          
</option>

     <?
     }
     ?>
</select></td>
      </table>
<?php
// close connection 
mysql_close();
?>
</body>
</html>
  • 1
    Yes, you are aproaching it right way. What problem you are facing ? – TBI Jul 07 '14 at 13:18
  • Possible duplicate of just about every question in the related questions list on the bottom right of this page. – Madara's Ghost Jul 07 '14 at 13:24
  • Hi Second Rikudo. It may be a duplicate, but I have been looking at those examples and I still cannot figure it out. So please do not always assume duplicate. –  Jul 07 '14 at 13:24
  • I assume duplicate, because it is a duplicate. If you have problems understanding a specific question, ask a question like "I saw this question, but I couldn't understand this specific part about it, what does it mean?" – Madara's Ghost Jul 07 '14 at 13:26

2 Answers2

1

So if i got this correct you want to adjust your bijwerk.php file to automaticly have the correct selected value. You can do this by modifying your loop to construct your dropdown:

while($row_list=mysql_fetch_assoc($list)){    ?>

<option value="<?echo $row_list['luchtvaartmaatschappijID']; ?>" <?php echo ($row_list['luchtvaartmaatschappijID'] == $_POST['luchtvaartmaatschappijID']) ? 'selected="selected"' : '' ?>>
<?echo $row_list['luchtvaartmaatschappij']; ?>          
</option>

         <?
         }
         ?>

The value of $_POST is the current value of the selected 'luchtvaartmaatschappijID'.

Is this what you ment?

YANTHO
  • 89
  • 7
  • Hi Yantho. No, this does not work either. I tried changing _POST to _GET also and this does not work. –  Jul 07 '14 at 13:44
  • the $_POST or $_GET represents the current value of the 'luchtvaartmaatschappij' so you will need to replace this with the correct variable. – YANTHO Jul 07 '14 at 13:54
  • hi Yantho. the $_GET['lvmID'] is an integer, which should be the luchtvaartmaatschappijID. luchtvaartmaatschappij is a text value. could this be where my issue lies? –  Jul 07 '14 at 14:09
  • shouldn't $_POST['lvmID']be changed to $row['lvm.luchtvaartmaatschappijID'] in the bijwerk.php file? – YANTHO Jul 07 '14 at 14:10
  • in PHP: echo (1==1) ? 'true' : 'false'; // true – YANTHO Jul 07 '14 at 14:13
  • and we have a winner!! I had to remove lvm from that, but once I did we had a winner. THANK YOU SO MUCH. This has frustrated me for several weeks now. I had to create search pages to get the information that i needed to enter this information and NOW, we have success!!! THANK YOU THANK YOU THANK YOU! –  Jul 07 '14 at 14:13
  • and: echo (1=="1") ? 'true' : 'false'; /: true – YANTHO Jul 07 '14 at 14:14
  • hmm. now the issue is that it's not updating the change in the table. –  Jul 07 '14 at 14:15
  • not to sound more stupid than i am, but what do you mean by echo (1=="1") ? 'true' : 'false'; /: true –  Jul 07 '14 at 14:21
0

You should check if the inactive field is actually a string or an integer. If it is an integer, quotation marks should not be used in the query, which should then be as follows:

"select luchtvaartmaatschappijID, luchtvaartmaatschappij from  
tbl_luchtvaartmaatschappij WHERE inactive=0 Order by luchtvaartmaatschappij ASC"

Also, in your html, you should change from selected="selected" to just selected.

Anyway, I would recommend you upgrade to mysqli functions, because the mysql_ functions are deprecated and are no longer safe to be used. Here you find good suggestions on how to upgrade:

  1. Updating from MYSQL to MYSQLI
  2. Upgrading to MySQLi - As easy as swapping out mysql for mysqli?
Community
  • 1
  • 1
clami219
  • 2,958
  • 1
  • 31
  • 45
  • Hi clami219, I have made all of these changes and still nothing. Yes, the value of lvmID (the field that the tbl_vluchtgegevens data is equal to the luchtvaartmaatschappijID). So I removed the ''. I changed the selected="selected" to just selected. I posted all of the relevant code for bijwerk.php up above, maybe something from that is going to yield additional information? –  Jul 07 '14 at 14:07