Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php正在上载图像,但未插入数据库_Php_Mysqli - Fatal编程技术网

Php正在上载图像,但未插入数据库

Php正在上载图像,但未插入数据库,php,mysqli,Php,Mysqli,我正在为我的博客开发一个后端,但是上传图片的代码没有按预期工作。 当我提交时,它上载图像并将其移动到指定的文件夹,但在数据库中没有插入任何记录。这是用于上传的php <?php session_start(); include('Connections/conn.php'); if (!isset($_SESSION['userid'])) { header("location:index.php"); } $suc=" "; $writer=$_S

我正在为我的博客开发一个后端,但是上传图片的代码没有按预期工作。 当我提交时,它上载图像并将其移动到指定的文件夹,但在数据库中没有插入任何记录。这是用于上传的php

<?php
session_start();
 include('Connections/conn.php');
  if (!isset($_SESSION['userid'])) {
    header("location:index.php");
    }

   $suc=" ";

    $writer=$_SESSION['my_username'];    

if(isset($_POST['submit']))

{
    error_reporting(E_ALL ^ E_NOTICE);
    $target_dir = "uploads/";
    $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
    $uploadOk = 1;
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

// Check if image file is a actual image or fake image

    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }

// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}

    $title=$_POST['title'];

    $intro=$_POST['intro'];
    $body=$_POST['body'];
    $keywords=$_POST['keywords'];
    $date=$_POST['date'];
    $fileToUpload=$_POST['fileToUpload'];


    $sql2="Insert into articles(title,intro,body,keywords,date,writer,fileToUpload)VALUES('$title','$intro','$body','$keywords','$date','$writer','$target_file')"or die(mysqli_error());
    $result2 = mysqli_query($db_conn,  $sql2);

    $suc=" <div class='alert alert-success'>
            <span><b>Success</b>: New article posted successfully!</span>

        </div>";

  }


?>
替换

$fileToUpload=$_POST['fileToUpload'];

替换

$fileToUpload=$_POST['fileToUpload'];

试试这个

$sql2="INSERT INTO articles(title,intro,body,keywords,date,writer,fileToUpload)VALUES('$title','$intro','$body','$keywords','$date','$writer','$target_file')");

if (!mysqli_query($db_conn, sql2))
{
  echo("Error description: " . mysqli_error($db_conn));
}
else{
    echo "Inserted";
}
我的意见是在
移动上传的文件
成功后添加插入代码

试试这个

$sql2="INSERT INTO articles(title,intro,body,keywords,date,writer,fileToUpload)VALUES('$title','$intro','$body','$keywords','$date','$writer','$target_file')");

if (!mysqli_query($db_conn, sql2))
{
  echo("Error description: " . mysqli_error($db_conn));
}
else{
    echo "Inserted";
}
我的意见是在
移动上传的文件
成功后添加插入代码


插入其他内容没有问题,只是上传图像。你应该尝试@Abdulla的回答插入其他内容没有问题,只是上传图像。你应该尝试@Abdulla的回答只在数据库中插入文件名,但不上传图像。这就是你在这里要求的。Rest以前是为您工作的,只在数据库中插入文件名,但不上传图像,这是您在这里要求的。Rest以前为你工作过