简体   繁体   中英

MySQL left join to display one table's values but store in second table

I'm building a property website and have created a page to create a new property. When I want to edit it, I'm having difficulty displaying desired results.

Relevant details: Property table has a developer field which is the id from developer table. property create page has a drop down to select a string (taken from developer table) shown to the user but the id is stored in developer field

When I want to set an edit page, I need to show the current table values but allow to select as before.

I've tried this but it doesn't show the current table's value and due to developer and id columns being used in both tables, I used "as" to create a shortened unique form.

<?php
$qry=mysql_query("SELECT Distinct a.id as a_id,a.developer as a_dev,b.developer as b_dev FROM developer  a left join property b on b.developer=a.id", $con);
if(!$qry)
{
    die("Query Failed: ". mysql_error());
}
?>
<div class="title">Select a  <?php echo $title2 ?> : </div>
<div class="description">
<select name="field2" id="field2">
<option value=""></option>
<?php
// First, define the desired default value
$default_value = $row['b_dev'];
while($row=mysql_fetch_array($qry))
{
    // Then you can mark that one as selected in your "while" loop
    $selected = ($row['b_dev'] == $default_value) ? ' selected' : '';
    echo "<option value='" . $row['a_id'] . "'" . $selected . ">" . $row['a_dev'] . "</option>";
}
?>

The selection drop down is working fine, but the currently stored field value for b_dev doesn't display. I've tried to use variables also, but it didn't show the stored value. one more caveat is that the stored value in b.developer is a number and the displayed value has to be the string that matches b.developer=a.id so we only see the string and not the integer.

I've given relevant details, if anyone requires more details, please mention in comments, and I will try to provide.

PS: Yes I know I should be using MySQLi or PDO, but I will convert it once I get the structure and back end completed.

What is $default_value = $row['b_dev']; ? Specifically, what is $row in your case? I assume $row is unassigned before your loop.

You need to set $default_value to your current value, which you need to get from the database as a separate query.

Apparently I had to rewrite my sql to take in all the information at once using left joins. It works on some of the stuff i tried.

$qry_main=mysql_query("SELECT a.id, name, description, b.developer as pdev, c.agent as pagent ,g.country as pcountry,f.city as pcity, d.area as parea, size, furnished, h.type as ptype, finished, bed, bath, pool, featured, price,delivery from property a left join developer b on a.developer_id=b.id left join agent c on a.agent_id=c.id left join area d on a.area_id=d.id left join type h on a.type_id=h.id left join city f on d.city_id=f.id left join country g on f.country_id=g.id where a.id='$id'",$con);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM