我创建了一个允许客户创建博客的表单。我希望表单提交到数据库并在提交时将图像上传到服务器。到目前为止,所有信息都已到达数据库,但图像尚未上传到服务器。
我想我要问的是,表单是否可以同时执行两个功能,这是如何完成的?谢谢。
我的表单代码如下。
<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/