首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >是否可以同时将图片上传到服务器并将表单提交到数据库?

是否可以同时将图片上传到服务器并将表单提交到数据库?
EN

Stack Overflow用户
提问于 2014-08-09 05:53:18
回答 2查看 200关注 0票数 0

我已经创建了一个表单,允许客户端创建博客。我希望表单提交到数据库和上传图像到服务器时提交。到目前为止,所有的信息都到达了数据库,但图像没有上传到服务器。

我猜我想问的是,表单是否可以同时执行两个功能,这是如何实现的?谢谢。

我的表单代码如下。

代码语言:javascript
运行
复制
 <form name="my-form" action="<?php echo $editFormAction; ?>" method="POST" id="my-form" class="my-form">




            <fieldset>
                    <section>
                      <label class="label">Blog Title <strong class="red-font">50 characters MAX with spaces</strong></label>
                      <label class="input">
                          <i class="icon-append fa fa-tag"></i>
                          <input type="text" name="title" id="title">
                      </label>
                    </section>

                <section>
                    <label class="label">descrition <strong class="red-font">150 characters MAX with spaces</strong></label>
                    <label class="input">
                        <i class="icon-append fa fa-edit"></i>
                        <input type="4" name="desc" id="desc">
                    </label>
                </section>


                <section>
                    <label class="label">Keywords <strong class="red-font">6-8 groups of key words</strong></label>
                    <label class="input">
                        <i class="icon-append fa fa-key"></i>
                        <input type="4" name="keywords" id="keywords">
                    </label>
                </section>


                <section>
                    <label class="label">Select A Category</label>
                    <label class="select" id="category">
                        <select name="category">
                          <?php
do {  
?>
                          <option value="<?php echo $row_rsCategory['category']?>"<?php      if (!(strcmp($row_rsCategory['category'], $row_rsCategory['category']))) {echo "selected=\"selected\"";} ?>><?php echo $row_rsCategory['category']?></option>
                          <?php
} while ($row_rsCategory = mysql_fetch_assoc($rsCategory));
$rows = mysql_num_rows($rsCategory);
if($rows > 0) {
  mysql_data_seek($rsCategory, 0);
  $row_rsCategory = mysql_fetch_assoc($rsCategory);
}
?>
                        </select>
                        <i></i>
                    </label>
                </section>


                 <section>
                    <label class="label">Add Blog Image </label>
                    <input type="file" name="blog_image" value="<?php 

//Properties of Image Upload
$name = $_FILES["myfile"] ["name"];
$type = $_FILES["myfile"] ["type"];
$size = $_FILES["myfile"] ["size"];
$temp = $_FILES["myfile"] ["tmp_name"];
$error = $_FILES["myfile"] ["error"];

if ($error > 0)

die ("Something went wrong. Please upload your image again");   
else
{   
move_uploaded_file($temp,"../../images/uploads/".$name);
}

?>">
                    </label>
                </section>  


                <section>
                    <label class="label">Type Blog</label>
                    <label class="textarea">
                        <!--<i class="icon-append fa fa-edit"></i>-->
                        <textarea rows="30" name="blog_content" id="blog_content"></textarea>
                    </label>
                </section>

                    <section>
                    <label class="label"><strong class="red-font">Publish to Web?</strong></label>
                    <label class="select" id="publish">
                        <select name="publish">
                          <option value="No" <?php if (!(strcmp("No", $row_rsBlogs['publish']))) {echo "selected=\"selected\"";} ?>>No</option>
                          <option value="Yes" <?php if (!(strcmp("Yes", $row_rsBlogs['publish']))) {echo "selected=\"selected\"";} ?>>Yes</option>
                        </select>
                        <i></i>
                    </label>
                    </section>




            <footer>
                <button type="submit" class="button">Add Blog </button>
            </footer>
            <input name="id" type="hidden" value="">
            <input name="author" type="hidden" value="<?php echo $row_rsAdmin['name']; ?>">
        </fieldset>
            <input type="hidden" name="MM_insert" value="my-form">  
  </form>
EN

回答 2

Stack Overflow用户

发布于 2014-08-09 05:56:00

您的<form>标记中缺少enctype="multipart/form-data"属性。没有它,文件上传将无法工作。

PHP :另外,您的<input type="file" ...>字段被分配了名称blog_image,但是在您的PHP代码中,您尝试获取一个由名称myfile标识的上传文件。

用错误消息填充<input type="file" ...>value属性也不是最好的主意。事实上,你根本不应该弄乱这个属性。最好在<input>标签之前或之后显示任何错误消息。

票数 2
EN

Stack Overflow用户

发布于 2014-08-09 06:01:16

打开错误报告,看看PHP是否抛出任何错误或警告。我建议使用以下方法打开错误报告:

代码语言:javascript
运行
复制
error_reporting(-1);
ini_set('display_errors', 'On');

另外,检查您的文件上传路径是否正确。

请确保enctype="multipart/ form - data“属性存在于您的also中,该属性指定在提交到服务器时表单中的数据应如何编码。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25212792

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档