我正在为我的网站创建后端页面,我想使用输入控件类型“file”编辑我数据库中已存在的图像。
但是当我选择一个新图像时,它不会在数据库中修改并在其文件夹中向上移动。
这是我的代码:
<?php
$db = new MySQLi(server,user,password,database);
//other function to show data
mysqli_query($db,'SET NAMES "utf8" COLLATE "utf8_general_ci"');
if ($db->connect_errno > 0) {
die ("Failed to connect to database".$db->connect_error);
}
$id = $_GET["id"];
$news_select = "select news_id,title,content,image,news_date
from news_slider
where news_id=$id";
if (!$news_result=$db->query($news_select)) {
die($db->error);
}
?>
<!--wrapper start -->
<div id="wrapper">
<!-- u data -->
<div id="u_data">
<?php
while ($row=$news_result->fetch_assoc()) {
?>
<img id="picture" src="../../images/slider_images/<?= $row["image"]; ?>">
<h3 class="id">News id:</h3>
<h3 class="id">Title:</h3>
<h3 class="id">Content:</h3>
<h3 class="id">News_date : </h3>
<ul id="data" >
<li class="data"><h4><?php echo $row["news_id"] ?></h4> </li>
<li class="data"><h4><?php echo $row["title"] ?></h4> </li>
<li class="data" style="height::10%"><h4><?php echo $row["content"] ?></h4></li>
<li class="data"><h4><?php echo $row["news_date"] ?></h4> </li>
</ul>
<?php
}
?>
</div>
<!-- udata end -->
这是我改变和编辑数据库的表单。我将只显示与问题相关的部分:
<form name="edit_news" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"])?>" method="get" enctype="multipart/form-data">
<h3> change pic </h3> <input type="file" name="pic" id="pic" />
<input type="hidden" name="id" value="<?php echo $id ;?>" />
<!-- div submit start-->
<div style="margin:10px 0 0 0" >
<input type="submit" name="go" value=" GO ">
<input type="reset" bame="back" value=" BACK ">
</div>
<!--div submit end -->
</form>
这部分代码更改了数据库中的图像:
<?php
if ($_SERVER["REQUEST_METHOD"]== 'GET') {
//change imge file news
if (isset($_FILES["pic"]) && !empty($_FILES["pic"])) {
//difneshion variables to file uploaded
//name of file + exetension
$imgtype=pathinfo($_FILES["pic"]["name"],PATHINFO_EXTENSION);
//echo $imgtype."<br />";
//root directory for file
$path="../../images/slider_images/";
//random name
$name=mt_rand(1000,1000000);
$target=$path.$name.".".$imgtype;
//image name to type it in DB
$imageName=$name.".".$imgtype;
if ($imgtype !='jpg' && $imgtype !='png') {
echo "invalid file type";
} else if($_FILES["pic"]["size"] > 2000000) {
echo "invalid file size";
}
/* sles of transfer file uploaded start*/
//transfer file to folder profile in site root and type the name in DB
else {
/* if statement of move file start */
if (move_uploaded_file($_FILES["pic"]["tmp_name"],$target)) {
//update imge file
$imge_update="update news_slider set image='$imageName' where news_id=$id";
if(!$imge_result=$db->query($imge_update)) {
die($db->error);
} else {
//header("location:../../bookstore/bookstore_en/news_control.php");
echo "done!!";
}
}//end of if move uploaded
}//end of else
}//end of change imge news
}// end of if REQUEST