Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/90.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 表中的文件类型输入值_Php_Html_File_Input - Fatal编程技术网

Php 表中的文件类型输入值

Php 表中的文件类型输入值,php,html,file,input,Php,Html,File,Input,我有一个编辑页面,在那里我可以添加图像 <div class="element"> <label for="attach">Attachments <span>(optional)</span></label> <input type="file" id="img" name="img"/> </div> 附件(可选) 这是我添加图像的输入,我的代码从中提取图像名称并插入到行中(这就是我如何称呼它$resu

我有一个编辑页面,在那里我可以添加图像

<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清理表单输入

其次,您是否看到“成功更新”消息,或者是否存在未显示的错误


下面是一个使用PHP访问MySQL数据库的示例。

它不应该是
?它只是插入了图像的名称。类似于-samsung-vc240-lcd-monitor.jpg,但当我编辑它时,它会删除该行中的旧信息。那么我如何给文件类型输入一个该行的值呢?正如我在回答中所说,您没有显示任何PHP或Javascript代码,因此我们无法知道您的代码出了什么问题。如果我们只有一些HTML,这个问题就无法调试。代码一切正常!!!!!!!当我编辑获得图像的页面并保存它而不再次选择该图像时,图像行(在数据库中)中的值正在删除,因为$edit['img']为空!!如何将行值设置为文件类型输入这是我的问题?!