Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Populating form fields with query from 3 tables

Tags:

php

mysql

I have table users with fields id, username, user_sex, user_city. In columns user_sex and user_city I save int values 1 ... n.

There are two more tables user_sex table with fields user_id,male and female and table cities fields city_id and city_name.

Now I want when user open his profile to be able to edit it but I can't populate fields. Fields are radio buttons for sex field and dropdown for the city.

<div class="form-group">
    <div class="col-md-6"> 
        <label class="radio-inline" for="radios-0">
            <input type="radio" name="user_sex" id="radios-0" value="1">
            Male
        </label> 
        <label class="radio-inline" for="radios-1">
            <input type="radio" name="user_sex" id="radios-1" value="2">
            Female
        </label> 
    </div>
</div>
<div class="form-group">
      <label class="col-md-4 control-label" for="selectbasic">City</label>
      <div class="col-md-4">
         <select id="selectbasic" name="selectbasic" class="form-control">
             <option value="">Choose your city</option>
         </select>
      </div>
</div>

I want this fields populated from user table.

Here is SQL Fiddle of what I explained above.

Note: For some reason this query doesn't return anything when I execute it in phpmyadmin

Update:

$result = $pdo->prepare("
                        SELECT *, userSex.*, city.* 
                        FROM users usr
                        JOIN user_sex userSex ON usr.user_sex = userSex.user_id
                        JOIN cities city ON usr.user_city = city.city_id
                        WHERE id =?
                        LIMIT 1");


 $result -> bindParam(1, $id, PDO::PARAM_INT);
 $result -> execute();
 foreach ( $result as $row )                
 {
  echo '<legend><strong><i>'.$row['username'].'</i></strong></legend>
   <div class="form-group">
    <div class="col-md-6"> 
        <label class="radio-inline" for="radios-0">
            <input type="radio" name="user_sex" id="radios-0" value="1">
            Male
        </label> 
        <label class="radio-inline" for="radios-1">
            <input type="radio" name="user_sex" id="radios-1" value="2">
            Female
        </label> 
    </div>
  </div>
  <div class="form-group">
      <label class="col-md-4 control-label" for="selectbasic">City</label>
      <div class="col-md-4">
         <select id="selectbasic" name="selectbasic" class="form-control">
             <option value="">Choose your city</option>
         </select>
      </div>
  </div>';
 }

So far is this. It is blank page. And as I said I don't know how exactly to populate the form.

If I make simple query like: SELECT * FROM users WHERE id = 1 I've got $row['username'] correctly. So i think the problem is in the query.

like image 430
S.I. Avatar asked Mar 14 '23 05:03

S.I.


1 Answers

Query was not returning because you where joining usr.user_sex to user id. Find the working one below:

SELECT usr.*, userSex.*, city.*
   FROM users usr
JOIN user_sex userSex
    ON usr.id = userSex.user_id
JOIN cities city
    ON usr.user_city = city.city_id
WHERE id = 1

To check the radio buttons you can use the "checked" attribute. A simple check if the sex equals 1 or 2 should do.

So:

$sex = $row['user_sex'];
$maleChecked = ($sex == 1) ? "checked" : "";
$femaleChecked = ($sex == 2) ? "checked" : "";

<radio name="sex" value="1" $maleChecked >
<radio name="sex" value="2" $femaleChecked >
like image 189
Hans Dubois Avatar answered Mar 17 '23 04:03

Hans Dubois