我在android中编写用于php响应的json文件 [英] where i write json file for php response in android
问题描述
嗨朋友们,
我试图构建一个执行基本CRUD操作的应用程序。此过程从远程数据库执行。我使用了wamp服务器。我有4个php文件和4个json文件。我不知道如何为相应的php文件编写json文件。在wamp服务器中,在www文件夹中,我有所有PHP文件但我可以在哪里编写json文件。例如,
这是PHP文件:
< pre lang =php> <?php
// < span class =code-comment> JSON响应数组
$ response = array();
// 检查必填字段
if (isset($ _ POST [' name']) && isset($ _ POST [' price'])&& isset($ _ POST [' description'])){
$ name = $ _POST [ ' name'];
$ price = $ _POST [' price'];
$ description = $ _POST [' description'];
// 包括db connect class
require_once __DIR__。 ' / db_connect.php';
// 连接到db
$ db = new DB_CONNECT();
// 插入新行的mysql
$ result = mysql_query( INSERT INTO产品(名称,价格,描述)VALUES('$ name','$ price',' $描述')跨度>);
// 检查行是否插入
if( $ result $ {b $ b // 已成功插入数据库
$ response [< span class =code-string> success] = 1 ;
$ response [ message] = 产品已成功创建。;
// 回显JSON响应
echo json_encode($ response) );
} 其他 {
// 未能插入行
$ response [ success] = < span class =code-digit> 0 ;
$ response [ message] = 糟糕!发生错误。;
// 回显JSON响应
echo json_encode($ response) );
}
} 其他 {
// 必填字段缺失
$ response [ success] = 0 ;
$ response [ message] = 缺少必填字段;
// 回显JSON响应
echo json_encode($ response) );
}
?>
对应json for thr高于PHP:
{
成功:0,
message:必填字段缺失
}
{
成功:1,
消息:产品已成功创建。
}
{
成功:0,
消息:糟糕!发生错误。
}
我还有3个php和json file.pls帮我解决了这个问题。谢谢提前..
response = array();
// 检查必填字段
if (isset(
_POST [' name'])&& isset(
_POST [' price'])&& isset(
Hi friends,
I tried to build a app which is perform basic CRUD operation. This process performed from remote database. I used wamp server for this.I have 4 php file and 4 json files. i don't know how to write json file for the corresponding php file. In wamp server, inside www folder, i have all PHP files but where i can write the json files. for example,
This is the PHP file:
<?php
// array for JSON response
$response = array();
// check for required fields
if (isset($_POST['name']) && isset($_POST['price']) && isset($_POST['description'])) {
$name = $_POST['name'];
$price = $_POST['price'];
$description = $_POST['description'];
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// mysql inserting a new row
$result = mysql_query("INSERT INTO products(name, price, description) VALUES('$name', '$price', '$description')");
// check if row inserted or not
if ($result) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "Product 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);
}
?>
corresponding json for thr above PHP:
{
"success": 0,
"message": "Required field(s) is missing"
}
{
"success": 1,
"message": "Product successfully created."
}
{
"success": 0,
"message": "Oops! An error occurred."
}
Still i have 3 more php and json file.pls help me to solve this issue.Thanks in advance..
response = array(); // check for required fields if (isset(
_POST['name']) && isset(
_POST['price']) && isset(
这篇关于我在android中编写用于php响应的json文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!