我遇到的问题是如何在我的查询中避免重复,我的代码是这样的:
<div class="control-group">
<label class="control-label" for="inputPassword">User:</label>
<div class="controls">
<select type="text" name="user_id" placeholder="User" >
<option><?php
$sql1=mysql_query("SELECT * from `user`,`appointment` where user.user_id = appointment.user_id and appointment.branch_id = $session_branchid")or die(mysql_error);
while($row = mysql_fetch_array($sql1)){
echo "<option value=".$row["user_id"].">" .$row["firstname"].' '.$row["lastname"]. "</option>";
}
?></option>
</select>
</div>
</div>
代码没问题,但用户反复进行选项选择?
答案 0 :(得分:2)
您必须更改查询。
SELECT * from `user` Join `appointment` ON ( user.user_id = appointment.user_id) where appointment.branch_id = $session_branchid
答案 1 :(得分:0)
您可以将SQL部分更改为:
$sql1 = mysql_query("SELECT DISTINCT * from `user`,`appointment` where user.user_id = appointment.user_id and appointment.branch_id = $session_branchid")or die(mysql_error);
如果用户&#39;数据相等,那么SQL只会返回一条记录。