如何修复错误数组到字符串转换并显示提取的数据

时间:2017-02-20 08:10:29

标签: php mysql error-handling echo

我在WordPress上使用MYSQL和PHP以便从数据库中检索数据,但使用4下拉列表允许用户进行选择,并根据用户的选择,SQL查询将检索所有相关数据。 / p>

问题是系统显示以下警告:

  

中的数组到字符串转换

并且不从数据库中检索任何内容。

注意:SQL查询是正确的,并检索所需的数据(在phpmyadmin上测试)。

代码:

<?php
    /*
    Template Name: search info
    */

    get_header();
    ?>

    <?php
    // code for submit button action
    global $wpdb;
//variables that handle the retrieved data from mysql database based on the ID of the variable in HTML (select)


if(isset($_POST['query_submit']))
{

   if(isset($_POST['site_name'])) 
      { 
       $site_name=$_POST['site_name'];
      }
      else { $site_name=""; }

    if(isset($_POST['owner_name'])) 
     {
      $owner_name=$_POST['owner_name']; 
     } 
     else { $owner_name=""; }

     if(isset($_POST['Company_name'])) 
     {
      $company_name=$_POST['Company_name'];
     } 
     else { $company_name=""; }

    if(isset($_POST['Subcontractor_name'])) 
    { 
     $Subcontractor_name=$_POST['Subcontractor_name']; 
    }
    else { $Subcontractor_name="";}

   $site_id = ['siteID'];
//$site_id = (array)$site_id;
$equipment_type = ['equipmentTYPE'];
//$equipment_type = (array)$equipment_type;
$lat=['latitude'];
//$lat = (array)$lat;
$long=['longitude'];
//$long = (array)$long;
$height = ['height'];
//$height = (array)$height;
$owner_contact = ['ownerCONTACT'];
//$owner_contact = (array)$owner_contact;
$sub_contact = ['subcontractorCONTACT'];
//$sub_contact = (array)$sub_contact;
$sub_company =[ 'subcontractorCOMPANY'];
//$sub_company = (array)$sub_company;


//query to retrieve all  related info of the selected data from the dropdown list  
$query_submit =$wpdb->get_results ("select 

site_info.siteID,site_info.siteNAME ,site_info.equipmentTYPE,site_coordinates.latitude,site_coordinates.longitude,site_coordinates.height ,owner_info.ownerNAME,owner_info.ownerCONTACT,company_info.companyNAME,subcontractor_info.subcontractorCOMPANY,subcontractor_info.subcontractorNAME,subcontractor_info.subcontractorCONTACT from `site_info`
LEFT JOIN `owner_info`
on site_info.ownerID = owner_info.ownerID
LEFT JOIN `company_info` 
on site_info.companyID = company_info.companyID
LEFT JOIN `subcontractor_info` 
on site_info.subcontractorID = subcontractor_info.subcontractorID
LEFT JOIN `site_coordinates` 
on site_info.siteID=site_coordinates.siteID 
where 
site_info.siteNAME = `$site_name` 
AND
owner_info.ownerNAME = `$owner_name`
AND
company_info.companyNAME = `$company_name`
AND
subcontractor_info.subcontractorNAME = `$Subcontractor_name`
 " , ARRAY_A);

// create a table  inside php code 

echo "<table width='30%' ";
echo     " <tr>";
echo         " <td>$query_submit ['siteNAME']</td>";
echo         " <td>$query_submit  ['ownerNAME']</td>";
echo         " <td>$query_submit  ['companyNAME']</td>";
echo         " <td>$query_submit ['subcontractorNAME']</td>";
echo         " <td>$query_submit ['siteID'] </td>";
echo         " <td>$query_submit ['equipmentTYPE'] </td>";
echo         " <td>$query_submit ['latitude'] </td>";
echo         " <td>$query_submit ['longitude'] </td>";
echo         " <td>$query_submit ['height']</td>";
echo         " <td>$query_submit ['ownerCONTACT']</td>";
echo         " <td>$query_submit ['subcontractorCONTACT']</td>";
echo         " <td>$query_submit ['subcontractorCOMPANY']</td>";
echo     "</tr>";
echo "</table>";
?>

web page after clicking the submit button

2 个答案:

答案 0 :(得分:0)

在你的回音中调用字符串的方法不正确你在$query_submit ['string']之间给出了空格{使用$query_submit['siteNAME']并且你的查询提交查询几乎没有变化:

    <?php
        /*
        Template Name: search info
        */

        get_header();
        ?>

        <?php
        // code for submit button ation
        global $wpdb;
    //variables that handle the retrieved data from mysql database based on the ID of the variable in HTML (select)


    if(isset($_POST['query_submit']))
    {

       if(isset($_POST['site_name'])) 
          { 
           $site_name=$_POST['site_name'];
          }
          else { $site_name=""; }

        if(isset($_POST['owner_name'])) 
         {
          $owner_name=$_POST['owner_name']; 
         } 
         else { $owner_name=""; }

         if(isset($_POST['Company_name'])) 
         {
          $company_name=$_POST['Company_name'];
         } 
         else { $company_name=""; }

        if(isset($_POST['Subcontractor_name'])) 
        { 
         $Subcontractor_name=$_POST['Subcontractor_name']; 
        }
        else { $Subcontractor_name="";}

       $site_id = $_POST['siteID'];
    //$site_id = (array)$site_id;
    $equipment_type = $_POST['equipmentTYPE'];
    //$equipment_type = (array)$equipment_type;
    $lat=$_POST['latitude'];
    //$lat = (array)$lat;
    $long=$_POST['longitude'];
    //$long = (array)$long;
    $height = $_POST['height'];
    //$height = (array)$height;
    $owner_contact = $_POST['ownerCONTACT'];
    //$owner_contact = (array)$owner_contact;
    $sub_contact = $_POST['subcontractorCONTACT'];
    //$sub_contact = (array)$sub_contact;
    $sub_company =$_POST[ 'subcontractorCOMPANY'];
    //$sub_company = (array)$sub_company;


    $query_submit =$wpdb->get_results ("select site_info.siteID,site_info.siteNAME ,site_info.equipmentTYPE,site_coordinates.latitude,site_coordinates.longitude,site_coordinates.height ,owner_info.ownerNAME,owner_info.ownerCONTACT,company_info.companyNAME,subcontractor_info.subcontractorCOMPANY,subcontractor_info.subcontractorNAME,subcontractor_info.subcontractorCONTACT from `site_info`
    LEFT JOIN `owner_info`
    on site_info.ownerID = owner_info.ownerID
    LEFT JOIN `company_info` 
    on site_info.companyID = company_info.companyID
    LEFT JOIN `subcontractor_info` 
    on site_info.subcontractorID = subcontractor_info.subcontractorID
    LEFT JOIN `site_coordinates` 
    on site_info.siteID=site_coordinates.siteID 
    where 
    site_info.siteNAME = ".$site_name."
    AND
    owner_info.ownerNAME = ".$owner_name."
    AND
    company_info.companyNAME = ".$company_name."
    AND
    subcontractor_info.subcontractorNAME = ".$Subcontractor_name."
     " , ARRAY_A);
    if($query_submit){
    echo "<table width='30%' ";
echo     "<tr>";
echo         "<td>".$query_submit->siteNAME."</td>";
echo         "<td>".$query_submit->ownerNAME."</td>";
echo         "<td>".$query_submit->companyNAME."</td>";
echo         "<td>".$query_submit->subcontractorNAME."</td>";
echo         "<td>".$query_submit->siteID."</td>";
echo         "<td>".$query_submit->equipmentTYPE."</td>";
echo         "<td>".$query_submit->latitude."</td>";
echo         "<td>".$query_submit->longitude."</td>";
echo         "<td>".$query_submit->height."</td>";
echo         "<td>".$query_submit->ownerCONTACT."</td>";
echo         "<td>".$query_submit->subcontractorCONTACT."</td>";
echo         "<td>".$query_submit->subcontractorCOMPANY."</td>";
echo     "</tr>";
echo "</table>";
    } else {
        echo "query is not worked";
    }

答案 1 :(得分:0)

错误来自以下几行:

$site_id = ['siteID'];
//$site_id = (array)$site_id;
$equipment_type = ['equipmentTYPE'];
//$equipment_type = (array)$equipment_type;
$lat=['latitude'];
//$lat = (array)$lat;
$long=['longitude'];
//$long = (array)$long;
$height = ['height'];
//$height = (array)$height;
$owner_contact = ['ownerCONTACT'];
//$owner_contact = (array)$owner_contact;
$sub_contact = ['subcontractorCONTACT'];
//$sub_contact = (array)$sub_contact;
$sub_company =[ 'subcontractorCOMPANY'];

如果您从帖子中获取这些变量,我不确定。如果您从帖子值中获取这些值,请使用:

$site_id = $_POST['siteID'];
           ^^^use global variable

但我可以看到你在$query_submit数组变量中使用它。这意味着它来自db。

在这种情况下,您必须删除或注释这些行。