我不明白为什么从 Php 返回 Json 对象后会显示错误消息。看了很多遍代码还是搞不懂
例如:
1.成功插入采购订单后,我收到消息“No value for messagesucc”,而不是 $poid,即 mysql_insert_id() 值
2.当没有产品发布时,预期的消息是“未提交采购订单”,但我得到“消息成功没有价值”
以及许多类似的不匹配错误。
php代码
<?php
//acceptporder.php
//to accept the purchase order
include("login.php");
include("functions.php");
// array for JSON response
$retJson = array();
$conn= mysql_connect($hname, $uname, $pass) or die (mysql_error());
mysql_select_db($dbase, $conn) or die (mysql_error());
$atleastone=0; //To make sure atleast one product was registered
$custid=clean($_POST['custid']);
$smid=clean($_POST['smid']);
$query="SELECT custid FROM custinfo WHERE custid='$custid'";
$res = mysql_query($query,$conn) or die(mysql_error());
$rows= mysql_num_rows($res);
if($rows>0)
{
$mustcommit=1; //Flag to indicate committing
$timestamp=time();
$odate=date("Y-m-d",$timestamp); //Date of Purchase Order
$otime=date("H:i:s",$timestamp); //Time of Purchase Order
$query="SELECT poid FROM purchaseordermaster WHERE orderdate='$odate' AND custid='$custid'";
$res=mysql_query($query, $conn) or die(mysql_error());
$rows=mysql_num_rows($res);
if($rows>0)
{
$retJson["success"] = 0;
$retJson["messagefail"] = "You have already submitted your Purchase Order for the day";
}
else
{
$query="SET AUTOCOMMIT=0"; //As we deal with transaction involving multiple tables.
mysql_query($query, $conn) or die(mysql_error());
$query="BEGIN";
mysql_query($query, $conn) or die(mysql_error());
//Create the master record
$querymaster="INSERT INTO purchaseordermaster (custid,smid,orderdate,ordertime) VALUES('$custid','$smid','$odate','$otime')";
$r1=mysql_query($querymaster, $conn) or die(mysql_error());
$poid=mysql_insert_id($conn);
if($r1)
{
$query="SELECT productid, productname FROM productinfo WHERE status=1 ORDER BY category";
$res=mysql_query($query, $conn) or die(mysql_error());
$rows=mysql_num_rows($res);
for($j=0; $j<$rows; $j++)
{
$thisrow=mysql_fetch_assoc($res);
$pid=$thisrow['productid']; //Take productid from productinfo table
$cases=clean($_POST["$pid"]); //Use the product id to retrieve the posted variable
if($cases==0)
{
continue;
}
$atleastone=1;
//Update the slave table of purchase order
$queryslave="INSERT INTO purchaseorderslave (poid,productid,cases) VALUES ($poid,'$pid',$cases)";
$r2=mysql_query($queryslave, $conn) or die(mysql_error());
if(!$r2)
{
$mustcommit=0;
break;
}
}
}
else
{
$retJson["success"] = 0;
$retJson["messagefail"] ="Error Updating Purchase Order Master Table";
$mustcommit=0;
}
if($mustcommit==1 && $atleastone>0)
{
$query="COMMIT";
mysql_query($query, $conn) or die(mysql_error());
$retJson["success"] = 1;
$retJson["messagesucc"] = "Purchase Order ID: $poid";
}
else
{
$query="ROLLBACK";
mysql_query($query, $conn) or die(mysql_error());
$retJson["success"] = 0;
$retJson["messagefail"] = "Purchase Order Has Not Been Submitted";
if($atleastone==0)
{
$retJson["messagefail"] = "No products could be ordered";
}
}
$query="SET AUTOCOMMIT=1";
mysql_query($query, $conn) or die(mysql_error());
}
echo json_encode($retJson); //Json encode and return
}
else //invalid custid
{
$retJson["success"] = 0;
$retJson["messagefail"] = "Check Customer ID";
echo json_encode($retJson); //Json encode and return
}
mysql_close($conn);
?>
清洁功能
function clean($var)
{
$var = strip_tags($var);
$var = htmlentities($var);
$var = stripslashes($var);
return mysql_real_escape_string($var);
}
使用的表
#Purchase Order Table purchaseordermaster
create table purchaseordermaster(
poid bigint key auto_increment,
custid varchar(18) not null,
smid varchar(18) not null,
orderdate varchar(12) not null,
ordertime varchar(15) not null,
consider tinyint not null default 0);
#Purchase Order Table purchaseorderslave
create table purchaseorderslave(
poid bigint not null,
productid varchar(18) not null,
cases mediumint not null,
primary key(poid,productid),
foreign key(poid) references purchaseordermaster(poid) on delete cascade);
部分 Android 代码存在于 --> 类 MakePO 扩展 AsyncTask
for(int i=0; i<totalprod; i++)
{
HashMap<String, String> map = new HashMap<String, String>();
map=productsList.get(i); //Get the map from arrayList at required position
pid=map.get("productid");
cases=map.get("cases");
if(Integer.parseInt(cases) == 0) //Only Send Cases More Than 0
{
continue;
}
params.add(new BasicNameValuePair(pid, cases));
}
//Contacting the server and getting the result
JSONObject json = pj.makeHttpRequest(link_acceptporder,"POST", params);
try
{
int success = json.getInt("success");
if (success == 1)
{
s = json.getString("messagesucc");
}
else
{
s = json.getString("messagefail");
}
}
catch (JSONException e)
{
s = e.getMessage();
}
return s;
感谢您阅读这篇文章。感谢任何帮助或提示(正面或负面)