【问题标题】:Perl, HTML and MySQL where did my program break? [closed]Perl、HTML 和 MySQL 我的程序哪里出错了? [关闭]
【发布时间】:2013-05-11 01:16:08
【问题描述】:

是的,我是一名学生,是的,这是一项作业,但我只是需要一些帮助来解决问题。我不打算让别人帮我做作业。

所以我有一个包含 5 本书的数据库,其中包含 6 个不同的数据列。我正在尝试制作一个可以搜索该数据库并在表中返回结果的 perl 程序。我必须添加一种将它们添加到购物车的方法,但这将在稍后进行。

Perl 的问题是我不知道如何检查为什么会出现“内部服务器错误”。该应用程序进入 Perl 页面,所以我猜不是这样。

    #!/usr/bin/perl

use warnings; # allow for warnings to be sent if error's occur
use CGI qw( :standard ); # not a 100% sure what the rest of these mean but they are like #includs in C++, libraries for reference in the code
use DBI;
use DBD::mysql; #database data will come from mysql

my $dbh = DBI->connect( "DBI:mysql:DATABASE NAME", "USERNAME", "PASS REMOVED" ) or
   die( "Could not make connection to database: $DBI::errstr" );   # connect to the database with address and pass or return error

   $term = $SEARCHTERM[]; #set the search char to $term

   $term =~ tr/A-Z/a-z/;  #set all characters to lowercase for convenience of search

   $sql = "SELECT * FROM Books WHERE Title LIKE %$term% OR Description LIKE %$term% OR Author LIKE %$term%" or die ("$term may have not worked");            #set the query string to search the database

   $sth = $dbh->prepare($sql);  # prepare to connect to the database

   $sth->execute   # connect to the database
 or die "SQL Error: $DBI::errstr\n";   #or return an error
 while (@data = $sth->fetchrow_array) {   #while we are grabbing he queried data do a table setup and set variables for table

print "<table width=\"100%\" border=\"0\"> ";

    $title = $data[0];
    $desc = $data[1];
    $author = $data[2];
    $pub = $data[3];
    $isbn = $data[4];
    $photo = $data[5];

    print "<tr> <td width=50%>Title: $title</td> <td width=50% rowspan=5>$photo</td></tr><tr><td>Discreption Tags: $desc</td></tr><tr><td></td></tr><tr><td>Author: $author</td></tr><tr><td>ISBN: $isbn</td>
</tr></table> \n";



 }  

请帮忙!

【问题讨论】:

  • Stack Overflow 不适合回答这个问题。我们不进行代码调试。您需要进行自己的调试,如果您不确定为什么某些事情没有按预期工作,请发布 relevant 代码(如果您发布了一堵代码,那么您还没有完成足够的调试您自己的),并说明您期望它做什么以及它实际上在做什么,包括所有错误消息。见about Stack Overflow
  • 听起来真正的问题是“我如何在 perl 脚本中追踪 501 错误?”,这对于 SO 来说似乎是完全合理的。也许检查你的 apache 日志?
  • 所有 CGI 程序都必须在打印其他任何内容之前发出一个标题。最小的标头是print "Content-type: text/html\n\n",但最好使用CGI 中的header 方法——参见概要示例。如果 die 被执行,你会在日志中找到消息,而客户端得到 500

标签: mysql perl error-handling internal-server-error


【解决方案1】:

早些时候,有人建议,

my $sql = "
   SELECT *
     FROM Books
    WHERE Title LIKE '%$term%'
       OR Description LIKE '%$term%'
       OR Author LIKE '%$term%'
";
my $sth = $dbh->prepare($sql);
$sth->execute();

假设他对你的问题是正确的,那是不正确的。考虑一下如果有人搜索标题“Foo's Bar”会发生什么......

选项 1:

my $sql = '
   SELECT *
     FROM Books
    WHERE Title LIKE '.$dbh->quote("%$term%").'
       OR Description LIKE '.$dbh->quote("%$term%").'
       OR Author LIKE '.$dbh->quote("%$term%").'
';
my $sth = $dbh->prepare($sql);
$sth->execute();

选项 2:

my $sql = '
   SELECT *
     FROM Books
    WHERE Title LIKE ?
       OR Description LIKE ?
       OR Author LIKE ?
';
my $sth = $dbh->prepare($sql);
$sth->execute("%$term%", "%$term%", "%$term%");

【讨论】:

    【解决方案2】:

    首先,我还强烈建议将use strict; 添加到脚本顶部,然后修复您将遇到的错误,主要是使用my 声明您的变量。

    我还可以建议使用现代框架,比如Mojolicious,而不是使用非常过时的 CGI 模块。它们都可以在类似 CGI 的环境下运行,并且更易于使用!

    最后,您应该避免将变量插入 SQL 字符串,因为这会带来巨大的安全风险!试试这个网站了解更多关于这个主题的信息:http://bobby-tables.com/

    【讨论】:

      【解决方案3】:

      您的选择无效。改变

      $sql = "SELECT * FROM Books WHERE Title LIKE %$term% OR Description LIKE %$term% OR Author LIKE %$term%"
      

      $sql = "SELECT * FROM Books WHERE Title LIKE '%$term%' OR Description LIKE '%$term%' OR Author LIKE '%$term%"'
      

      LIKE 的模式文字应该被引用。

      【讨论】:

      • 谢谢,我不确定这些文字的语法
      • Joel 在他的回答中正确添加了指向bobby-tables.com 的链接。上面的代码可能有效,但任何人都不应该使用它。