php error file not upload on this api code - php

this api code is in php and server is in digitalocen and ubuntu 16.04.
we are try upload file but every time we show the error param missing .
we are try thi change folder permission 777 but nothing steel file is not uploading.the same code working 2 days ego but now code is not working.
<?php
include('conn.php');
include('function.php');
$user_id = $_POST['user_id'];
$dute_user_id = $_POST['dute_user_id'];
$music_id = $_POST['music_id'];
$music = $_FILES['music']['name'];
$hashtags = $_POST['hashtags'];
$video = $_FILES['video']['name'];
$gif = $_FILES['gif']['name'];
$microtime = round(microtime(true) * 1000);
validate_user($con, $user_id);
if($user_id=="" || $video=="" || $gif=="" || ($music_id=="" && $music=="")){
$res['status'] = 0;
$res['msg'] = "param missing";
echo json_encode($res);
die;
}
else{
$user_data = mysqli_fetch_assoc(mysqli_query($con, "SELECT * FROM `user` WHERE `user_id`='".$user_id."'"));
if($music_id=="" && $music!=""){
$music = basename($_FILES["music"]["name"]);
$fileType = strtolower(pathinfo($music,PATHINFO_EXTENSION));
$music_file = "music/" . $microtime .".". $fileType;
move_uploaded_file($_FILES["music"]["tmp_name"], $music_file);
$title = "original - " . $user_data['user_name'] . rand(1111,9999);
mysqli_query($con, "INSERT INTO `music` (`title`, `user_id`, `m_link`, `time`) VALUES ('".$title."', '".$user_id."', '".$music_file."', '".$microtime."')");
$music_id = mysqli_insert_id($con);
mysqli_query($con, "INSERT INTO `m_category` (`music_id`, `category_id`) VALUES ('".$music_id."', '1')");
}
if($video!=""){
$video = basename($_FILES["video"]["name"]);
$fileType = strtolower(pathinfo($video,PATHINFO_EXTENSION));
$video_file = "video/" . $microtime .".". $fileType;
move_uploaded_file($_FILES["video"]["tmp_name"], $video_file);
}
if($gif!=""){
$gif = basename($_FILES["gif"]["name"]);
$fileType = strtolower(pathinfo($gif,PATHINFO_EXTENSION));
$gif_file = "gif/" . $microtime .".". $fileType;
move_uploaded_file($_FILES["gif"]["tmp_name"], $gif_file);
}
mysqli_query($con, "INSERT INTO `post`(`music_id`, `v_url`, `user_id`, `dute_user_id`, `gif_url`, `time`) VALUES ('".$music_id."', '".$video_file."', '".$user_id."', '".$dute_user_id."', '".$gif_file."', '".$microtime."')");
$post_id = mysqli_insert_id($con);
if($dute_user_id!=""){
mysqli_query($con, "INSERT INTO `post_hashtag`(`post`, `hashtag`) VALUES ('".$post_id."','duet')");
}
if($hashtags!=""){
$hashtag = explode("#", $hashtags);
for($i=0; $i < count($hashtag); $i++) {
mysqli_query($con, "INSERT INTO `post_hashtag`(`post`, `hashtag`) VALUES ('".$post_id."','".$hashtag[$i]."')");
}
}
$res['status'] = 1;
$res['msg'] = "Post Uploaded Successfully";
echo json_encode($res);
die;
}
?>
result
{"status":0,"msg":"param missing"}
here is postman error image

Related

File path with '/' is not inserting in DB

