gpt4 book ai didi

php - 无法将图像和文本发布到数据库。接收无错误

转载 作者:行者123 更新时间:2023-11-30 22:13:09 25 4
gpt4 key购买 nike

大家好,我的 php 文件有问题,该文件应该允许用户发布状态以及上传到服务器的图片,其路径以及用户的用户名被添加到数据库中。

数据库列:

  • postID (A.I)

  • 用户名

  • 状态

  • 图片发布路径

  • 时间戳(插入新条目时自动添加)

额外信息:我已经更改了我已经工作的代码之一,但是当我尝试使用 Postman 测试 PHP 文件时,我的错误是“[]”。

我对 PHP 不太熟悉,所以如果您发现我犯的错误很简单,请帮助我理解它:)这是我的代码:

<?php 

//importing dbDetails file
require_once 'dbDetails.php';

//this is our upload folder
$upload_path = '000002/';

//Getting the server ip
$server_ip = gethostbyname(gethostname());

//creating the upload url
$upload_url = 'http://'.$server_ip.'/Users/Images/'.$upload_path;

//response array
$response = array();


if($_SERVER['REQUEST_METHOD']=='POST'){

//checking the required parameters from the request
if(isset($_POST['name']) and isset($_FILES['image']['name'])){

//connecting to the database
$con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect...');

//getting name from the request
$name = $_POST['name'];
$status = $_POST['status'];
$timestamp = date('Y-m-d H:i:s');

//getting file info from the request
$fileinfo = pathinfo($_FILES['image']['name']);

//getting the file extension
$extension = $fileinfo['extension'];

//file url to store in the database
$file_url = $upload_url . getFileName() . '.' . $extension;

//file path to upload in the server
$file_path = $upload_path . getFileName() . '.'. $extension;

//trying to save the file in the directory
try{
//saving the file
move_uploaded_file($_FILES['image']['tmp_name'],$file_path);
$sql = "INSERT INTO `flare`.`tbl_user_feed` (`postID`, `username`, `status`, `imagepostpath`, `timestamp`) VALUES (NULL, '$name', '$status', '$file_url');";

//adding the path and name to database
if(mysqli_query($con,$sql)){

//filling response array with values
$response['error'] = false;
$response['name'] = $name;
$response['imagepostpath'] = $file_url;

}
//if some error occurred
}catch(Exception $e){
$response['error']=true;
$response['message']=$e->getMessage();
}
//displaying the response
echo json_encode($response);

//closing the connection
mysqli_close($con);
}else{
$response['error']=true;
$response['message']='Please choose a file';
}
}

/*
We are generating the file name
so this method will return a file name for the image to be upload
*/
function getFileName(){
$con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect...');
$sql = "SELECT max(postID) as postID FROM tbl_user_feed";
$result = mysqli_fetch_array(mysqli_query($con,$sql));

mysqli_close($con);
if($result['postID']==null)
return 1;
else
return ++$result['postID'];
}

?>

最佳答案

更改这些行:

move_uploaded_file($_FILES['image']['tmp_name'],$file_path);

您的文件路径始终相同,因此旧文件会被新文件覆盖...使用 md5() 对其进行随机化

$unix = time();
$file_path = $upload_path . getFileName() . md5($unix) . '.'. $extension;

然后稍微改变你的查询

$sql = "INSERT INTO `flare`.`tbl_user_feed` (`postID`, `username`, `status`, `imagepostpath`, `timestamp`) VALUES (NULL, '$name', '$status', '$file_url', '$unix')";// remove the semicolon before last double quote and add value for 5th column

关于php - 无法将图像和文本发布到数据库。接收无错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39372558/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com