我得到了编辑页面,在那里我可以添加图像。
<div class="element">
<label for="attach">Attachments <span>(optional)</span></label>
<input type="file" id="img" name="img"/>
</div>
这是我添加图像的输入,我的代码从中获取图像名称并插入行(这就是我称之为$result['img']的方式)。
但是当我再次编辑页面并保存它时,图像行中的值正在删除,因为输入在上面没有值。我如何设置它的值,这样它就不会在每次编辑时删除。
edit.php
<?php
while($edit = mysql_fetch_array($query_edit)){
print "
<h2>".$edit['lang']." - ".$edit['title']."</h2>
<div class='"entry'">
<div class='"sep'"></div>
</div>
<form action='"/admin/save'" method='"post'" ENCTYPE='"multipart/form-data'">
<div class='"element'">
<label for='"name'">Page ID <span class='"red'">(required)</span></label>
<input id='"id'" name='"id'" value=".$edit['id']." class='"text'" />
</div>
<div class='"element'">
<label for='"name'">Page title <span class='"red'">(required)</span></label>
<input id='"title'" name='"title'" value=".$edit['title']." class='"text'" />
</div>
<div class='"element'">
<label for='"category'">Category <span class='"red'">(required)</span></label>
<input id='"category'" name='"category'" value=".$edit['category']." class='"text'" />
</div>
<div class='"element'">
<label for='"category'">Sub-Category <span class='"red'">(required)</span></label>
<input id='"sub_category'" name='"sub_category'" value=".$edit['sub_category']." class='"text'" />
</div>
<div class='"element'">
<label for='"category'">News-Category <span class='"red'">(required)</span></label>
<input id='"news_category'" name='"news_category'" value=".$edit['news_category']." class='"text'" />
</div>
<div class='"element'">
<label for='"img'">Attachments <span>(optional)</span></label>
<input type='"file'" id='"img'" name='"img'"/><br><br>
<img src='/views/admin/uploads/".$edit['img']."' title='no image'/>
</div>";
if($edit['short_content'] == ''){}
else {
print"
<div class='"element'">
<label for='"short-content'">Short content <span>(optional)</span></label>
<textarea name='"short_content'" id='"short_content'" class='"textarea'" rows='"10'">".$edit['short_content']."</textarea>
</div>
";
}
print "
<div class='"element'">
<label for='"content'">Long content <span>(optional)</span></label>
<textarea name='"long_content'" id='"long_content'" class='"textarea'" rows='"10'">".$edit['content']."</textarea>
</div>
<div class='"element'">
<label for='"date'">Date <span class='"red'">(required)</span></label>
<input id='"date'" name='"date'" class='"text'" value=".$edit['date']." />
</div>
<div class='"element'">
<label for='"language'">Language <span class='"red'">(required)</span></label>
<input id='"language'" name='"language'" value=".$edit['lang']." class='"text'" />
</div>
<div class='"entry'">
<button type='"submit'" id='"button-save'" class='"add button-save'">Save page</button>
</div>
</form>
";
}
?>
save.php
<?php
$id = $_POST['id'];
$category = $_POST['category'];
$sub_category = $_POST['sub_category'];
$news_category = $_POST['news_category'];
$title = $_POST['title'];
$short_content = $_POST['short_content'];
$long_content = $_POST['long_content'];
$date = $_POST['date'];
$lang = $_POST['language'];
//echo $id." ".$category." ".$title." ".$short_content." ".$lang." ".$date." ".$sub_category;
$errors = array();
if(empty($id)){
$errors[] = "Please fill ID";
}
if(empty($category)){
$errors[] = "Please fill Category";
}
if(empty($sub_category)){
$errors[] = "Please fill Sub-Category";
}
if(empty($news_category)){
$errors[] = "Please fill News-Category";
}
if(empty($title)){
$errors[] = "Please fill Title";
}
if(empty($date)){
$errors[] = "Please fill Date";
}
if(empty($lang)){
$errors[] = "Please fill Lang";
}
if(!empty($_FILES['img']['name'])){
$extension = end(explode(".",$_FILES['img']['name']));
$name = $_FILES['img']['name'];
$size = $_FILES['img']['size'];
if(file_exists("views/admin/uploads/".$name)){
$errors[] = "File with this name already exists!";
}
if($extension != "jpg" && $extension != "png" && $extension != "gif" && $extension != "JPG"){
$errors[] = "Unknown file format!";
}
}
if(count($errors)==0){
$query = mysql_query("UPDATE `$category` SET `category`='$category',`sub_category`='$sub_category',`news_category`='$news_category',`title`='$title',`img`='$name',`short_content`='$short_content',`content`='$long_content',`date`='$date',`lang`='$lang' WHERE `id`='$id'");
move_uploaded_file($_FILES['img']['tmp_name'],"views/admin/uploads/".$name);
echo "<h2 align='"center'">Successfully updated!</h2>";
}
else{
print "<h3>Errors!</h3><ul><li>".join("</li><li>",$errors)."</li></ul>";
}
?>
听起来您希望使用PHP持久化图像名称。您需要使用PHP将每个添加的图像名称保存到数据库中,最好使用AJAX请求。
编辑
首先,您确实需要使用PHP来净化表单输入。
其次,您是否看到"成功更新"消息,或者是否有未显示的错误?
这里有一个链接到PDO库,用于使用PHP访问MySQL数据库。