I need to check duplicate files from a path and need to insert the file name in a table if it is new and then insert all records of it accordingly into another table. Do not insert or step forward if file already exists.
Here is my code where i can't bring the file path with '/' into the DB. Can anyone assist? Thanks in advance.
File Path with '/' is not passing using $open = fopen('$cont[$x]','r');
<?php
//include ("connection.php");
$conn = new mysqli('localhost','root','','demo');
$path = _DIR_ . DIRECTORY_SEPARATOR ."*.{txt}";
$cont = glob($path, GLOB_BRACE);
//print_r($content);
$arrlength = count($cont);
for($x = 0; $x < $arrlength; $x++){
// $sql = "INSERT INTO `file_record` (`file_name`) VALUES ('$cont[$x]') ";
$dup = mysqli_query($conn,"SELECT * FROM file_record WHERE file_name = '$cont[$x]' ");
if(mysqli_num_rows($dup)>0)
{
echo "File already Exists";
}
else {
$insert = "INSERT INTO `file_record` (`file_name`) VALUES ('$cont[$x]') ";
// $conn->query($insert);
if (mysqli_query($conn,$insert)) {
$open = fopen('$cont[$x]','r');
while (!feof($open))
{
$content = fgets($open);
$carry = explode(",",$content);
list($name,$city,$postcode,$job_title) = $carry;
$sql = "INSERT INTO `employee` (`name`, `city`, `postcode`, `job_title`) VALUES ('$name','$city','$postcode','$job_title')";
$conn->query($sql);
}
fclose($open);
echo 'inserted';
} else {
echo 'Not inserted';
}
}
}
?>

Sql issues when trying to upload using php

