2017-05-10 28 views
0
//Add this product into database now 

$sql = "INSERT INTO products 
       (product_name,price,details,category, 
       subcategory,date_added) 
     VALUES ('$product_name','$price','$details','$category', 
       '$subcategory',now())"; 
     //now() will add todays date in database 

if($result = mysqli_query($conn,$sql)){ 
    $pid = mysqli_insert_id($conn);//Generated an auto-generated id 

    //Place image in the folder 

    $newname = "$pid.jpg"; 
    move_uploaded_file($_FILES['fileField']['temp_name'],"../inventory_images/$newname");//$_FILES is a global variable. 
    header("location: inventory_list.php");//if you will not include this line then it will add the same product again if you refresh the page. 
    exit(); 
} 
else{ 
    echo"Data didn't inserted in the database"; 
} 

每当我运行代码它工作正常,数据被插入,但是当我检查我上传的图像的目录,没有什么夹。请尽早帮助我。我试图上传图像在目录中使用PHP

+1

是否打开了错误报告? – MonkeyZeus

+0

确保目录是可写的。 Chmod – Akintunde007

+1

仔细检查您是否正试图上传到正确的文件夹。相对路径,特别是用'../',总是让我感到紧张:p –

回答

1

你的问题是,你正在使用的$ _FILES数组中不存在的次数在move_uploaded_file()功能

move_uploaded_file($_FILES['fileField']['temp_name'],"../inventory_images/$newname"); 
// - - - - - - - - - - - - -- - - - - - -^^^^^^^^^ 

应该

move_uploaded_file($_FILES['fileField']['tmp_name'],"../inventory_images/$newname"); 

参考

http://php.net/manual/en/features.file-upload.post-method.php

这确实应该产生一个错误。在测试时,如果您要在实时服务器上进行测试,则应始终手动打开错误报告。

ini_set('display_errors', 1); ini_set('log_errors',1); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);添加到脚本的顶部。这会强制任何mysqli_错误到 生成您可以在浏览器上看到的异常,并且其他错误也会在浏览器中显示。

+0

很好的结果,令人惊讶的是这个人对错误报告有多么有用;和99%的PHP标记的问题在这个网站上... ... – MonkeyZeus

+0

@MonkeyZeus是的,我刚刚添加了一个关于这个建议 – RiggsFolly