如何将对象放入字符串并打印

时间:2018-07-10 17:13:26

标签: php jquery json

我有一个脚本,该脚本从html表单获取变量并将其发送到php脚本。我根据这些数字查询新数据,并将其格式化为字符串以发送回脚本。问题是我的php变量没有打印,我认为这是因为它们是对象。这是我的代码:

//GET VENDOR PO NUMBER AND APPEND ONCHANGE OF # OF EXISTING POS
            $('#numvendpo').mouseover(function(){
            var countpre = $(this).val();
            var p = $('#pro').val();
            var c = $('#custponumhold').val();
            var v = $('#vendorid').val();
            var cp = (parseInt(countpre)+1);
            var data_String;
             data_String = 'p='+p+'&c='+c+'&v='+v+'&cp='+cp;
             $.post('ft-final-v-po-num.php',data_String,function(data){
                   var data = jQuery.parseJSON(data);
                   $('#vendponum').val(data);
                });
            });

然后我将值发布到此php脚本:

<?php
require "../inc/dbinfo.inc";

$p = $_POST['p'];
$c =$_POST['c'];
$v = $_POST['v'];
$cp = $_POST['cp'];

if ($c == 'null') { //cant use (!$customerpo) because $customerpo is passing the string of 'null' instead of the actual null value              
$c = NULL;         //so we change that to the actual null value
}

$getprojectnum = "SELECT ProjectNumber FROM tblProjects WHERE PROJECTNOID = '$p'"; //check
$getcustomerpo = "SELECT SequentialPONum FROM tblCustomerPOs WHERE CustomerPOID = '$c'"; //check
$getvendornum = "SELECT VendorNumber FROM tblVendors WHERE VENDORID = '$v'"; //check

$acpnhold = $conn->query($getprojectnum);
$accphold = $conn->query($getcustomerpo);
$acvnhold = $conn->query($getvendornum);

$acpn = mysqli_fetch_object($acpn);
$accp = mysqli_fetch_object($accp);
$acvn = mysqli_fetch_object($acvn);

if($c){
  $string = $acpn.'-'.$accp.'-'.$acvn.'-'.$cp;
  echo json_encode($string);
  exit();
}elseif(!$c){
  $string = $acpn.'-'.$acvn.'-'.$cp;
  echo json_encode($string);
  exit();
}else{
  echo json_encode('Error');
  exit();
}
?>

我网页上的响应是---2,而不是(例如:18000-1-2-2)。如前所述,我认为这是因为它们是对象,但我不太确定。任何建议表示赞赏。

2 个答案:

答案 0 :(得分:2)

您的问题在于:

$acpn = mysqli_fetch_object($acpn);
$accp = mysqli_fetch_object($accp);
$acvn = mysqli_fetch_object($acvn);

从php文档中:

  

对象mysqli_fetch_object(mysqli_result $ result [,字符串$ class_name =“ stdClass” [,数组$ params]])

您的$acpn $accp$acvn不是结果对象。在您在这些调用中使用它们之前,甚至没有定义它们。

这应该从每个查询结果中获得一列:

$acpn = $acpnhold->fetch_row()[0];
$accp = $accphold->fetch_row()[0];
$acvn = $acvnhold->fetch_row()[0];

请记住,原始查询调用仍然存在一个主要的SQL注入漏洞。

答案 1 :(得分:0)

请尝试以下代码:

    $.ajax({
    type: 'POST',
    url: 'ft-final-v-po-num.php',
    data: { 
        'p': p, 
        'c': c, 
        'v': v, 
        'cp': cp
    },
    success: function(msg){
        var data = jQuery.parseJSON(data);
        $('#vendponum').val(data);
    }
});