-2

我有文件输入的错误。诚然,图像已移至服务器的文件夹 - isset($_FILES['avatar']),没问题 - 但 user_avatar 的字段未填写您的姓名。它在数据库中以“default_130x130.png”命名。我应该如何将“$user_avatar”变量从 upload_image.php 发送到 new_user.php?


zh/users/new.html

    <form id="avatar_file_upload_form" action="../../utils/upload_image.php" method="post" enctype='multipart/form-data'style="position:absolute;z-index:-10;height:1px;width:1px;overflow:hidden;visibility:hidden;">
        <input type="file" name="avatar" id="avatar_file_upload_field" accept="image/jpeg,image/pjpeg,image/bmp,image/gif,image/jpeg,image/png"/>
        <input type="submit" />
    </form>

实用程序/upload_image.php

<?php
ob_start();
$mimeExt = array();
$mimeExt['image/jpeg'] ='.jpg';
$mimeExt['image/pjpeg'] ='.jpg';
$mimeExt['image/bmp'] ='.bmp';
$mimeExt['image/gif'] ='.gif';
$mimeExt['image/x-icon'] ='.ico';
$mimeExt['image/png'] ='.png'; 
if(isset($_FILES["avatar"])) { 
     //Begins image upload
        $user_avatar = md5(uniqid(time())).$mimeExt[$_FILES["avatar"]["type"]]; //Get image extension
        $user_avatar_dir = "../img/".$user_avatar; //Path file
        move_uploaded_file($_FILES["avatar"]["tmp_name"], $user_avatar_dir); 

} else {
    $user_avatar = "default_130x130.png";
}
?>

新用户.php

<?php
ob_start();
include "config.php";
include "utils/upload_image.php";
$sql = mysql_query("insert into user(user_avatar) values('$user_avatar')", $db_connection) or die("Error: ".mysql_Error());
ob_end_clean();
mysql_close($db_connection);
?>

注意:“config.php”文件完美运行。

4

2 回答 2

0

我认为您应该将表单的 action 属性设置为new_user.php. 现在你正在将它路由到upload_image.php,我不知道你new_user.php之后如何加载。当没有文件发送到该页面时,您实际上包括在内upload_image.phpnew_user.php

这应该是您的 HTML:

<form id="avatar_file_upload_form" action="new_user.php" method="post" enctype='multipart/form-data'style="position:absolute;z-index:-10;height:1px;width:1px;overflow:hidden;visibility:hidden;">
    <input type="file" name="avatar" id="avatar_file_upload_field" accept="image/jpeg,image/pjpeg,image/bmp,image/gif,image/jpeg,image/png"/>
    <input type="submit" />
</form>

这应该是你的upload_image.php

<?php
$mimeExt = array();
$mimeExt['image/jpeg'] ='.jpg';
$mimeExt['image/pjpeg'] ='.jpg';
$mimeExt['image/bmp'] ='.bmp';
$mimeExt['image/gif'] ='.gif';
$mimeExt['image/x-icon'] ='.ico';
$mimeExt['image/png'] ='.png'; 
if(isset($_FILES["avatar"])) { 
     //Begins image upload
        $user_avatar = md5(uniqid(time())).$mimeExt[$_FILES["avatar"]["type"]]; //Get image extension
        $user_avatar_dir = "../img/".$user_avatar; //Path file
        move_uploaded_file($_FILES["avatar"]["tmp_name"], $user_avatar_dir); 

} else {
    $user_avatar = "default_130x130.png";
}
?>

这应该是你的new_user.php

<?php
ob_start();
include "config.php";
include "utils/upload_image.php";
$sql = mysql_query("insert into user(user_avatar) values('$user_avatar')", $db_connection) or die("Error: ".mysql_Error());
ob_end_clean();
mysql_close($db_connection);
?>
于 2013-10-17T14:40:00.920 回答
0

表单操作是utils/upload_image.php代码,流程表示您需要将表单操作设置为new_user.php文件。

另外,我不确定为什么插入查询有效,因为提交到页面并且与..utils/upload_image.php之间没有可见的关系。在这种情况下,您的插入查询不应该工作utils/upload_image.phpnew_user.php

更新:对于

我怎样才能做真正的插入,所以?

让您的表单转到 new_user.php 更改action="../../new_user.php"

<form id="avatar_file_upload_form" action="../../new_user.php" method="post" enctype='multipart/form-data'style="...">

</form>
于 2013-10-17T14:49:06.067 回答