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
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.
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.
<table border="1">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Points</th>
</tr>
<?php
global $wpdb;
$result = $wpdb->get_results ( "SELECT * FROM myTable" );
foreach ( $result as $print ) {
?>
<tr>
<td><?php echo $print->firstname;?></td>
</tr>
<?php }
Related
I have two tables "personal_trainer" and "training_plan". PersonalTrainerID is a foreign key in training_plan. I want to display that when a trainer logs in with their email and password, only the training plans that apply to that ID appears.
However, I am having trouble understanding the logic. I have coded it so that all the information from the training_plan table appears, I cannot created it such that only the rows that apply to the ID are visible to the user. I have made this by the simple sql statement "SELECT * from training_plan". There is a filter textbox to search the table that doesn't effect the code if you're wondering.
I have commented the code to try make it easier to understand. Any help would be greatly appreciated!
<?php
if (isset($_POST['search'])) /*This code allows the filter textbox to search the db */
{
$valueToSearch = $_POST['ValueToSearch'];
$query = "select * from training_plan WHERE concat('trainingPlanID', `personalTrainerID`, `clientID`, `trainingType`, `exercise1`, `exercise2`, `exercise3`, `exercise4`, `exercise5`, `exercise6`, 'reps', 'sets', 'description')like'%".$valueToSearch."%'";
$search_result = filterTable($query);
}
else {
$query = "SELECT * from training_plan WHERE PersonalTrainerID= (SELECT personalTrainerID FROM personal_trainer WHERE email=$_SESSION['user'])"; /*The error that is displayed is 'syntax error, unexpected string after ['*/
$search_result = filterTable($query);
}
function filterTable($query)
{
$connect = mysqli_connect("localhost:3308","root","","fypdatabase");
$filter_Result = mysqli_query($connect, $query);
return $filter_Result;
}
?>
<?php /*This displays the data in a table but so far outputs all of the table data */
while($row = mysqli_fetch_array($search_result))
{
?>
<tr>
<td><?php echo $row["trainingPlanID"]; ?></td>
<td><?php echo $row["personalTrainerID"]; ?></td>
<td><?php echo $row["clientID"]; ?></td>
<td><?php echo $row["trainingType"]; ?></td>
<td><?php echo $row["exercise1"]; ?></td>
<td><?php echo $row["exercise2"]; ?></td>
<td><?php echo $row["exercise3"]; ?></td>
<td><?php echo $row["exercise4"]; ?></td>
<td><?php echo $row["exercise5"]; ?></td>
<td><?php echo $row["exercise6"]; ?></td>
<td><?php echo $row["reps"]; ?></td>
<td><?php echo $row["sets"]; ?></td>
<td><?php echo $row["description"]; ?></td>
<td>
Delete
</td>
<td>
Update
</td>
</tr>
<?php
Change your query to:
$query = "SELECT * from training_plan WHERE PersonalTrainerID = (SELECT personalTrainerID FROM personal_trainer WHERE email='".$_SESSION['user']."')";
I want to arrange the table head with my table body, so the data fill have a same type, how can i doing it??
The black square is the correct one
This is my code
<table>
$syntax = 'SELECT customer,type, count(type) as num FROM view_detail GROUP BY type ORDER BY customer';
$rows = $config->prepare($syntax);
$rows->execute();
$result = $rows->fetchAll(PDO::FETCH_OBJ);
?>
</tr>
<td>No</td>
<td>Customer</td>
<?php for($x=0;$x<count($result);$x++){ ?>
<td><?php echo $result[$x]->type; ?></td>
<?php } ?>
<td>Total</td>
<?php $no = 1;
$query = 'SELECT customer,type, count(type) as tipe FROM view_detail GROUP BY customer ORDER BY customer';
$baris = $config->prepare($query);
$baris->execute();
$hasil1 = $baris->fetchAll(PDO::FETCH_OBJ);
for($i=0;$i<count($hasil1);$i++){
$sql = 'SELECT * FROM view_detail WHERE customer = "'.$hasil1[$i]->customer.'" ORDER BY customer';
$row = $config->prepare($sql);
$row->execute();
$hasil = $row->fetchAll(PDO::FETCH_OBJ);
?>
<tr>
<td><?php echo $no; $no++;?></td>
<td><?php echo $hasil[$i]->customer; ?></td>
<?php
for($y=0;$y<count($result);$y++){
if($y < count($hasil)){
?>
<td><?php echo $hasil[$y]->type; ?></td>
<?php }else{ ?>
<td>0</td>
<?php } } }?>
</tr>
</table>
Thanks
I want to make it like this
Edit:
Dynatrade -> MFN200, N50Z, and N70
Super Bloom -> N70, MFN100, MFN120, and MFN150
I've been troubleshooting for a while now, and I really can't find any answer. Basically this is what it looks like:
When I type the name of a real user: Nothing posted on the page with an exception of the header
When I type a fake user: User has not been found (direct translation from Norwegian in the code)
<?php
include_once("moduler/head.php");
$user = $_GET['user'];
$query1 = $con->query("SELECT * FROM players WHERE name='$user'");
echo $con->error;
if ($query1->num_rows == 0) {
echo "<h1>Ingen spiller funnet med: $user.</h1>";
return;
}
while ($row == mysqli_fetch_array($query1)) {
$styrkeLvl = $row['styrke'];
$beskyttelseLvl = $row['beskyttelse'];
$bueskytingLvl = $row['bueskyting'];
$trehuggingLvl = $row['trehugging'];
$gruvedriftLvl = $row['gruvedrift'];
$fiskingLvl = $row['fisking'];
$kills = $row['kills'];
$deaths = $row['deaths'];
$rank = $row['rank'];
$money = $row['money'];
$donstatus = $row['donationstatus'];
$lastlogin = $row['lastlogin'];
$regtime = $row['registrationtime'];
$rankName = getRankString($rank);
?>
<h1><?php echo $user; ?></h1>
<table class=\"userView\">
<tbody>
<tr><td>Brukerstatus</td>
<td><?php echo $rankName; ?></td>
</tr>
<tr><td>Donasjon status</td>
<td>D<?php echo $donstatus; ?></td>
</tr>
<tr><td>Styrke level</td>
<td><?php echo $styrkeLvl; ?></td>
</tr>
<tr><td>Beskyttelse level</td>
<td><?php echo $beskyttelseLvl; ?></td>
</tr>
<tr><td>Bueskyting level</td>
<td><?php echo $bueskytingLvl; ?></td>
</tr>
<tr><td>Trehugging level</td>
<td><?php echo $trehuggingLvl; ?></td>
</tr>
<tr><td>Fisking level</td>
<td><?php echo $fiskingLvl; ?></td>
</tr>
<tr><td>Drap</td>
<td><?php echo $kills; ?></td>
</tr>
<tr><td>Dødsfall</td>
<td><?php echo $deaths; ?></td>
</tr>
<tr><td>Sist pålogget</td>
<td><?php echo $lastlogin; ?></td>
</tr>
<tr><td>Registreringsdato</td>
<td><?php echo $regtime; ?></td>
</tr>
</tbody>
</table>
<?php
}
?>
Any ideas? I've also tried running the entire thing in an echo with no result
Thanks
All your variables inside the while loop are only available inside the while loop. so you cannot echo them in the tables. so <td><?php echo $rankName; ?></td> will give you an undefined variable error. The same applies for the other variables you are trying to display.
To avoid this you should try to declare your variables globally, or put your table inside the loop.
Also the other issue is that as it is the code inside the loop will never execute because of the double equal (==). You should use double equal (==) for comparison and single equal (=) for assignment.
So you have to change while ($row == mysqli_fetch_array($query1)) to while ($row = mysqli_fetch_array($query1))
I have a table which is fed dynamically from the database, this works as it should, I've then put the datatables functionality on the top which is working well, the sorting, search, the pagination is again working as it should.
I'm now looking at being able to edit a row and update the data back into the db and refresh the table.
My table structure
<table class="table table-striped table-hover table-bordered" id="tobebookedtable">
<thead>
<tr>
<th style="display:none;">PropID</th>
<th>ProjectNo</th>
<th>Householder</th>
<th>HouseNoName</th>
<th>Street Name</th>
<th>Postcode</th>
<th>Telephone</th>
<th>EPCPlanned</th>
<th>TAM</th>
</tr>
</thead>
<tbody>
<tr>
<?php
$planning = db::getInstance()->query('CALL sp_tobebooked()');
foreach ($planning->results() as $planning) {
?>
<td style="display:none;"><?php echo $planning->PropID; ?></td>
<td><?php echo $planning->ProjectNo; ?></td>
<td><?php echo $planning->Householder; ?></td>
<td><?php echo $planning->HouseNoName; ?></td>
<td><?php echo $planning->StreetName; ?></td>
<td><?php echo $planning->Postcode; ?></td>
<td><?php echo $planning->Telephone; ?></td>
<td><?php echo $planning->EPCPlanned; ?></td>
<td><?php echo $planning->TAM; ?></td>
</tr>
<?php }; ?>
</tbody>
</table>
The EPCPlanned is the only field I want to update.
This here is my code for the jQuery side, the edit ability works, I can click into a cell and edit the record.
<script type="text/javascript">
$(document).ready(function() {
$('#tobebookedtable').dataTable( {
//"bProcessing": true,
//"bServerSide": true,
//"bJQueryUI": true,
"bPaginate": true,
"bStateSave": true
} );
/* Init DataTables */
var oTable = $('#tobebookedtable').dataTable();
/* Apply the jEditable handlers to the table */
oTable.$('td').editable( 'tobebooked_edit.php', {
"callback": function( sValue, y ) {
var aPos = oTable.fnGetPosition( this );
oTable.fnUpdate( sValue, aPos[0], aPos[1] );
//window.location.reload();
},
"submitdata": function ( value, settings ) {
console.log(value);
console.log(settings);
return {
"row_id": this.parentNode.getAttribute('id'),
"column": oTable.fnGetPosition( this )[2]
};
},
"height": "26px",
"width": "100%"
} );
} );
</script>
this is the tobebooked_edit.php
<?php
require 'core/init.php';
$table="temp_tobebooked";
$rawdata = $_POST;
$query = "show columns from $table";
$result= mysql_query($query) or die(mysql_error());
$fields = array();
while ( $row = mysql_fetch_assoc($result) )
{
$fieldname = $row['Field'];
array_push($fields, $fieldname);
}
$id = $rawdata['row_id'];
$value = $rawdata['value'];
$column = $rawdata['column'];
$column = $column + 1;
$fieldname = $fields[$column];
$value = utf8_decode($value);
$query = "update $table set $fieldname = '$value' where PropID = '$id'";
$result = mysql_query($query);
if (!$result) { echo "Update failed"; }
else { echo "UPD: $value"; }
?>
When it all runs and I update a record the field Updates on the screen and shows with the "UPD:" and the entered value.
But when I check my database there is no record updated. No errors show either
How can I get the data to update in my db?
First of all, I think that this
<tr>
<?php
$planning = db::getInstance()->query('CALL sp_tobebooked()');
foreach ($planning->results() as $planning) {
?>
should be
<?php
$planning = db::getInstance()->query('CALL sp_tobebooked()');
foreach ($planning->results() as $planning) {
?>
<tr>
Then, with
"row_id": this.parentNode.getAttribute('id'),
for each td element, you select the tr element and search for the id attribute, that doesn't exists
Firstly as per rsella's comment you nee to move the row inside the loop.
Secondly as suspected you are holding the ID in a hidden cell which is infact a sibling of the editable elements rather than the parent. Try changing the table structure to the following
<tbody>
<?php
$planning = db::getInstance()->query('CALL sp_tobebooked()');
foreach ($planning->results() as $planning) {
?>
<tr id="<?php echo $planning->PropID; ?>" >
<td><?php echo $planning->ProjectNo; ?></td>
<td><?php echo $planning->Householder; ?></td>
<td><?php echo $planning->HouseNoName; ?></td>
<td><?php echo $planning->StreetName; ?></td>
<td><?php echo $planning->Postcode; ?></td>
<td><?php echo $planning->Telephone; ?></td>
<td><?php echo $planning->EPCPlanned; ?></td>
<td><?php echo $planning->TAM; ?></td>
</tr>
<?php };
?>
</tbody>
This should mean that the parent has an ID and thus this.parentNode.getAttribute('id') should be able returning the required value.
As you're only editing the EPCPlanned cell then an alternative would be
<tbody>
<?php
$planning = db::getInstance()->query('CALL sp_tobebooked()');
foreach ($planning->results() as $planning) {
?>
<tr>
<td><?php echo $planning->ProjectNo; ?></td>
<td><?php echo $planning->Householder; ?></td>
<td><?php echo $planning->HouseNoName; ?></td>
<td><?php echo $planning->StreetName; ?></td>
<td><?php echo $planning->Postcode; ?></td>
<td><?php echo $planning->Telephone; ?></td>
<td id="<?php echo $planning->PropID; ?>"><?php echo $planning->EPCPlanned; ?></td>
<td><?php echo $planning->TAM; ?></td>
</tr>
<?php };
?>
</tbody>
and in your JQuery change "row_id": this.parentNode.getAttribute('id') to this.getAttribute('id')
I am trying to set up a PHP MySQL search script which will let me print the content of my membership MySQL database table and then filter the results using different criteria.
The following fields are used in my MySQL table:
committee_id
rank
last_name
first_name
sex
address
email
phone_number
active_status
I want 2 ways to filter the data:
1) using using a drop down with all the available rank i can filter the results by rank of member.
2) using a drop down with all the available active_status you can filter the results by active status only.
I have set up my search form successfully, and printed all MySQL Table contents, Only the search by position part is filtering my result, but not the active_status, the filter part is a challenge. here is my html table and search script:
<?php include("./includes/connnect.php");?>
<!DOCTYPE HTML>
<html>
<body>
<table>
<thead>
<tr>
<td>ID</td>
<td>position</td>
<td>Last Name</td>
<td>First Name</td>
<td>Sex</td>
<td>Address</td>
<td><strong>Email</td>
<td><strong>Phone Number</td>
<td>Status</td>
</tr>
</thead>
<tbody>
<?php
if ($_REQUEST["position"]<>'') {
$search_position = " AND position='".mysql_real_escape_string($_REQUEST["position"])."'";
}
if ($_REQUEST["status"]<>'') {
$search_status = " AND status='".mysql_real_escape_string($_REQUEST["status"])."'";
}
else {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE committee_id>0".$search_position.$search_status;
}
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
if (mysql_num_rows($sql_result)>0) {
while ($row = mysql_fetch_assoc($sql_result)) {
?>
<tr>
<td><?php echo $row["committee_id"]; ?></td>
<td><?php echo $row["rank"]; ?></td>
<td><?php echo $row["last_name"]; ?></td>
<td><?php echo $row["first_name"]; ?></td>
<td><?php echo $row["sex"]; ?></td>
<td><?php echo $row["address"]; ?></td>
<td><?php echo $row["email"]; ?></td>
<td><?php echo $row["phone_number"]; ?></td>
<td><?php echo $row["active_status"]; ?></td>
</tr>
<?php
}
} else {
?>
<tr><td colspan="5">No results found.</td>
<?php
}
?>
</tbody>
</table>
</body>
</html>
I would appreciate any suggestions to get this going.
Thanks.
That is because when there is $_REQUEST["status"] the variable SQL is not setted as you put it on the else statement
if ($_REQUEST["status"]<>'') {
$search_status = " AND status='" .
mysql_real_escape_string($_REQUEST["status"])."'";
}
else { /// this is your problem
$sql = "SELECT * FROM ".$SETTINGS["data_table"]."
WHERE committee_id>0".$search_position . $search_status;
}
Put the $sql out of the else and take the else out.
Thank you Jorge your solution worked. But I got the same result with these elseif statements:
if ($_REQUEST["position"]<>'') {
$search_position = " AND position='".mysql_real_escape_string($_REQUEST["position"])."'";
}
if ($_REQUEST["status"]<>'') {
$search_status = " AND status='".mysql_real_escape_string($_REQUEST["status"])."'";
}
if ($_REQUEST["position"]<>'' and $_REQUEST["status"]<>'') {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]."
WHERE position = '".mysql_real_escape_string($_REQUEST["position"])."'
AND status = '".mysql_real_escape_string($_REQUEST["status"])."'";
}else if ($_REQUEST["position"]<>'') {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE position = '".mysql_real_escape_string($_REQUEST["position"])."'".$search_city;
}else if ($_REQUEST["status"]<>'') {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE status = '".mysql_real_escape_string($_REQUEST["status"])."'".$search_position;
}else {
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE committee_id>0".$search_position.$search_status;
}
I shall have to convert these to mysqli or pdo as advised.