duanjia7607 2013-03-12 15:04
浏览 34

JSON(或PHP)返回意外消息

I do not understand why wrong messages are being displayed after Json object is returned from Php. Went through the code many times, still can't figure out

For Example:

1.When the purchase order is successfully inserted, i get the message "No value for messagesucc" instead of the $poid which is mysql_insert_id() value

2.When no products have been posted, the expected message is "Purchase Order Has Not Been Submitted" but i get "No value for messagesucc"

And many similar mismatch errors.

The php code

<?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);
?>

The clean function

function clean($var) 
{
    $var = strip_tags($var);
    $var = htmlentities($var); 
    $var = stripslashes($var);
    return mysql_real_escape_string($var);
}

Tables Used

#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);

Part of Android Code Present In --> class MakePO extends 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;

Thank You For Going Through This Post. Any Help or Tips (positive or negative) Appreciated

  • 写回答

1条回答 默认 最新

  • doushichun9409 2013-03-12 15:54
    关注

    For the 1., could you try step to step : - to get the value of $poid after its instanciation - to get the value of $mustcommit and $atleastone before the condition in which $retJson["success"] is set to 1

    For the 2., you seem to try to get a value from messagesucc whereas the message "Purchase Order Has Not Been Submitted" is set in messagefail.

    Best Regards,

    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向