2014-11-02 35 views
2

我正在開發一個android應用程序,我應該創建一個包含其圖片的配方。我有一個名爲create_recipe.php的php文件,在該文件中,我創建了標題,成分,描述和類別 - 所有這些值都是在單個查詢中創建的。另外,我還有第二個名爲UploadImage.php的php文件,可以從相冊或相機中選擇一張照片並將其上傳到網絡服務器,並且這也可以在單個查詢中完成。在我的java代碼中,我首先調用create_recipe.php,然後調用UploadImage.php。這樣做會將信息保存在不同的行中。將來自2個php文件的2個查詢加在一起

有沒有什麼辦法讓這個查詢在數據庫的單個行中?任何幫助將不勝感激!

這裏是我的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 

    // here I'm making the query to add the image 
    $result = mysql_query("INSERT INTO scb(name) VALUES('$target_path')"); 
} else 
    echo "There was an error uploading the file, please try again!"; 

?>; 

代碼這裏是我的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); 
} 

回答

1

一個簡單的更新查詢會做的伎倆代碼!

$result = mysql_query("UPDATE scb SET name = '$target_path' ORDER BY id DESC LIMIT 1");