我在学校的博客网站上工作,我必须使用PHP MySQL建立一个发布系统。我现在正在尝试将图像添加到我的帖子中,我已经制作了一个图像上传器并且它完全正常工作。现在我想做一个选项按钮,在特定的博客文章中选择我想要的图像,所以我试着制作一个选项按钮,它会显示我在我的数据库中的所有图像名称,但现在它没有说什么和代码在我的表单中的PHP代码后停止工作。它显示标题,正文和类别,但随后显示一个空选项框。提交按钮也没有显示。
我希望有人可以帮助我解决这个问题!
我无法发布图片,所以我会告诉我的数据库名称是什么, image_id //图像的id name //图像的名称 image // 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>
答案 0 :(得分:0)
if($title && $body && $category && $image){}
检查代码上的布尔值。
将此行更改为
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
方法进行插入和更新以及绑定值
$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列上有默认值。