使用3个表

时间:2015-11-10 08:07:42

标签: php mysql

我的表格users包含字段idusernameuser_sexuser_city。 在user_sexuser_city列中,我保存了int 1 ... n

还有两个表user_sex表,其中包含字段user_idmalefemale以及表cities字段city_id和{{1 }}

现在我想要当用户打开他的个人资料以便能够编辑它但我无法填充字段。字段是city_name字段的单选按钮和城市的下拉列表。

sex

我希望从用户表中填充此字段。

以上是我在上面解释过的SQL Fiddle

注意:由于某些原因,当我在phpmyadmin中执行时,此查询不会返回任何内容

更新

<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>

到目前为止。这是空白页。正如我所说,我不知道如何填写表格。

如果我进行简单查询,请执行以下操作:$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。所以我认为问题出在查询中。

2 个答案:

答案 0 :(得分:3)

查询未返回,因为您将usr.user_sex加入用户ID。找到下面的工作:

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

<radio name="sex" value="1" $maleChecked >
<radio name="sex" value="2" $femaleChecked >

要检查单选按钮,您可以使用&#34;选中&#34;属性。如果性别等于1或2,应该进行简单的检查。

所以:

// InputStream contains file data.
byte[] bytes = IOUtils.toByteArray(inputStream);

File file = new File(msg + "temp");
if (file.exists() && file.isDirectory()) {
  OutputStream outputStream = new FileOutputStream(new File(msg + "temp" + "/" +
    groupAttachments.getFileName()));
  outputStream.write(bytes);
  outputStream.close();
}
java.io.File savedFile = new java.io.File(msg + "temp" + "/" + 
  groupAttachments.getFileName());
DiskFileItem fileItem = new DiskFileItem("file", "text/plain", false,
                                            savedFile.getName(), (int) savedFile.length(), savedFile.getParentFile());
fileItem.getOutputStream();
MultipartFile multipartFile = new CommonsMultipartFile(fileItem);

System.out.println("Saved file size is "+savedFile.length());
if (multipartFile.isEmpty()) {
  System.out.println("Dropbox uploaded multipart file is empty");
} else {
  System.out.println("Multipart file is not empty.");
}
this.dropboxTask.insertFile(multipartFile, "",
  savedPersonalNoteObject.getNoteid(), (long) 0, true);
Path path = Paths.get(msg + "temp" + "/" + groupAttachments.getFileName());

答案 1 :(得分:3)

您的查询返回布尔值false请参阅:PHP PDO fetch returns false

所以而不是

$result -> bindParam(1, $id, PDO::PARAM_INT);

请使用以下代码:

 $id = 1;
 $result -> bindParam(intval($id), $id, PDO::PARAM_INT);

比你可以玩你的结果之后,比如获取数据$ result-&gt; fetch();等