【问题标题】:autocomplete address form cross two tables跨两个表的自动完成地址表格
【发布时间】:2014-08-15 00:38:47
【问题描述】:

这里是新手,需要帮助。

我使用来自http://www.jensbits.com/2010/03/29/jquery-ui-autocomplete-widget-with-php-and-mysql/的代码

它适用于某些领域。我的问题是我将国家和州存储在另一个表中,并且在“if ($address_query->num_rows) {”周围出现“尝试获取非对象的属性”错误。

autocomplete.php

$dbhost = 'SERVER';
$dbuser = 'USERNAME';
$dbpass = 'PASSWORD';
$dbname = 'DATABASE_NAME';

try {
  $conn = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
}
catch(PDOException $e) {
    echo $e->getMessage();
}

$return_arr = array();
if ($conn)
{
$ac_term = "%".$_GET['term']."%";
$address_query = "SELECT * FROM address where customer_id = '" . $customer_id . "' AND (firstname like :term OR lastname like :term) LIMIT 10";
if ($address_query->num_rows) { // got "Trying to get property of non-object" error
        $country_query = "SELECT * FROM country WHERE country_id = '" . (int)$address_query->row['country_id'] . "'";

if ($country_query->num_rows) {
            $country = $country_query->row['name'];
            $iso_code_2 = $country_query->row['iso_code_2'];
        } else {
            $country = '';
            $iso_code_2 = '';
        }

        $zone_query = "SELECT * FROM zone WHERE zone_id = '" . (int)$address_query->row['zone_id'] . "'";

        if ($zone_query->num_rows) {
            $zone = $zone_query->row['name'];
            $zone_code = $zone_query->row['code'];
        } else {
            $zone = '';
            $zone_code = '';
        }   
$result = $conn->prepare($address_query);
$result->bindValue(":term",$ac_term);
$result->execute();

/* Retrieve and store in array the results of the query.*/
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
    $return_arr [] = array(

    'label' => $row['firstname'] .' '. $row['lastname'] .' '. $row['company'] .' '.         $row['city'] .' '. $row['postcode'],
  'value' => $row['firstname'] .' '. $row['lastname'],
  'Company' => $row['company'],
  'Country' => $row['iso_code_2'],
  'postCode' => $row['postcode'],

   );
}


}

echo json_encode($return_arr);
flush();
?>

如果我删除代码

if ($query->num_rows) { // got "Trying to get property of non-object" error
        $country_query = "SELECT * FROM country WHERE country_id = '" . (int)$address_query->row['country_id'] . "'";

if ($country_query->num_rows) {
            $country = $country_query->row['name'];
            $iso_code_2 = $country_query->row['iso_code_2'];
        } else {
            $country = '';
            $iso_code_2 = '';
        }

        $zone_query = "SELECT * FROM zone WHERE zone_id = '" . (int)$address_query->row['zone_id'] . "'";

        if ($zone_query->num_rows) {
            $zone = $zone_query->row['name'];
            $zone_code = $zone_query->row['code'];
        } else {
            $zone = '';
            $zone_code = '';
        }   

它适用于姓名、公司和邮政编码。顺便说一句,国家和州字段是下拉选择表单。

任何帮助将不胜感激!

【问题讨论】:

  • $address_query 只是一个 sql 字符串不是对象,你不能从字符串中获取属性

标签: php jquery mysql autocomplete


【解决方案1】:

您的 $address_query 不是一个对象,而是一个字符串,因此您会收到错误“尝试获取非对象的属性”

要访问 pdo 对象,您需要通过您创建的 $conn 访问它

$conn = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);

$address_query = "SELECT * FROM address where customer_id = '" . $customer_id . "' AND (firstname like :term OR lastname like :term) LIMIT 10";

$result = $conn->prepare($address_query);
$result->execute(array(':term'-> $term)); 

if ($result->rowCount()){

$conn 是对象,$address_query 是字符串,在您的字符串中还有一个占位符:term 您需要在执行函数中为您的 sql 语句设置该值,这是 find all :term in字符串并用值替换它们

你的 sql 也很奇怪,因为你知道表中的国家/地区 ID,你应该加入表

您的代码应如下所示:

$dbhost = 'SERVER';
$dbuser = 'USERNAME';
$dbpass = 'PASSWORD';
$dbname = 'DATABASE_NAME';

try {
  $conn = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
}
catch(PDOException $e) {
    echo $e->getMessage();
}

$return_arr = array();
if ($conn)
{
$ac_term = "%".$_GET['term']."%";
$address_query = "SELECT * FROM address LEFT JOIN country address.country_id = country. country_id ON where address.customer_id = '" . $customer_id . "' AND (address.firstname like :term OR address.lastname like :term) LIMIT 10";

$result = $conn->prepare($address_query);
$result->bindValue(":term",$ac_term);
$result->execute();

/* Retrieve and store in array the results of the query.*/
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
    $return_arr [] = array(

    'label' => $row['firstname'] .' '. $row['lastname'] .' '. $row['company'] .' '.         $row['city'] .' '. $row['postcode'],
  'value' => $row['firstname'] .' '. $row['lastname'],
  'Company' => $row['company'],
  'Country' => $row['iso_code_2'],
  'postCode' => $row['postcode'],

   );
}}

【讨论】:

  • 感谢您的回复。错误信息消失了,但是json返回do data,[].
  • 这不是你问的问题,而是确认你的 sql 正在返回数据或者你 $return_arr 里面有东西, print_r($result->fetch(PDO::FETCH_ASSOC)) 或print_r($return_arr)
【解决方案2】:

我明白了。我将行改为

$address_query = "SELECT * 
FROM address 
LEFT JOIN country 
ON address.country_id = country.country_id 
where address.customer_id = '" . $customer_id . "' AND (address.firstname like :term OR address.lastname like :term) LIMIT 10";

感谢所有帮助过我的人。

【讨论】:

    猜你喜欢
    • 2019-06-09
    • 1970-01-01
    • 2017-09-10
    • 2013-10-24
    • 1970-01-01
    • 2019-12-22
    • 1970-01-01
    • 2020-09-15
    • 2021-08-03
    相关资源
    最近更新 更多