Skip to content

Instantly share code, notes, and snippets.

@brucenorton
Created November 13, 2018 21:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brucenorton/c0029f1e031555601cf71fe00ce90c11 to your computer and use it in GitHub Desktop.
Save brucenorton/c0029f1e031555601cf71fe00ce90c11 to your computer and use it in GitHub Desktop.
gallery_uploader insert.php
<?php
require_once('../_includes/connect.php');
//should uploading the file be dependent on a successful insert? or vice-versa?
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
//get posted form data
$userID = isset($_REQUEST['userID']) ? $_REQUEST['userID'] : '1';
$title = isset($_REQUEST['title']) ? $_REQUEST['title'] : 'not_set';
$src = isset($_FILES['fileInput']) ? $_FILES['fileInput']['name'] : 'not_set';
$caption = isset($_REQUEST['caption']) ? $_REQUEST['caption'] : 'not_set';
//upload.php
// *** chmod of 'uploads' folder to 777 ***
//create an array for the results
$jsonResponse = [ "file" => "upload.php" ];
if (isset($_FILES['fileInput'])) {
$jsonResponse["file"] = $_FILES['fileInput']['name'];
if(move_uploaded_file($_FILES['fileInput']['tmp_name'], "../photos/" . $_FILES['fileInput']['name'])){
//echo $_FILES['fileInput']['name']. " OK";
$jsonResponse["response"] = "success";
$jsonResponse["file"] = $_FILES['fileInput']['name'];
//insertDB($_FILES['fileInput']['name']);
} else {
//echo $_FILES['fileInput']['name']. " KO";
$jsonResponse["response"] = "fail";
}
//echo(json_encode($jsonResponse));
//exit;
} else {
$jsonResponse["response"] = "fail no file";
//echo(json_encode($jsonResponse));
}
//echo("response: $userID, $title, $src, $caption")
$tbl = "photos";//change to your table i.e. John_app
//write query
$query = "INSERT INTO $tbl (userID, title, src, caption) VALUES (?,?,?,?)";
//prepare statement, execute, store_result
if($insertStmt = $mysqli->prepare($query)){
$insertStmt->bind_param("isss", $userID, $title, $src, $caption);
$insertStmt->execute();
$insertRows = $insertStmt->affected_rows;
$insertID = $mysqli->insert_id;
}else{
$jsonResponse["response"] = "error";
$jsonResponse["messageError"] = "$insertStmt->error $mysqli->error";
}
//if the info got inserted
if($insertRows > 0){
$jsonResponse["response"] = "success";
$jsonResponse["messageSuccess"] = "$insertRows inserted";
$jsonResponse["insertID"] = $insertID;
$jsonResponse["src"] = $src;
}else{
$jsonResponse["response"] = "error";
$jsonResponse["messageError"] = "else error $insertStmt->error $mysqli->error";
}
$insertStmt->close();
$mysqli->close();
echo(json_encode($jsonResponse));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment