phpbindingprepared-statement

Prepared statement execute fatal error and variables and database table miss match


I get the following error Fatal error: Call to a member function execute() on a non-object in /home/wt/public_html/view-reports.php on line 237 .

I also see another problem. The variables generated doesn't echo the right corresponding data. For instance, $rep_type echo position. Could you please help me on this. Thank you! Test site url is here. ethioserver.com/~wt/view-reports.php?rep_id=144 The problem is not visible in wampserver. The php version in both servers is 5.4.

The main code is below.

<?php
//generate page
     $_GET['rep_id']>0;
  if ($_GET['rep_id']!=0){
  require ('includes/db.php');
  mysqli_select_db($con, $db_name);
  $sql= 'SELECT * FROM Reports';
  $stmt = $con->prepare($sql);

  $stmt->execute();

  $stmt->bind_result($rep_id, $rep_date, $rep_ledit_date, $rep_by, $rep_type, $department, $position, $report, $rep_to);
  $stmt->fetch();
//allow users to edit

        if($fname . ' '. $lname!=$rep_by){
            echo '<div class="links"></div>';
        } 
        else {
            echo '<div class="links">';
            echo '<a href="edit-this-report.php?rep_id=' . $rep_id;
            echo 'target="_blank" img src="images/pdf.png" alt="edit report"  target="_blank" ><img src="images/edit.png"> </a>';
            echo '</div>';
        } 
echo '<div class="links"><a href="pdf/'. (str_replace(' ', '-',($rep_by) .'-'. str_replace(':','.',$rep_date))). '.pdf"  
        target="_blank" img src="images/pdf.png" alt="download report"><img src="images/pdf.png"> </a></div>'; 

  //start html creation
ob_start();
 echo "<h1>$rep_by ($rep_date)</h1>";
 echo '<div class="infobar"><strong>Report Type: </strong>'. $position . '</div>' ;
 echo '<div class="infobar"><strong>Department: </strong>'. $rep_type  . '</div>';
 echo '<div class="infobar"><strong>Position: </strong>'.  $department . '</div>';
 echo $report; 
 file_put_contents(('scripts/dompdf/html/'. (str_replace(' ', '-',($rep_by) .'-'. str_replace(':','.',$rep_date))). '.html'), ob_get_contents()); 


if  ($rep_ledit_date>0) {echo '<div class="infobar">' . 'Last Edited: ' . $rep_ledit_date . '</div>';


} else  {echo "";
//end html creation
ob_end_flush();}
//generate pdf using dompdf
require_once "scripts/dompdf/dompdf_config.inc.php";
$file='scripts/dompdf/html/'. (str_replace(' ', '-',($rep_by) .'-'. str_replace(':','.',$rep_date))) . '.html';
$dompdf = new DOMPDF();
$dompdf->load_html_file($file);
$dompdf->render();
$canvas = $dompdf->get_canvas();
//For the header
$header = $canvas->open_object();
    $font = Font_Metrics::get_font("helvetica", "bold");
    $date = date("Y-m-d H:i:s");
    $canvas->page_text(500, 20, "Page: {PAGE_NUM} of {PAGE_COUNT}", $font, 8, array(0, 0, 0));
    $canvas->page_text(30, 20,  "Crystal Reporting System", $font, 8, array(0, 0, 0));
    $canvas->page_text(300, 20,  $date, $font, 8, array(0, 0, 0));
    $canvas->close_object();
    $canvas->add_object($header, "all");

//For Footer
$footer = $canvas->open_object();
    $font = Font_Metrics::get_font("helvetica", "bold");
    $canvas->page_text(30,750, "$rep_by  $position", $font, 8, array(0, 0, 0));
    $canvas->page_text(440,750,  "crystalreportingsystem.com", $font, 8, array(0, 0, 0));
    $canvas->close_object();
    $canvas->add_object($footer, "all");
$output = $dompdf->output();


file_put_contents('pdf/'. (str_replace(' ', '-',($rep_by) .'-'. str_replace(':','.',$rep_date))). '.pdf', $output);
}
else {

 ?>
<h1>View Latest Reports</h1>
<p> You can now view all reports by clicking on the links on the side menu. </p>

<?php
}
?>

The problem area.

  $_GET['rep_id']>0;
  if ($_GET['rep_id']!=0){
  require ('includes/db.php');
  mysqli_select_db($con, $db_name);
  $sql= 'SELECT * FROM Reports';
  $stmt = $con->prepare($sql);

  $stmt->execute();

  $stmt->bind_result($rep_id, $rep_date, $rep_ledit_date, $rep_by, $rep_type, $department, $position, $report, $rep_to);
  $stmt->fetch();

The line with error. is

 $stmt->execute();

Thanks!


Solution

  • I expect some error checking ought to shed light on the problem. Add or die(mysqli_error($con)) after the mysqli_select_db and $con->prepare functions:

    require ('includes/db.php');
    mysqli_select_db($con, $db_name) or die(mysqli_error($con));
    $sql= 'SELECT * FROM Reports';
    $stmt = $con->prepare($sql) or die(mysqli_error($con));
    
    $stmt->execute();
    

    Regarding the mismatched variables, apparently the order of table columns is not corresponding with the order of the variable assignments. I'm guessing at your table names but I expect this gives you the idea. Change or $sql assignment to:

    $sql= 'SELECT rep_id, rep_date, rep_ledit_date, rep_by, rep_type, department, position, report, rep_to FROM Reports';
    

    Alternatively you could reorder the variable assignments to match the order of the columns in the database table Reports, however the way I showed you above will not break if someone where to add or reorder table columns in the future.

    On a side note, what is the purpose of this line at the top:

    $_GET['rep_id']>0;
    

    I don't see an assignment, or conditional evaluation. Looks like it's doing nothing.