简体   繁体   中英

JOIN in MySQL and relational tables error

I have three tables, "food","member" and "member_food". I'm trying to make an update user page where a collection of tags are prepopulated by the data in "member_food".

I have debugged the ID sending from the previous page which allows me to select the entry I wish to query, ID:4 .

$query = "SELECT * FROM `food` left join `member_food` on food.entityid = member_food.food_id WHERE member_id = '$id'";
            //Breakfast
            $breakfastresult1 = $mysqli->query($query);

echo '<select name="breakfast1">';
        while($BreakfastData1 = mysqli_fetch_array($breakfastresult1, MYSQL_ASSOC))
        {
            echo '<p><option value="' . htmlspecialchars($BreakfastData1['member_food.food_id']) . '">' 
                . htmlspecialchars($BreakfastData1['member_food.food_name']) 
                . '</option>'
                . '</p>';        
        }
echo '</select>';

However, the select fields appear to be empty. I think it's not pulling the correct values from my leftjoin table.

Here is an example of my member_food table: 在此处输入图片说明

food table:

在此处输入图片说明

编辑此内容,首先需要输入错误(左+联接中缺少空格),其次您需要知道哪个表member_id属于

     $query = "SELECT * FROM `food` as f left join `member_food` as mf on f.entityid = mf.food_id WHERE mf.member_id = '$id'";

You can use this to plan your joins correctly. And, as Abdul pointed out, typos are bad ;)

在此处输入图片说明

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