JSON(或PHP)返回意外消息

时间:2013-03-12 15:04:36

标签: php android mysql json

我不明白为什么在从Php返回Json对象后显示错误消息。经过代码多次,仍然无法弄清楚

例如:

1.当成功插入采购订单时,我收到消息“没有值为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代码存在于 - &gt; 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;

感谢您阅读本文。任何帮助或提示(正面或负面)赞赏

1 个答案:

答案 0 :(得分:0)

对于1.,您可以尝试一步到位: - 在实例化后得到$ poid的值 - 在$ retJson [“success”]设置为1的条件之前获取$ mustcommit和$ atleastone的值

对于2.,您似乎尝试从messagesucc获取值,而在messagefail中设置消息“未提交采购订单”。

最诚挚的问候,

相关问题