0

I am using WordPress and the global class $wpdb in order to retrieve data from MySQL database and display the results in a table.

I have 4 dropdown list that allow the user to select the required inputs then based on the selected inputs the system display all the related data for the user selection.

When I try to run the code it display error:

Notice: Array to string conversion

web page display the error

first part of code :

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

    get_header();
    ?>

    <?php
    // code for submit button ation
    global $wpdb,$_POST;
//variables that handle the retrieved data from mysql database
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'];
$equipment_type = ['equipmentTYPE'];
$lat=['latitude'];
$long=['longitude'];
$height = ['height'];
$owner_contact = ['ownerCONTACT'];
$sub_contact = ['subcontractorCONTACT'];
$sub_company = ['subcontractorCOMPANY'];


   if(isset($_POST['query_submit']))
   {
//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`
 ");
 ?>
    <table width="30%" >
        <tr>
           <td>Site Name</td>
           <td>Owner Name</td>
           <td>Company Name</td>
           <td>Subcontractor Name</td>
         </tr>
         <tr>
            <td><?php echo $site_name ; ?></td>
            <td><?php echo $owner_name ; ?></td>
            <td><?php echo $company_name ; ?></td>
            <td><?php echo $Subcontractor_name ; ?></td>
            <td><?php echo $site_id ; ?></td>
            <td><?php echo $equipment_type ; ?></td>
            <td><?php echo $lat ; ?></td>
            <td><?php echo $long ; ?></td>
            <td><?php echo $height ; ?></td>
            <td><?php echo $owner_contact ; ?></td>
            <td><?php echo $sub_contact ; ?></td>
            <td><?php echo $sub_company ; ?></td>


         </tr>
    </table>
    <?php }  ?>

The second part of code is for retrieve data from database and includes it in the dropdown list.

I will appreciate any help.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Nabil Jaroush
  • 49
  • 3
  • 12

1 Answers1

2

You can get rid of the "Array to string conversion" error quite easy.

In these lines, you are creating arrays:

$site_id = ['siteID'];
$equipment_type = ['equipmentTYPE'];
$lat=['latitude'];
...
$sub_company = ['subcontractorCOMPANY'];

...which you later are trying to echo. You simply can't echo arrays.

Just change the above to be strings instead:

$site_id = 'siteID';
$equipment_type = 'equipmentTYPE';
$lat = 'latitude';
...
$sub_company = 'subcontractorCOMPANY';

Note: As others already pointed out, your code is wide open to SQL Injections. You should really escape your data, before using it in any queries.

M. Eriksson
  • 13,450
  • 4
  • 29
  • 40
  • i did as you suggest in your answer this fix the error but i am still not able to retrieve the requested data from the query in **$query_submit** – Nabil Jaroush Feb 17 '17 at 08:25
  • @NabilJaroush - `= \`$site_name\` ` <- You have the wrong type of quotes here. (or rather, you're using backticks when you should be using quotes). Use `'` around values. Here's a post that explains when to use what: http://stackoverflow.com/questions/11321491/when-to-use-single-quotes-double-quotes-and-backticks-in-mysql – M. Eriksson Feb 17 '17 at 21:14