So I'm trying to get the max id of a table which I can do using
SELECT * FROM forsale ORDER BY StockID DESC LIMIT 0,1
I then save the result so I can use it in another table for reference when displaying images. The only issue is when I print the result it shows the MAX id but no inputting it into the table? Does anyone have an suggestions? I have code prior to this inputting into the forsale table and I am then getting the ID of that record. Here's what I get in the table, here's the code:
if(isset($_POST['add'])){
include '../Login-System/db.php';
$make = mysqli_real_escape_string($conn, $_POST['Make']);
$model = mysqli_real_escape_string($conn, $_POST['Model']);
$variant = mysqli_real_escape_string($conn, $_POST['Variant']);
$year = mysqli_real_escape_string($conn, $_POST['Year']);
$mileage = mysqli_real_escape_string($conn, $_POST['Mileage']);
$fuel = mysqli_real_escape_string($conn, $_POST['Fuel']);
$doors = mysqli_real_escape_string($conn, $_POST['Doors']);
$trans = mysqli_real_escape_string($conn, $_POST['transmission']);
$enginesize = mysqli_real_escape_string($conn, $_POST['Enginesize']);
$price = mysqli_real_escape_string($conn, $_POST['Price']);
$description = mysqli_real_escape_string($conn, $_POST['description']);
$makeupper = strtoupper($make);
$modelupper =strtoupper($model);
$variantupper =strtoupper($variant);
$sqlcarinsert = "INSERT INTO forsale (make, model, variant, year, mileage, fuel, doors, trans, enginesize, price, description) VALUES ('$makeupper','$modelupper','$variantupper','$year','$mileage','$fuel','$doors','$trans','$enginesize','$price','$description');";
//Image Upload
//Find next StockID
$sql = "SELECT * FROM forsale ORDER BY StockID DESC LIMIT 0, 1";
$result = mysqli_query($conn, $sql);
$stockIDtable = mysqli_fetch_assoc($result);
$stockID = $stockIDtable['StockID'];
if(!empty($_FILES['files']['name'][0])){
$files = $_FILES['files'];
//File Extensions allowed
$allowed = array('jpg', 'jpeg', 'png');
foreach ($files['name'] as $position => $file_name) {
$file_tmp = $files['tmp_name'] [$position];
$file_size = $files['size'] [$position];
$file_error = $files['error'] [$position];
//Order
$orderimg = $position;
//Get file extension
$FileExt = explode('.', $file_name);
$endext = end($FileExt);
$fileActualExt = strtolower($endext);
if (in_array($fileActualExt, $allowed)) {
//Checks for Errors in uploading
if ($file_error === 0) {
//New name to remove possibilities of duplicates
$fileNameNew = uniqid('', true).".".$fileActualExt ;
$FileDestination = '../Photos/forsale/'.$fileNameNew;
$SQLDestination = 'Photos/forsale/'.$fileNameNew;
//Upload to Designated folder with name
move_uploaded_file($file_tmp, $FileDestination);
//Insert into forsaleimg
$sqlimginsert = "INSERT INTO forsaleimg (id, StockID, imgOrder, FileDestination) VALUES ('NULL', '$stockID', '$orderimg', '$SQLDestination');";
mysqli_query($conn, $sqlimginsert);
//echo "<pre>";
//print_r($sqlimginsert);
//echo "</pre>";
$orderimg++ ;
} else {
header("Location: ../salelist.php?upload=error");
exit();
}
} else {
header("Location: ../salelist.php?fucked");
exit();
}
}
}
mysqli_query($conn, $sqlcarinsert);
header("Location: ../salelist.php?added=".$make);
exit();
} else {
header("Location: ../salelist.php?add=notclicked");
exit();
}
Use the query as:-
$sql = "select * from forsale
where StockID = (select max(StockID) as 'StockID'
from forsale)
order by StockID" ;
Related
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
Back again! So I'm trying to upload multiple images to a server and save the path and corresponding stock number in the database. Currently I have the car details, $sqlcarinsert, working and inserting into the relevant database, forsale.
The issue arises when I try save all of the image details. I have the images being given new names and uploading to the server but not being inputted into the database with the stockID, which links it to the advert, as well as the filepath. Along with this only one of the files is being inputted into the database not say all three that uploaded to the server. Is this possible to do?
Need anything else let me know I shall try my best to provide!
P.S Yes I know prepared statements, shoody code etc etc..
<?php
if(isset($_POST['add'])){
include '../Login-System/db.php';
$make = mysqli_real_escape_string($conn, $_POST['Make']);
$model = mysqli_real_escape_string($conn, $_POST['Model']);
$variant = mysqli_real_escape_string($conn, $_POST['Variant']);
$year = mysqli_real_escape_string($conn, $_POST['Year']);
$mileage = mysqli_real_escape_string($conn, $_POST['Mileage']);
$fuel = mysqli_real_escape_string($conn, $_POST['Fuel']);
$doors = mysqli_real_escape_string($conn, $_POST['Doors']);
$trans = mysqli_real_escape_string($conn, $_POST['transmission']);
$enginesize = mysqli_real_escape_string($conn, $_POST['Enginesize']);
$price = mysqli_real_escape_string($conn, $_POST['Price']);
$description = mysqli_real_escape_string($conn, $_POST['description']);
$sqlcarinsert = "INSERT INTO forsale (make, model, variant, year, mileage, fuel, doors, trans, enginesize, price, description) VALUES ('$make','$model','$variant','$year','$mileage','$fuel','$doors','$trans','$enginesize','$price','$description');";
//Image Upload
//Find next StockID (Surely not, right?)
$sql = "SELECT * FROM forsale ;";
$result = mysqli_query($conn, $sql);
$rowcount = mysqli_num_rows($result);
$stockID = $rowcount + 1;
if(!empty($_FILES['files']['name'][0])){
$files = $_FILES['files'];
//File Extensions allowed
$allowed = array('jpg', 'jpeg', 'png');
foreach ($files['name'] as $position => $file_name) {
$file_tmp = $files['tmp_name'] [$position];
$file_size = $files['size'] [$position];
$file_error = $files['error'] [$position];
//Get file extension
$FileExt = explode('.', $file_name);
$endext = end($FileExt);
$fileActualExt = strtolower($endext);
if (in_array($fileActualExt, $allowed)) {
//Checks for Errors in uploading
if ($file_error === 0) {
include '../Login-System/db.php';
//New name to remove possibilities of duplicates
$fileNameNew = uniqid('', true).".".$fileActualExt ;
$FileDestination = '../Photos/forsale/'.$fileNameNew;
//Upload to Designated folder with name
move_uploaded_file($file_tmp, $FileDestination);
//Insert into forsaleimg
$sqlimginsert = "INSERT INTO forsaleimg (StockID, FileDestination) VALUES ('$stockID','$FileDestination');";
mysqli_query($conn, $sqlimginsert);
} else {
header("Location: ../salelist.php?upload=error");
exit();
}
}
}
}
mysqli_query($conn, $sqlcarinsert);
header("Location: ../salelist.php?added=".$make);
exit();
} else {
header("Location: ../salelist.php?add=notlcicked");
exit();
}
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."')";
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";
I'm getting data from excel sheet and storing it in database. It is storing the data successfully. I had created two tables in database, one is to import values from excel sheet & store temporarily and other is to get values from temporarily stored table,also checking for duplicate records.
I can insert data in second table but please suggest me how to check duplicate records before storing in original table.
This is the code:
<?php
if(isset($_POST["submit"]))
{
$file = $_FILES['file']['tmp_name'];
$handle = fopen($file, "r");
$c = 0;
$row = 1;
while(($filesop = fgetcsv($handle, 1000, ",")) !== false)
{
$row++;
if($row != 1)
{
$custid = $filesop[0];
$zone = $filesop[1];
$city = $filesop[2];
$category = $filesop[3];
$focus = $filesop[4];
$customer_type = $filesop[5];
$lead_source = $filesop[6];
$exhibition = $filesop[7];
$organization_name = $filesop[8];
$assign_to = $filesop[9];
$description = $filesop[10];
$division = $filesop[11];
$product = $filesop[12];
$grade = $filesop[13];
$potential = $filesop[14];
$firstname = $filesop[15];
$lastname = $filesop[16];
$designation = $filesop[17];
$mobile = $filesop[18];
$primary_phone = $filesop[19];
$primary_email = $filesop[20];
$address_type = $filesop[21];
$address1 = $filesop[22];
$address2 = $filesop[23];
$state = $filesop[24];
$country = $filesop[25];
$hgf = "INSERT INTO temp_const set custid='$name', zone='$zone', city='$city', category='$category', focus='$focus', customer_type='$customer_type', lead_source='$lead_source', exhibition='$exhibition', organization_name='$organization_name', assign_to='$assign_to', description='$description', division='$division', product='$product', grade='$grade', potential='$potential', firstname='$firstname', lastname='$lastname', designation='$designation', mobile='$mobile', primary_phone='$primary_phone', primary_email='$primary_email', address_type='$address_type', address1='$address1', address2='$address2', state='$state', country='$country' ";
$sql = mysql_query($hgf);
$c = $c + 1;
}
}
if($sql){
echo "You database has imported successfully. You have inserted ". $c ."recoreds";
}else{
echo " Sorry! There is some problem.";
}
}
?>
You can create a unique index on the fields where you want to check on duplication, lets say it is fieldA, fieldB and fieldC
ALTER TABLE destTable add unique key idx_abc (fieldA,fieldB,fieldC);
after this you can copy your tmp table into the destTable with the option IGNORE
INSERT IGNORE INTO destTable SELECT * FROM temp_const;
or use only the field you want.