【问题标题】:Adding 2 queries from 2 php files together将来自 2 个 php 文件的 2 个查询加在一起
【发布时间】:2014-11-02 18:55:05
【问题描述】:

我正在开发一个 Android 应用程序,我应该在其中创建一个包含其图片的食谱。我有一个名为create_recipe.php 的php 文件,在该文件中我创建了标题、成分、描述和类别——所有这些值都是在一个查询中创建的。此外,我还有一个名为UploadImage.php 的第二个php 文件,用于从画廊或相机中挑选一张照片并将其上传到Web 服务器,这也是在单个查询中完成的。在我的 java 代码中,我先调用create_recipe.php,然后调用UploadImage.php。这样做会将信息保存在不同的行中。

有什么方法可以在数据库的单行中进行此查询?任何帮助将不胜感激!

这是我的 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);
}

【问题讨论】:

    标签: php android mysql


    【解决方案1】:

    一个简单的更新查询就可以解决问题!

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-08
      • 2017-09-28
      • 1970-01-01
      • 1970-01-01
      • 2013-07-04
      • 2019-05-15
      相关资源
      最近更新 更多