Php 为什么我上传的图像没有保存在数据库和我分配给它的文件夹中?
我不明白为什么我在创建新闻时上传的图像不能同时保存在数据库和文件夹中。也没有错误,这就是为什么我不能找出我的代码出了什么问题 这是我的createNews.phpPhp 为什么我上传的图像没有保存在数据库和我分配给它的文件夹中?,php,mysql,sql,image,mysqli,Php,Mysql,Sql,Image,Mysqli,我不明白为什么我在创建新闻时上传的图像不能同时保存在数据库和文件夹中。也没有错误,这就是为什么我不能找出我的代码出了什么问题 这是我的createNews.php <form action="/cn/admin/post_news.php" method="post" enctype="multipart/form-data"> <div class="control-group form-group"> &
<form action="/cn/admin/post_news.php" method="post" enctype="multipart/form-data">
<div class="control-group form-group">
<div class="controls">
<label>Title</label>
<input type="text" name="title" class="form-control">
<p class="help-block"></p>
</div>
</div>
<div class="control-group form-group">
<div class="controls">
<label>Image:</label>
<input type="file" name="file" multiple accept='image/*' >
<p class="help-block"></p>
</div>
</div>
<div class="control-group form-group">
<div class="controls">
<label>Body:</label><br>
<textarea rows="10" cols="100" name="body" class="form-control" id="body" maxlength="999" style="resize:none"></textarea>
</div>
</div>
<input type="submit" name="submit" value="Post News" style="float: right"/>
</form>
标题
图片:
正文:
这是我的post_news.php,这个过程发生在这里
<?php
date_default_timezone_set('Asia/Manila');
include_once('db.php');
if(isset($_POST['submit'])) {
$title = $_POST['title'];
$body = $_POST['body'];
$date = date('Y-m-d H:i:s');
$title= stripslashes($title);
$body= stripslashes($body);
$title = mysql_real_escape_string($title);
$body = mysql_real_escape_string($body);
$file = rand(1000,100000)."-".$_FILES['file']['name'];
$type = $_FILES['file']['type'];
$size = $_FILES['file']['size'];
$loc = $_FILES['file']['tmp_name'];
$destination = "/cn/news-images/";
$new_size=$size/1024; // file size in KB
// make file name in lower case
$new_file_name = strtolower($file);
// make file name in lower case
$final_file=str_replace(' ','-',$new_file_name);
$servername = "localhost";
$username="root";
$password = "";
$database = "cn";
$connection = new mysqli($servername, $username, $password, $database);
if ($connection->connect_error) {
die("Connection failed: " . $connection->connect_error);
}
if(move_uploaded_file($loc,$destination.$final_file)) {
$sql = "INSERT INTO news (title, body, name, date, photo, type, size) VALUES('$title','$body','$name','$date','$final_file','$type','$new_size')";
mysql_query($sql);
echo "<script type='text/javascript'>alert('Your news has been posted!'); window.location.assign('home.php');</script>";
}
else
{
$sql = "INSERT INTO news (title, body, name, date, photo, type, size) VALUES('$title','$body','$name','$date','','', '0')";
mysql_query($sql);
echo "<script type='text/javascript'>alert('Your news has been posted!'); window.location.assign('home.php');</script>";
}
}
?>
因为您使用的是Mysqli,所以不要使用mysql\u query($sql),请使用$connection->query($sql)。然后将文件保存到tmp dir。如果你想把它放在任何地方看
之后
$destination=“/cn/news images/”强>
关于写DB……mb之前就已经发过了…照你说的做了,现在仍然是你混合了mysql
和mysqli
,这是行不通的。正文和标题实际上保存了,除了@chay22图片的第一件事,你应该尝试给我们提供一个最简单可行的例子。你的代码中有很多行与你所面临的问题无关,它们只会让我们更难了解到底发生了什么。