php - 返回意外消息的 JSON(或 PHP)

标签 php android mysql json

我不明白为什么从Php返回Json对象后显示错误信息。翻了很多遍代码,还是想不通

例如:

1.当采购订单成功插入时,我得到的消息是“No value for messagesucc”,而不是 $poid,它是 mysql_insert_id() 的值

2.当没有发布任何产品时,预期的消息是“采购订单尚未提交”,但我得到“消息成功没有值(value)”

还有许多类似的不匹配错误。

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 代码出现在 --> 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;

感谢您浏览这篇文章。感谢任何帮助或提示(正面或负面)

最佳答案

对于 1.,您能否尝试逐步执行: - 在实例化后获取 $poid 的值 - 在$retJson["success"]设置为1的条件之前获取$mustcommit和$atleastone的值

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

最好的问候,

关于php - 返回意外消息的 JSON(或 PHP),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15364815/

相关文章:

php - 来自数据库的 URL 和链接文本

PHP if 语句不适用于所有有效数字

java - POST 时传递到持久化的分离实体

mysql - SQL 从两个不同的表中添加和减去数据

PHP 博客条目未提交到 MySQL 数据库

php - 删除行结果 SQL 查询

Android- ScrollView : change layout position by dragging

Android 集成 AdWhirl 和 AdMob

Android:使用文本文件还是XML文件存储静态数据效率更高

mysql - 如何根据可用性从两个表之一查询数据