Brian Brian - 1 year ago 53
SQL Question

Unable to post image and text to database. Receiving no errors

Hey guys I am having issues with my php file which is supposed to allow a user to post a status along with a picture which is uploaded to a server and its path along with the username of the user is added to the db.

DB Colomns:

  • postID (A.I)

  • username

  • status

  • imagepostpath

  • timestamp (added automatically inserting a new entry)

extra info: I have changed the code from one of my already working ones, but when I attempt to test the PHP file with Postman my error is "[]".

I'm not too familiar with PHP so if you see that the mistake that I'm making is simple, please help me understand it :)
Here is my code:


//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();


//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
//saving the file
$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

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

//if some error occurred
}catch(Exception $e){
//displaying the response
echo json_encode($response);

//closing the connection
$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));

return 1;
return ++$result['postID'];


Answer Source

Change these lines:


Your file path is always the same so old files are being overwritten by new...randomize it with md5()

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

then alter your query slightly

$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