//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";
}
每当我运行代码它运行正常,数据被插入,但当我检查目录中我上传的图像时,该文件夹中没有任何内容。请尽早帮助我。
答案 0 :(得分:1)
您的问题是您在move_uploaded_file()
函数的<_FILES数组中使用了非现有的出现
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_
错误 生成您可以在浏览器上看到的异常,并且您的浏览器上也会显示其他错误。