Forum Moderators: coopster
clients are uniquely identified by the primary key clientid. A foreign key field called clientgroupid links to tblgroups.
Groups are uniquely identified by the primary key groupid. There is also a descriptive text field called group.
I have a clients form which displays all the client fields including the 'group' field which I have set up as a combo box showing the text values. The value is actually saved in the clients table using the related clientgroupid value.
I can add and delete records just fine, but I'm having problems with my edit form.
On the edit form I am able to determine (and store as a variable) the clientgroupid of the relevant client by querying tblclients, I can also query tblgroups to fill a combo box with the relevant values.
I would like the combo box to be set to the previously stored group value on opening the form. I want to keep my two tables, which allows the user to add new groups via a seperate form. I do not want the values hardcoded into the form.
I've found very few examples dealing with combo boxes and two related tables, though I'm certain what I need must be popular throughout database systems.
I have found several examples on your site, and have got so far, but then got stuck.I'm getting a blank dropdown.I'm using PostgreSQL and PHP5, both on a Debian computer.
I feel I'm getting close, but am now in uncharted territory. My apologies for any glaring errors. Any advice will be much appreciated. What follows is my edit form code:
<html>
<head></head>
<body>
<?php
$db=pg_connect('dbname=clients');
$clientid=0;
if (isset($_GET['clientid'])) //clientid passed from previous form
$clientid=$_GET['clientid'];
else
{
header('Location:index.php');
exit;
}
echo $clientid; //displays clientid
$myquery="SELECT clientgroupid FROM tblclients WHERE clientid='$clientid'";
$myresult=pg_query($myquery);
while ($myrow=pg_fetch_assoc($myresult)) {
$cb1=htmlspecialchars($myrow['clientgroupid']);
$query1="SELECT group FROM tblgroups WHERE groupid='$cb1'";
$result1=pg_query($query1);
$result1=pg_fetch_assoc($result1);
$result1=$result1['group']; //now have text value of previously saved group ie Group1
}
echo $result1; //displays Group1
$myquery2="SELECT * FROM tblgroups"; //I would like to create a dropdown with previously saved value selected
$myresult2=pg_query($myquery2);
echo "<select name='dropdown'>";
while ($myrow2=pg_fetch_assoc($myresult2)) {
echo "<option value='$row_item[group]'";
if (isset($_POST['$result1']) && $_POST['$result1'] == $row_item['group']) {
echo " selected='selected'";
}
echo ">$row_item[group]</option>";
}
echo "</select>";
?>
</body>
</html>
I should add that this is a cut-down version of my edit form, all other text fields (which I can edit and update successfully) along with the form tags and submit button are removed for clarity.
I am guessing that it is this comparison that is killing you
if (isset($_POST['$result1']) && $_POST['$result1'] == $row_item['group']) {
a couple things here
the variables here will not be resolved
$_POST['$result1']) && $_POST['$result1']
they will be taken as literal strings since you have them surrounded with single quotes, just use the varnames by themselves
$_POST[$result1]) && $_POST[$result1]
but I think the comparison may be wrong anyway. $result1 should give you a value of 'Group1' whci means this is what you are comparing
$_POST['Group1']) && $_POST['Group1']
I am not sure that is what you want
I'm attempting to write a line of code which states:
"Compare $result1 (the text equivalent of the previously saved value) to all group values in tblgroups and make the matching group the selected value in the combobox. Then proceed to add the remaining group values to the combobox. I got this far using hardcoded values but had two entries for the 'selected' value. I'd really prefer to do it dynamically, using the two tables.
You know when you have a form in MS Access, and there's a dropdown linking to another table, and the selected value is stored as a foreign key so it's still there when you revisit the record - that's what I'm attempting. Very tricky for me in PHP though :)
<html>
<head></head>
<body>
<?php
$db=pg_connect('dbname=clients');
$clientid=0;
if (isset($_GET['clientid']))
$clientid=$_GET['clientid']; //get clientid from previous form
else
{
header('Location:index.php');
exit;
}
echo $clientid; //displays 128 clientid
$myquery="SELECT groupidclients FROM tblclients WHERE clientid='$clientid'";
$myresult=pg_query($myquery); //get id of previously selected value
while ($myrow=pg_fetch_assoc($myresult)) {
$cb4=htmlspecialchars($myrow['groupidclients']);
echo $cb4; //displays 6 - id relating to Group1 in tblgroups
$query4="SELECT group FROM tblgroups WHERE groupid='$cb4'";
$result4=pg_query($query4);
$result4=pg_fetch_assoc($result4);
$result4=$result4['group']; //now have text equivalent of id i.e. Group1
echo "$result4"; //displays Group1
echo "<select name='group'>";
$query5="SELECT * FROM tblgroups";
$result5=pg_query($query5);
while ($myrow=pg_fetch_assoc($result5)) {
echo "<option value='$myrow[group]'";
if ($result4 == $myrow['group']) {
echo " selected='selected'";
}
echo ">$myrow[group]</option>";
}
echo "</select>";
}
?>
</body>
</html>
Here's the code, tweaked ever so slightly to work in PostgreSQL:
//$clientid is captured from previous form
<?php
$info = array('Mr','Mrs','Ms','Miss');
$query7=pg_query("SELECT clienttitle FROM tblclients WHERE clientid='$clientid'");
while ($row=pg_fetch_assoc($query7))
{
echo "<select name='clienttitle'>";
foreach($info as $data)
{
echo '<option'.($row["clienttitle"]==$data? ' selected' : '').'>'.$data.'</option>';
}
echo "</select>";
}
?>