I'm trying to upload a video to a folder which is working but, the relevant entry in the DB isn't occurring to match it. Really having trouble seeing what's wrong, as no errors are reported.
session_start();
require 'db.php';
$name = $_FILES['video']['name'];
$uploader = $_SESSION['first_name'].$_SESSION['last_name'];
$newstring = $_SESSION['last_name'].'_'.$_SESSION['first_name'].'_'.date('ymdhms').".mp4";
$extension = strtolower(substr($name, strpos($name, '.') + 1));
$size = $_FILES['video']['size'];
$max_size = '1073741824';
$type = $_FILES['video']['type'];
$id = $_SESSION['id'];
$date = date('Y-m-D');
$tmp_name = $_FILES['video']['tmp_name'];
if(!empty($name)){
$location = "uploads/";
if($extension=='mp4'&&$type == 'video/mp4'){
if($size <= $max_size){
if(move_uploaded_file($tmp_name, $location.$newstring)){
$sql = "INSERT INTO videos (file_name, upload_by, date) VALUES
('$newstring', '$id', '$date')";
mysqli_query($mysqli, $sql);
require('profile.php');
$_SESSION['message'] = "Upload Successful!";
header('Refresh:0; url=profile.php');
}else{
$_SESSION['message'] = "File failed to upload";
header("location: error.php");
}
I'm not getting a corresponding DB entry. Any help would be really appreciated.
Try this:
$sql = "INSERT INTO videos (file_name, upload_by, date) VALUES
('".$newstring."', '".$id."', '".$date."')";

inserting record if count of column is less than two else not inserting into mysql

just want to add record if count of column is less than 2 for today's date and if count is more than two it should not get insert into the db.It's keep getting added after two records.
$user_ip = getenv('REMOTE_ADDR');
$geo = unserialize(file_get_contents("http://www.geoplugin.net/php.gp?ip=$user_ip"));
$city = $geo["geoplugin_city"];
$region = $geo["geoplugin_regionName"];
$img = $_POST['img'];
$amount = 5;
$sql = "SELECT COUNT(*) as totalupload FROM `daily_uploads` WHERE DATE_FORMAT(`date`, '%Y-%m-%d') = CURDATE()";
$row = mysqli_fetch_assoc($sql);
$sum = $row['totalupload'];
if ($sum < 2 ) {
$sql = "INSERT INTO `daily_uploads` (img, geoplugin_city, geoplugin_regionName, amount)
VALUES ('$img', '$city', '$region','$amount')";
if ($conn->query($sql)) {
echo ('success');
} else {
echo ('error');
}
} else {
echo"already exist";
make the connection after count query like this,
$result = mysqli_query($con,$sql);
Try this one hope it will help you.
$user_ip = getenv('REMOTE_ADDR');
$geo = unserialize(file_get_contents("http://www.geoplugin.net/php.gp?ip=$user_ip"));
$city = $geo["geoplugin_city"];
$region = $geo["geoplugin_regionName"];
$img = $_POST['img'];
$amount = 5;
$sql = "SELECT COUNT(*) as totalupload FROM `daily_uploads` WHERE DATE_FORMAT(`date`, '%Y-%m-%d') = CURDATE()";
$qry= mysql_query($sql);
$row = mysql_fetch_assoc($qry);
$count = $row['totalupload'];
if ($count < 2 ) {
$sql = "INSERT INTO `daily_uploads` (img, geoplugin_city, geoplugin_regionName, amount)
VALUES ('$img', '$city', '$region','$amount')";
if ($conn->query($sql)) {
echo ('success');
} else {
echo ('error');
}
} else {
echo"already exist";

Why is $result always returning me false?

I have this insert.php file that inserts data to my database but every time I submit data $result always returns false. I don't know where the problem is, its always echoing "something error".
##Insert.php
include_once("connection.php");
if (
isset($_POST['txtCarModel']) && isset($_POST['txtCarType']) &&
isset($_POST['txtCapacity']) && isset($_POST['image']) &&
isset($_POST['txtFuelType']) && isset($_POST['txtPlateNumber']) &&
isset($_POST['txtOwner'])
) {
$now = DateTime::createFromFormat('U.u', microtime(true));
$id = $now->format('YmdHis');
$upload_folder = "upload";
$path = "ftp://$upload_folder/$id.jpeg";
$image = $_POST['image'];
$fullpath = "carkila.esy.es/$upload_folder/$id.jpeg";
$Car_Model = $_POST['txtCarModel'];
$Car_Type = $_POST['txtCarType'];
$Capacity = $_POST['txtCapacity'];
$Fuel_Type = $_POST['txtFuelType'];
$PlateNumber = $_POST['txtPlateNumber'];
$Image = $_POST['image'];
$Owner = $_POST['txtOwner'];
$query = "INSERT INTO tbl_cars (Car_Model, owner, Car_Type,Capacity,fuelType,carPlatenuNumber,Image) VALUES ('$Car_Model', '$Owner', '$Car_Type',$Capacity,'$fuelType','$carPlatenuNumber','$Image')";
$result = mysqli_query($conn, $query);
if ($result > 0) {
if (isset($_POST['mobile']) && $_POST['mobile'] == "android") {
echo "success";
exit;
}
echo "Insert Successfully";
} else {
if (isset($_POST['mobile']) && $_POST['mobile'] == "android") {
echo "failed";
exit;
}
echo "Something Error";
}
$query = "INSERT INTO tbl_cars (Car_Model, owner, Car_Type,Capacity,fuelType,carPlatenuNumber,Image) VALUES ('$Car_Model', '$Owner', '$Car_Type',$Capacity,'$fuelType','$carPlatenuNumber','$Image')";
$carPlatenuNumber should be $plateNumber
Use mysqli_affected_rows ()
mysqli_query($conn, $query);
$result = mysqli_affected_rows ($conn);
It will return the number of rows affected by the previous operation.
mysqli_affected_rows

incomplete insertion from csv to mysql database php

I am facing an error while inserting data records from CSV file to mysql database. I have a script which works fine. It inserts data from CSV file to mysql database. It has around 40,000 records. But after running the script, it inserts only around 1000 records and stops without error. What may be the problem? Please help.
Below is my script for inserting data.
include('dbconnection.php');
if (($handle = fopen("hotels.csv", "r")) !== FALSE) {
$flag = true;
$id=1;
while (($data = fgetcsv($handle, 100000000, ",")) !== FALSE) {
if($flag){
$flag = false;
continue;
}
$hotelid = mysql_real_escape_string($data[0]); //var_dump($hotelid); exit();
$hotelname = mysql_real_escape_string($data[1]);
$address1 = mysql_real_escape_string($data[2]);
$address2 = mysql_real_escape_string($data[3]);
$country = mysql_real_escape_string($data[4]);
$city = mysql_real_escape_string($data[5]);
$postcode = mysql_real_escape_string($data[6]);
$telephone = mysql_real_escape_string($data[7]);
$hotelfax = mysql_real_escape_string($data[8]);
$hotelemail = mysql_real_escape_string($data[9]);
$longitude = mysql_real_escape_string($data[10]);
$latitude = mysql_real_escape_string($data[11]);
$website = mysql_real_escape_string($data[12]);
$location = mysql_real_escape_string($data[13]);
$description = mysql_real_escape_string($data[14]);
$starrating = mysql_real_escape_string($data[15]);
$statecode = mysql_real_escape_string($data[16]);
$select_query = "SELECT * FROM `hotelDB`.`hotels` WHERE `hotelid` = $hotelid";
if(mysql_query($select_query) != '$hotelid'){
$sql = "INSERT IGNORE INTO `hotelDB`.`hotels` (`id`,`hotelid`, `hotelname`, `address1`,`address2`,`country`,`city`,`postcode`,`telephone`,`hotelfax`,`hotelemail`,`longitude`,`latitude`,`website`,`location`,`description`,`starrating`,`statecode`) VALUES ('$id','$hotelid', '$hotelname', '$address1','$address2', '$country', '$city', '$postcode', '$telephone', '$hotelfax', '$hotelemail', '$longitude', '$latitude', '$website', '$location', '$description', '$starrating', '$statecode')";
echo $sql;
$retval = mysql_query($sql,$conn);
if(! $retval )
{
die('Could not enter data: ' . mysql_error());
}
echo "<p style='color: green;'>Entered data having id = " .$id. " successfully</p><br>";
$id++;
}
}
echo "<br><p style='color: orange;'>Congratulation all data successfully inserted</p>";
fclose($handle);
}
mysql_close($conn);`
The insertion stops after inserting some values. This is example screenshot where the code stops executes.
You can try one query with multiple-row INSERT. In below example I am inserting 500 row with one insert query. You can change it to 1000.
<?php
include('dbconnection.php');
if (($handle = fopen("grnconnect-dump-hotels.csv", "r")) !== FALSE) {
//var_dump( $handle);
$flag = true;
$id=1;
$counter=0;
$sql = "INSERT IGNORE INTO `hotelDB`.`hotels` (`id`,`hotelid`, `hotelname`, `address1`,`address2`,`country`,`city`,`postcode`,`telephone`,`hotelfax`,`hotelemail`,`longitude`,`latitude`,`website`,`location`,`description`,`starrating`,`statecode`) VALUES";
$saveString="";
while (($data = fgetcsv($handle, 100000000, ",")) !== FALSE) {
//var_dump($data); exit();
if($flag){
$flag = false;
continue;
}
$hotelid = mysql_real_escape_string($data[0]); //var_dump($hotelid); exit();
$hotelname = mysql_real_escape_string($data[1]);
$address1 = mysql_real_escape_string($data[2]);
$address2 = mysql_real_escape_string($data[3]);
$country = mysql_real_escape_string($data[4]);
$city = mysql_real_escape_string($data[5]);
$postcode = mysql_real_escape_string($data[6]);
$telephone = mysql_real_escape_string($data[7]);
$hotelfax = mysql_real_escape_string($data[8]);
$hotelemail = mysql_real_escape_string($data[9]);
$longitude = mysql_real_escape_string($data[10]);
$latitude = mysql_real_escape_string($data[11]);
$website = mysql_real_escape_string($data[12]);
$location = mysql_real_escape_string($data[13]);
$description = mysql_real_escape_string($data[14]);
$starrating = mysql_real_escape_string($data[15]);
$statecode = mysql_real_escape_string($data[16]);
if(trim($counter)=="500") {
$sql = $sql.''.rtrim($saveString,",");
echo $sql;echo "<br /";
mysql_query($sql);
$counter=0;
$saveString="";
}
$saveString .="('".$id."','".$hotelid."', '".$hotelname."', '".$address1."','".$address2."', '".$country."', '".$city."', '".$postcode."', '".$telephone."', '".$hotelfax."', '".$hotelemail."', '".$longitude."', '".$latitude."', '".$website."', '".$location."', '".$description."', '".$starrating."', '".$statecode."'),";
$counter++;
}
if(trim($saveString)!=='') {
$sql = $sql.''.rtrim($saveString,",");
echo $sql;echo "<br /";
mysql_query($sql);
}
echo "<br><p style='color: orange;'>Congratulation all data successfully inserted</p>";
fclose($handle);
}
mysql_close($conn);
?>
Is there a particular reason to insert via script ?
Else I'd suggest you just insert manually or let the DB do it, sinces it's more efficient anyway. MySQL provides functions
for this: http://www.mysqltutorial.org/import-csv-file-mysql-table/

Categories