我在学校的博客网站上工作,我必须用 PHP MySQL 制作一个发布系统。我现在正在尝试将图片添加到我的帖子中,我已经制作了一个图片 uploader 并且它可以正常工作。现在我想制作一个选项按钮来选择我想要在特定博客文章中使用的图像,所以我尝试制作一个选项按钮来显示我数据库中的所有图像名称,但现在它什么也没说,而且我的表单中的 php 代码后代码停止工作。它显示标题、正文和类别,但随后显示一个空的选项框。提交按钮也没有显示。
我希望有人能帮我解决我遇到的这个问题!
我还不能发布图片,所以我会告诉我我的数据库名称是什么, image_id//图片的id name//图片名称 图片//blob
我还将 image_id 放入我的“post”表中。
代码:
<?php
session_start();
if(!isset($_SESSION['user_id'])){
header('Location: login.php');
exit();
}
include('../includes/db_connection.php');
if(!isset($_SESSION['user_id'])){
header('Location: login.php');
exit();
}
if(isset($_POST['submit'])){
//get the blog data
$title = $_POST['title'];
$body = $_POST['body'];
$category = $_POST['category'];
$image = $_POST['name'];
//link everything to the db
$title = $db->real_escape_string($title);
$body = $db->real_escape_string($body);
$user_id = $_SESSION['user_id'];
$date = date('Y-m-d G:i:s');
$body = htmlentities($body);
//check if all of these are there
if($title && $body && $category && $image){
//place data back into the database
$query = $db->query("INSERT INTO posts (user_id, title, body, category_id, image_id, posted) VALUES('$user_id', '$title', '$body', '$category', '$image', '$date')");
if($query){
echo "post added";
}
else{
echo "error";
}
}
else{
echo "MISSING DATA";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<title> Portfolio Sander Mateijsen </title>
<style>
#wrapper{
margin: auto;
width: 800px;
}
label(display:block)
</style>
</head>
<body>
<div id="wrapper">
<div id="content">
<form action="<?php echo $_SERVER["PHP_SELF"]?>" method="post">
<label>Titel:</label><br><input type="text" name="title" /><br>
<label for="body"> Body:</label><br>
<textarea name="body" cols=50 rows=10></textarea><br>
<label> Category:</label><br>
<select name="category">
<?php
//display categories from the database as options
$query = $db->query("SELECT * FROM categories");
while($row = $query->fetch_object()){
echo "<option value='".$row->category_id."'>".$row->category."</option>";
}
?>
</select>
<label> Image:</label><br>
<select name="name">
<?php
//display images from the database as options
$query = $db->query("SELECT * FROM blob");
while($row = $query->fetch_object()){
echo "<option value='".$row->image_id."'>".$row->name."</option>";
}
?>
</select>
<br><br>
<input type="submit" name="submit" value="submit" />
</form>
<a href="overzicht_post.php"> Terug naar overzicht.</a>
</div>
</div>
</div>
</body>
</html>
最佳答案
if($title && $body && $category && $image){}
检查代码中的 bool 值。
将此行更改为
if(isset($title) && isset($body) && isset($category) && isset($image)){}
还有
$query = $db->query("INSERT INTO posts (user_id, title, body, category_id, image_id, posted) VALUES('$user_id', '$title', '$body', '$category', '$image', '$date')");
是错误的,使用prepare
和execute
方法来插入和更新和绑定(bind)值
$query = $db->prepare("INSERT INTO posts (user_id, title, body, category_id, image_id, posted) VALUES(:user_id, :title, :body, :category, :image, :date)");
$query->bindParam(":user_id",$user_id);
$query->bindParam(":title",$title);
$query->bindParam(":body",$body);
$query->bindParam(":category",$category);
$query->bindParam(":image",$image);
$query->bindParam(":mydate",$date); //dont use date as bindparam because it is a special name.
$query->execute();
此外,您没有通过 posted
。我假设您在该字段的 sql 列上有默认值。
关于PHP MySQL 无法从数据库中找到我的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30781370/