Erro PDO Fatal error: Uncaught exception 'PDOException' - pdo

Please Help me, i have error in my script.
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1065 Query was empty' in C:\xampp\htdocs\File\project\blog\class\Blog.php:36 Stack trace: #0 C:\xampp\htdocs\File\project\blog\class\Blog.php(36): PDOStatement->execute() #1 C:\xampp\htdocs\File\project\blog\index.php(32): Blog->dataview(NULL) #2 {main} thrown in C:\xampp\htdocs\File\project\blog\class\Blog.php on line 36
public function dataview($query)
{
$stmt = $this->connect->prepare($query);
$stmt->execute();
return true;
if($stmt->rowCount() > 0) {
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
?>
<tr>
<td><?php print($row['id']); ?></td>
<td><?php print($row['judul']); ?></td>
<td><?php print($row['kategori']); ?></td>
<td><?php print($row['isi']); ?></td>
<td></td>
<td></td>
</tr>
<?php
}
} else {
?>
<tr>
<td>Not Found</td>
</tr>
<?php
}
}

Related

Datables - hide data until search

I'd like for my page to initially load with just a search box, and then when someone begins to search that is when the table appears.
My example table is below:
<script>
$(document).ready(function() {
$('#example').DataTable( {
responsive: true
} );
} );
</script>
<table id="example" class="display" style="width:100%">
<thead>
<tr>
<th>Name</th>
<th>Description</th>
<th class="none">Branch</th>
</tr>
</thead>
<tbody>
<tr>
<td>Test 1</td>
<td>Test test test</td>
<td>ABC</td>
</tr>
<tr>
<td>Test 2</td>
<td>Test test test</td>
<td>DEF</td>
</tr>
<tr>
<td>Test 3</td>
<td>Test test test</td>
<td>GHI</td>
</tr>
</tbody>
</table>
yes it can be done using ajax call and i suppose you use php.
For example, you can generate a table everytime you press any char, populating the table with the results or using a button to search the keyword you type on the input field.
Example with ajax and php:
var search_string = $('#id_of_the_input_box').val();
$.ajax({
url:"php_file.php",
method:"POST",
data:{action:action},
dataType:"json",
success:function(data){
console.log(data);
}
and the php code using PDO
$search_string = $_POST["string"];
$result = '';
$query = $connection->prepare(" select * from table where name = '%".search_string."' ";
$query->execute();
$query_results->query->fetchAll;
if($query->rowCount() > 0) {
$result = '
<script>
$(document).ready(function() {
$('#example').DataTable( {
responsive: true
});
});
<table id="example" class="display" style="width:100%">
YOUR TABLE ROWS HERE
</table>
';
// foreach loop to populate the table example
foreach ($query_results as $row) {
$result.= '
<tr>
<td>'.$row["table_column_name_1"].'</td>
<td>'.$row["table_column_name_1"].'</td>
<td>'.$row["table_column_name_1"].'</td>
<td>'.$row["table_column_name_1"].'</td>
'
} else {
echo "No results";
}
}
// echo the generated table as ajax response
echo $result;
Hope you have caught the idea with this example.

PDO gather BLOB files

I'm trying to find a way to output whatever BLOB files are inserted into a silly database. In my code when I query the CV column I get all sorts of weird symbols. Can you guys please help me fix my code?
$crud->dataview($query);
}else{
$query = "SELECT id, from_unixtime(cheie_wp) as `datetime`, nume, prenume, email, telefon, oras, denumiri_job, cv FROM jobselection_data_old ORDER BY id DESC";
$records_per_page=10;
$newquery = $crud->paging($query,$records_per_page);
$crud->dataview($newquery);
public function dataview($query)
{
$stmt = $this->db->prepare($query);
$stmt->execute();
if($stmt->rowCount()>0)
{
while($row=$stmt->fetch(PDO::FETCH_ASSOC))
{
$date_in = $row['datetime'];
$date_out = explode(' ', $date_in);
?>
<tr>
<td><?php echo $date_out[0]; ?></td>
<td><?php print($row['nume']); ?></td>
<td><?php print($row['prenume']); ?></td>
<td><?php print($row['email']); ?></td>
<td><?php print($row['telefon']); ?></td>
<td><?php print($row['oras']); ?></td>
<td><?php print($row['denumiri_job']); ?></td>
<td align="center"><?php
if($row['cv'] != NULL) { ?>
view
<?php }else{
echo '-';
}
?></td>
<td align="center">
<i class="glyphicon glyphicon-edit"></i>
</td>
<td align="center">
<i class="glyphicon glyphicon-remove-circle"></i>
</td>
</tr>
<?php
}
}else{ ... }
}
EDIT
I have modified my code and added view.php with this code:
if(isset($_GET['id']))
{
$stmt = $DB_con->prepare("SELECT cv FROM jobselection_data_old WHERE id=:id");
$stmt->execute(array(":id"=>$_GET['id']));
$stmt->bindColumn(1, $lob, PDO::PARAM_LOB);
$stmt->fetch(PDO::FETCH_BOUND);
header('content-type: image/jpeg');
echo $lob;
}
?>
The problem is that it does not generate the image file I have uploaded to the database.
You can check it at https://ns.jobselection.ro/db/
user/pass: test
I added a screen capture of my database as well: https://ns.jobselection.ro/db/screen.png . I only added one row for testing purposes!
Wow, this is really weird. I found what the issue was. I had my require configuration files in a separate php block of code in the same file. Until I placed them both in the same block of code it did not work!

How to set css of dataTable after reload?

There is a dataTable :
<table id="list_details_livraison" class="striped cell-hovered border bordered" data-searching="true">
<thead>
<tr>
<th style="text-align: center;"><?php echo _getText('detaillivraison.entete.article');?></th>
<th style="text-align: center;"><?php echo _getText('detaillivraison.entete.lot');?></th>
<th style="text-align: center;"><?php echo _getText('detaillivraison.entete.qtelivrer');?></th>
<th style="text-align: center;"><?php echo _getText('detaillivraison.entete.pu');?></th>
<th style="text-align: center;"><?php echo _getText('detaillivraison.entete.taxe');?></th>
<th style="text-align: center;"><?php echo _getText('detaillivraison.entete.remise');?></th>
<th style="text-align: center;"><?php echo _getText('detaillivraison.entete.prixtotal');?></th>
<th></th>
</tr>
</thead>
<tbody>
</tbody>
</table>
$(document).ready(function(){
var list_details_livraison = $('#list_details_livraison').DataTable({
...
"initComplete": function(settings, json) {
$(this).css("table-layout","fixed");
}
});
Then I filter the dataTable by selecting a value from a select element :
$('#cacher').on("change", function() {
var ddeb = convertDateFormat3($("#date_deb_").val());
var dfin = convertDateFormat3($("#date_fin_").val());
list_details_livraison.ajax.url("<?php echo RP_SSP; ?>server_processing_livraisons_frnsr.php?ddeb="+ddeb+"&dfin="+dfin+"&type="+$("#h_filtre_type_livraison").val()+"&valider="+$("#h_filtre_etre_valider").val()).load();
});
At runtime the dataTable's columns are not autowidth after I make the filter ; I tried using list_details_livraison.css("table-layout","fixed"); but I got a console error ! So how to reapply the table-layout fixed css at this moment ?
Comment converted into an answer at the request of the OP.
initComplete() will only run once per table creation, so won't be called after subsequent paging, filtering etc. For this you need drawCallback() which gets called after every draw.
You just need to change:
"initComplete": function(settings, json) {
to
"drawCallback": function( settings ) {

cakephp sql log is correct, not returning data

I'm creating a simple view function, after checking my sql log its correct, However my view is not printing the information.
SELECT `AccountsUser`.`id`, `AccountsUser`.`account_id`
FROM `pra`.`accounts_users` AS `AccountsUser`
WHERE `user_id` = 14
SELECT `Template`.`id`, `Template`.`name`, `Template`.`description`,
`Template`.`account_id`
FROM `pra`.`templates` AS `Template`
WHERE `Template`.`account_id` = (10)
The account_id=10 but it isn't printing out the templates related to the account.id
function view(){
$this->set('title_for_layout', 'View Templates');
$this->set('stylesheet_used', 'homestyle');
$this->set('image_used', 'eBOXLogoHome.jpg');
$this->layout='home_layout';
$this->Template->unbindModel(array('belongsTo'=>array('Account')));
$templates = $this->Auth->user('name');
$accounts=$this->User->AccountsUser->find('list', array('fields'=>array('id', 'account_id'),'conditions' => array('user_id' => $this->Auth->user('id'))));
$templates=$this->Template->find('all', array('conditions' => array('Template.account_id' => $accounts)));
$this->set('template', $templates);
$this->set('account'. $accounts);}
Here is the view
<table width="100%" border="1">
<table width="100%" border="1">
<tr>
<th>Template Name</th>
<th>Template Description</th>
</tr>
<?php foreach($template as $templates): ?>
<tr>
<td align='center'><?php echo $templates['templates']['id']; ?> </td>
<td align='center'><?php echo $templates['templates']['name']; ?> </td>
</tr>
<?php endforeach; ?>
</table>
id like it to print a list of templates related to the account.id
You should try the following:
<table width="100%" border="1">
<tr>
<th>Template Name</th>
<th>Template Description</th>
</tr>
<?php if(!empty($template))
{
foreach($template as $templates)
{?>
<tr>
<td align='center'><?php echo $templates['Template']['id']; ?> </td>
<td align='center'><?php echo $templates['Template']['name']; ?> </td>
</tr>
<?php
}// end of foreach
}
else
{?>
<tr> <td>No Templates Found.</td></tr>
<?php
}?>
</table>

I have got wrong alert! What is wrong?

I have a problem in revealing the actual results of the radio buttons. I use javascript to get value from radio button, succeeded anyway, BUT when I create a condition where if value! = "Yes" alert that appears why still "OK", I would like to choose if it is "No" means alert that should be appear is "NOT OKE ".
This is the form:
<form id="form_test" name="form_test" method="POST">
<input type="hidden" id="menu_id" name="menu_id" value="<?=$menu_id?>">
<table align='center' width='auto' class='table1' cellpadding='5' cellspacing="0">
<tr>
<td align='center' class='tabletitle'>No</td>
<td align='center' class='tabletitle'>PC</td>
<td align='center' class='tabletitle'>PN</td>
<td align='center' class='tabletitle'>PCO</td>
<td align='center' class='tabletitle'>GC</td>
<td align='center' class='tabletitle'>CUR</td>
<td align='center' class='tabletitle'>Codes</td>";
<td align='center' class='tabletitle'>Percent</td>";
<td align='center' width='5%' class='tabletitle'>Yes</td>";
<td align='center' width='5%' class='tabletitle'>No</td>";
</tr>
[call to database]
$num++;
echo " <tr>
<td class='tablecontent' align='center' valign='top'>$num</td>
<td class='tablecontent' valign='top'>$pc</td>
<td class='tablecontent' valign='top' nowrap>$pn</td>
<td class='tablecontent' align='center' valign='top'>$pco</td>
<td align='center' class='tablecontent' align='center' valign='top'>$gc</td>
<td class='tablecontent' align='center' valign='top'>$cur</td>
<td class='tablecontent2' valign='top'>$codes</td>";
<td align='center' class='tablecontent' valign='top'>$percent</td>";
<td align='center' class='tablecontent' valign='top'><input type='radio' id='yes' name='cek$num' value='yes' checked></td>";
<td align='center' class='tablecontent' valign='top'><input type='radio' id='no' name='cek$num' value='no' ></td>";
</tr>";
echo "
<tr>
<td><input type='button' name='accept' value='accept' onclick='getaccept()'></td>";
</tr>";
echo "</table>";
echo "<input type='hidden' id='jmlcek' name='jmlcek' value='$num'>";
echo "</form>";
Then this is the Javascript codes to get value:
<script language="Javascript">
function getaccept(){
//var str = '';
var elem = document.getElementById('form_test').elements;
var c = confirm("Yakin mau nerima?");
for (var i = 1; i <= elem.length; i++)
{
var e = elem[i].value
if (elem[i].type == "radio" && c)
{
if (e != "yes" && elem[i].checked){
alert('NOT OKE');
return false;
}else{
alert('OKE');
return true;
}
}
}
}
</script>
For example the amount of data that actually are three and all got selected "yes", when I select "no" at one of the existing number it will display alert "NOT OKE", but in fact when I select "no" the value I got was "yes" and alert up "OKE".
I have tried many times to make them correct but still the results are wrong, so what's wrong?
Thanks.
This is what I got:
function getApprove(){
//var elem = document.getElementById('form_test').elements;
var RadioButton = document.getElementsByTagName('input');
var Approve = confirm("Are you sure?");
for (var i = 0; i < RadioButton.length; i++)
{
if (RadioButton[i].type == "radio" && Approve)
{
if(RadioButton[i].checked && RadioButton[i].value != "yes"){
alert("NOT OKE");
return false;
}
}
}
for (var i = 0; i < RadioButton.length; i++)
{
if (RadioButton[i].type == "radio" && Approve)
{
if(RadioButton[i].checked && RadioButton[i].value == "yes"){
alert("OKE");
return true;
}
}
}
=>SOLVED<=
Thankyou so much p.campbell, for you concern and helping me m(_ _)m :)
What's happening here is that you're only evaluating the FIRST radio button because you've got a return statement in both those conditions in your (e != "yes" && elem[i].checked). It's exiting the function.
Remove your return statements near your alerts. They're exiting the function, and disallowing the loop to iterate through the form elements.
Here's a demo on JsBin.