数据库上传不起作用[关闭]

I am trying to upload the name of an image and the name of the user to a database already created and its not working. Everything in the code is working(putting the file in the file system etc) less putting the name of the file in the database. I've tried without the ,now() and everything and its still not working.

 <?php
include_once("php_includes/check_login_status.php");

if($_FILES['image']['name'])
{
    $fileName = $_FILES["image"]["name"];
    $kaboom = explode(".", $fileName);
    $fileExt = end($kaboom);
    $db_file_name = date("DMjGisY")."".rand(1000,9999).".".$fileExt;  
    $destination_path = getcwd()."/user/$log_username/$db_file_name";
    $target_path = $destination_path . basename( $_FILES["image"]["name"]);
    move_uploaded_file($_FILES['image']['tmp_name'], $target_path);
}

$sql="INSERT INTO photos(user,filename) VALUES('$log_username', '$db_file_name',now()))";

$query = mysqli_query($db_conx, $sql);

    if($query)
    echo "Image uploaded";
    else
    echo "ERROR";
    /*<script> location.replace('../user.php?u=<?php echo $log_username;?>'); </script> */
?>

Problem is with your sql query. It has a binding error & an extra parenthesis.

$sql="INSERT INTO photos(user,filename) VALUES('$log_username', '$db_file_name',now())";

You are specifying two fields user,filename but passing three args '$log_username', '$db_file_name',now()

Correct query would be (assuming you have an DEFAULT Time stamp column in the table, otherwise you have to specify the column name for that field as well),

 $sql="INSERT INTO photos(user,filename) VALUES('$log_username', '$db_file_name')";

Note:

  • Your $query returns false because your query statement is wrong. You try to bind three VALUES on only two columns.
  • You have extra ) in your query inside $sql variable.

Your $sql query should look like this:

$sql = "INSERT INTO photos(user, filename)
                    VALUES('$log_username', '$db_file_name')";

Or if you have a datetime data type column in your photos table, it should look like this:

$sql = "INSERT INTO photos(user, filename, datecolumn) /* REPLACE NECESSARY COLUMN */
                    VALUES('$log_username', '$db_file_name', now())";