我需要从db获取类别的标题,但我有此类别的ID。类别和内容具有不同的表,但内容按字段类别与类别表连接。如何加载类别的标题但不能加载ID?
创建内容的代码如下:
<form action="create_content.php" method="POST" id="content_form" enctype="multipart/form-data" >
<div class="content_text">
<p><label for="title">Title:</label></p>
<input type="text" name="title" value="">
</div>
<div class="content_textarea">
<p><label for="text">Text</label></p>
<textarea name="text" id="text" cols="30" rows="20" form="content_form">
</textarea></div>
<p><label for="text">Add image</label></p>
<div class='add_image'><input type="file" name="content_pic"></div>
<select name="category">
<?php foreach ($result as $item) {?>
<option value="<?php print $item['id'] ?>"><?php print $item['cat_title'] ?>
</option> <?php }?>
</select>
<div class="submit_content">
<input type="submit" value="Send"></div>
</form>
create_content($_POST['title'], $_POST['text'], $_FILES['content_pic'], $_POST['category']);
function create_content($title, $text, $content_pic, $category) {
$title = trim($title);
$text = trim($text);
$category= $category;
if (!empty($content_pic)) {
if ($content_pic ["error"] == UPLOAD_ERR_OK) {
$uploads_dir = __DIR__ . '/files';
$tmp_name = $content_pic ["tmp_name"];
$name = $content_pic ["name"];
if (move_uploaded_file($tmp_name, "{$uploads_dir}/{$name}")) {
$content_pic = "files/{$name}";
}
}
}
$db = database_connect();
/*if ($id) {
$stmt = $db->prepare("UPDATE INTO content (id, title,text) VALUES (?, ?, ?)");
$stmt -> execute(array($title ,$text, $id ));
}
else {*/
$stmt = $db->prepare("INSERT INTO content (`title`,
`text`,`content_pic`, `category`, `author`, `time`)
VALUES (?, ?, ?, ?, ?, ?)");
$stmt -> execute(array($title, $text, $content_pic, $category, $_SESSION['user_id'], time()));
header("Location: review.php?id=" . $insertId=$db->lastInsertId());
exit();
}
呈现视图的代码:
$id = $_GET['id'];
$select_query = "SELECT `c`.`title`,`c`.`content_pic`,`c`.`text`,
`c`.`status`,`c`.`category`, `u`.`login`,
`u`.`id` AS `user_id`, `c`.`time`
FROM content c
LEFT JOIN users u ON c.author= u.id
WHERE c.id = ?";
$db = database_connect();
$stmt = $db->prepare($select_query);
$stmt->execute(array($id));
$result = $stmt->fetch(PDO::FETCH_ASSOC);
if ($result) {
$row = $result;
$title = $row['title'];
$content_pic = $row['content_pic'];
$text = $row['text'];
$category= $row['category'];
$created = !empty($row['time']) ? date('d.m.Y H:i', $row['time']) : '';
$author = 'Anonymous';
if (!empty($row['login'])) {
$author = '<a href="review_account.php?id=' . $row['user_id'] .
'">' . $row['login'] . '</a>';
}
}
?>
<?php if ($result) {?>
<div id="destinations_content">
<div id="item_view">
<div class="items">
<div class="items_title">
<?php print $title ?>
<br>
</div>
<div class="my_pic">
<img src="<?php print $content_pic ?>" alt="">
</div>
<?php print $text ?><br>
<?php print $created?>
</div>
<?php print $category ?>
</div>
</div>
<?php }?>
答案 0 :(得分:0)
为什么不加入类别表以获取类别标题。由于您已将内容表与类别表映射。(我假设您的类别表名称为&#34;类别&#34;)。你如何获得$ row [&#39; cat_title&#39;],我在选择查询中看不到任何cat_title?
$select_query = "SELECT `c`.`title`,`c`.`content_pic`,`c`.`text`,`c`.`status`,`c`.`category`, `u`.`login`, `u`.`id` AS `user_id`, `c`.`time`, cat.title as cat_title
FROM content c
LEFT JOIN users u ON c.author= u.id
INNER JOIN categories cat ON c.category= cat.id
WHERE c.id =."(int)$id;