为什么图像上传到文件夹但没有插入到mysql DB中?

时间:2014-09-22 10:00:43

标签: php jquery mysql image image-uploading

嗨,大家好我有一个问题我上传图片上传文件夹上传工作正常,但他没有提交到mysql数据库的值,我真的不知道失败在哪里这是整个代码。 Unique Value是用户的id,图像名称的字段是company_logo。 我的仪表板代码: 表格:

 <form id="imageform" method="post" enctype="multipart/form-data" action='ajaximage.php'>
                           <input type="file" name="photoimg" id="photoimg" />
                           </form>

JQuery代码

 <script type="text/javascript" >
 $(document).ready(function() { 

            $('#photoimg').on('change', function()          { 
                       $("#preview").html('');
                $("#preview").html('<div class="spinner"></div>');
            $("#imageform").ajaxForm({
                        target: '#preview'
        }).submit();

            });
        }); 
</script>

最后是ajaximage.php

<?php
session_start();
ob_start();
$valid_user_id = trim($_SESSION["VALID_USER_ID"]);

if(isset($_SESSION["VALID_USER_ID"]) && !empty($valid_user_id))
{

include "database_connection.php"; //Include the database connection script

//Check the logged in user information from the database
$check_user_details = mysql_query("select * from `signup_and_login_table` where `email` = '".mysql_real_escape_string($_SESSION["VALID_USER_ID"])."'");

//Get the logged in user info from the database
$get_user_details = mysql_fetch_array($check_user_details);

//Pass all the logged in user info to variables to easily display them when needed
$user_id = strip_tags($get_user_details['id']);
$firstname = strip_tags($get_user_details['firstname']);
$lastname = strip_tags($get_user_details['lastname']);
$company = strip_tags($get_user_details['company']);
$company_logo = strip_tags($get_user_details['company_logo']);
$email = strip_tags($get_user_details['email']);
$passwd = strip_tags($get_user_details['password']);

// User Id for Image Upload
$session_id = strip_tags($get_user_details['id']); 


$path = "uploads/";

$valid_formats = array("jpg", "png", "gif", "bmp");
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
    {
        $name = $_FILES['photoimg']['name'];
        $size = $_FILES['photoimg']['size'];

        if(strlen($name))
            {
                list($txt, $ext) = explode(".", $name);
                if(in_array($ext,$valid_formats))
                {
                if($size<(1024*1024))
                    {
                        $actual_image_name = time().substr(str_replace(" ", "_", $txt), 5).".".$ext;
                        $tmp = $_FILES['photoimg']['tmp_name'];
                        if(move_uploaded_file($tmp, $path.$actual_image_name))
                            {
                            mysqli_query($db,"UPDATE signup_and_login_table SET company_logo='$actual_image_name' WHERE id='$session_id'");

                                echo "<img src='uploads/".$actual_image_name."'  class='preview'>";
                            }
                        else
                            echo "failed";
                    }
                    else
                    echo "Image file size max 1 MB";                    
                    }
                    else
                    echo "Invalid file format..";   
            }

        else
            echo "Please select image..!";

        exit;
    }

}
else
{
//Send every user who tries to access this page directly without valid session to the login page. 
//The login page is the door that every user needs to pass to this page
header("location: login.html");
}
?>

2 个答案:

答案 0 :(得分:0)

使用以下sql查询: INSERT INTO signup_and_login_table (company_logo, id) VALUES ('$actual_image_name', '$session_id')

您已经创建了一个UPDATE查询,它只更新已经存在的行。

亲切的问候!

答案 1 :(得分:0)

尝试此查询

 mysqli_query($db,"UPDATE signup_and_login_table SET company_logo='$actual_image_name' WHERE id=".$session_id);