我有表用户的字段ID,用户名,user_sex,user_city.
在user_sex和user_city列中,我保存int值1 … n.
另外还有两个表user_sex表,其中字段为user_id,男性和女性以及表城市字段为city_id和city_name.
现在我想要当用户打开他的个人资料以便能够编辑它但我无法填充字段.字段是性别字段的单选按钮和城市的下拉列表.
<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>
我希望从用户表填充此字段.
这是我上面解释的SQL Fiddle.
注意:由于某种原因,当我在PHPmyadmin中执行它时,此查询不会返回任何内容
更新:
$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>';
}
到目前为止.这是空白页.正如我所说,我不知道如何填写表格.
如果我做简单的查询,如:SELECT * FROM users WHERE id = 1我正确地得到$row [‘username’].所以我认为问题出在查询中.
解决方法:
查询未返回,因为您将usr.user_sex加入用户ID.找到下面的工作:
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
要检查单选按钮,可以使用“已检查”属性.如果性别等于1或2,应该进行简单的检查.
所以:
$sex = $row['user_sex'];
$maleChecked = ($sex == 1) ? "checked" : "";
$femaleChecked = ($sex == 2) ? "checked" : "";
<radio name="sex" value="1" $maleChecked >
<radio name="sex" value="2" $femaleChecked >