准备好的语句执行致命错误和变量以及数据库表未命中匹配

时间:2014-06-06 15:57:44

标签: binding prepared-statement

我收到以下错误致命错误:在第237行的/home/wt/public_html/view-reports.php中的非对象上调用成员函数execute()。

我也看到了另一个问题。生成的变量不会回显正确的相应数据。例如,$ rep_type回显位置。你能帮帮我吗?谢谢!测试网站网址在这里。 ethioserver.com/~wt/view-reports.php?rep_id=144问题在wampserver中不可见。两个服务器中的php版本是5.4。

主要代码如下。

<?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
}
?>

问题区域。

  $_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();

有错误的行。是

 $stmt->execute();

谢谢!

1 个答案:

答案 0 :(得分:0)

我希望一些错误检查应该能够解决这个问题。在or die(mysqli_error($con))mysqli_select_db函数后添加$con->prepare

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();

关于不匹配的变量,显然表列的顺序与变量赋值的顺序不对应。我猜你的桌子名字,但我希望这会给你一个想法。更改或$sql分配到:

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

或者,您可以对变量赋值重新排序以匹配数据库表Reports中列的顺序,但是,如果有人将来添加或重新排序表列,我上面展示的方式将不会中断。

另一方面,这条线在顶部的目的是什么:

$_GET['rep_id']>0;

我没有看到作业或条件评估。看起来它什么都不做。