表格中的文件类型输入值

时间:2013-06-20 15:06:18

标签: php html file input

我有编辑页面,我可以在其中添加图片。

<div class="element">
<label for="attach">Attachments <span>(optional)</span></label>
<input type="file" id="img" name="img"/>
</div>

这是我添加图片的输入,我的代码从中获取IMAGE NAME并插入行(这就是我如何称之为$ 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>";
                    }
?>

1 个答案:

答案 0 :(得分:0)

听起来你想用PHP保持图像名称的持久性。您需要使用PHP将每个添加的图像名称保存到数据库中,最好使用AJAX请求。

修改

首先,您确实需要使用PHP清理表单输入。

其次,您是否看到&#34;已成功更新&#34;消息,或者是否有未显示的错误?

这里是使用PHP进行MySQL数据库访问的link to the PDO library