我创建了一个表单,允许客户创建博客。我希望表单提交到数据库,并在提交时将图像上传到服务器。到目前为止,所有信息都已到达数据库,但是图像未上载到服务器。

我想我要问的是表格有可能一次执行两个功能吗,这是怎么做到的?谢谢。

我的表单代码如下。

 <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>

最佳答案

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

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

用错误消息填充<input type="file" ...>value属性也不是最好的主意。实际上,您根本不应该使用此属性。更好地在<input>标记之前或之后显示任何错误消息。

关于php - 是否可以同时将图像上传到服务器并将表单提交到数据库?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25212792/

10-11 22:20
查看更多