我正在开发一个Android应用程序,我应该创建一个包含其图片的配方。我有一个名为create_recipe.php
的php文件,在该文件中我创建了标题,成分,描述和类别 - 所有这些值都是在一个查询中创建的。另外,我有一个名为UploadImage.php
的第二个php文件,用于从图库或相机中选择一张照片并将其上传到网络服务器,这也可以在一个查询中完成。在我的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);
}
答案 0 :(得分:1)
一个简单的更新查询就可以了!
$result = mysql_query("UPDATE scb SET name = '$target_path' ORDER BY id DESC LIMIT 1");