Json オブジェクトが Php から返された後に間違ったメッセージが表示される理由がわかりません。コードを何度も調べましたが、まだわかりません
例えば:
1.発注書が正常に挿入されると、mysql_insert_id() 値である $poid の代わりに「メッセージの値がありません」というメッセージが表示されます。
2.商品が投稿されていない場合、予想されるメッセージは「注文書が送信されていません」ですが、「メッセージの値がありません」と表示されますsucc
そして、多くの同様の不一致エラー。
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;
この投稿をご覧いただきありがとうございます。任意のヘルプまたはヒント (肯定的または否定的) 感謝