I'm working on an android app where i'm supposed to create one recipe including it's picture. I have one php file called create_recipe.php, in that file i'm creating the title ingredients, description and category, all these values are created in a single query. Also, I have a second php file called UploadImage.php to pick a photo from the gallery or from camera and upload it to the web server, and this is done in a single query as well. In my java code i'm calling create_recipe.php first and then UploadImage.php. Doing it like this will make the information to be saved in a deferent rows
I tried several thing to make it one query so that all the information are together: 1- Merging the files to be one file, and only making one query the result was: every thing was added but not the image, because UploadImage.php wasn't called yet. 2- Saving the value of image and add it to the create_recipe.php, didn't work again because UploadImage.php wasn't called yet. 3-Since create_recipe.php is called before UploadImage.php and i know that there is an id for each recipe with auto increment value, I tried to call it by mysqli_insert_id ( ) and then update the rows with the image but again that didn't work.
My question is is there any way to make this query in a single row in the DB ? Any help would be greatly appreciated!
Here is my code for UploadImage.php
<?php
$target_path = "./images".basename( $_FILES['uploadedfile']['name']);
$pic = ($_FILES['photo']['name']);
$file_path = $_FILES['tmp_name'];
// include db connect class
define('__ROOT__', dirname(dirname(__FILE__)));
require_once(__ROOT__.'/android_connect/db_connect.php');
// connecting to db
$db = new DB_CONNECT();
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name'])." has been uploaded";
// Make your database insert only if successful and insert $target_path, not $file_path
// Use mysqli_ or PDO with prepared statements
$result = mysql_query("INSERT INTO scb( name) VALUES('$target_path')");// here i'm making the query to add the image
}
else
echo "There was an error uploading the file, please try again!";
?>;
Here is my code for create_recipe.php
<?php
/*
* Following code will create a new recipe row
* All recipe details are read from HTTP Post Request
*/
// array for JSON response
$response = array();
// check for required fields
if (isset($_POST['title']) && isset($_POST['ingredients']) && isset($_POST['description'])&& isset($_POST['category']) ) {
// && isset($_POST['image'])
$title = $_POST['title'];
$ingredients = $_POST['ingredients'];
$description = $_POST['description'];
$category = $_POST['category'];
// include db connect class
define('__ROOT__', dirname(dirname(__FILE__)));
require_once(__ROOT__.'/android_connect/db_connect.php');
// connecting to db
$db = new DB_CONNECT();
// mysql inserting a new row
$result = mysql_query("INSERT INTO scb(title, ingredients, description, category, name) VALUES('$title', '$ingredients', '$description', '$category', '$target_path')");//
// check if row inserted or not
if ($result) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "Recipe successfully created.";
// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
0 comments:
Post a Comment