0

我试图将它组织成一个有自己列的表格,但每次我尝试时,我都会得到一些非常混乱或完全错误的东西。如果我能从 SQL 中获得一些帮助,那就太好了。例如看这张图片:

http://bfast.elementfx.com/design/table.JPG

这是代码

$dbHost = 'localhost'; // localhost will be used in most cases
$dbUser = 'root'; 
$dbPass = 'root';
$dbDatabase = 'root'; // the database you put the table into.
$con = mysql_connect($dbHost, $dbUser, $dbPass) or trigger_error("Failed to connect to MySQL Server. Error: " . mysql_error());

mysql_select_db($dbDatabase) or trigger_error("Failed to connect to database {$dbDatabase}. Error: " . mysql_error());

$error = array();
$results = array();

if (isset($_GET['search'])) {
   $searchTerms = trim($_GET['search']);
   $searchTerms = strip_tags($searchTerms); // remove any html/javascript.

   if (strlen($searchTerms) < 2) {
      $error[] = "Search terms must be longer than 2 characters.";
   }else {
      $searchTermDB = mysql_real_escape_string($searchTerms); // prevent sql injection.
   }

   // If there are no errors, lets get the search going.
   if (count($error) < 1) {
      $searchSQL = "SELECT brandname, manufacturer, distributor, modelnumber, date, expirey FROM productlist WHERE ";

      // grab the search types.
      $types = array();
      $types[] = isset($_GET['Brand Name'])?"`brandname` LIKE '%{$searchTermDB}%'":'';
      $types[] = isset($_GET['Manufacturer'])?"`manufacturer` LIKE '%{$searchTermDB}%'":'';
      $types[] = isset($_GET['distributor'])?"`distributor` LIKE '%{$searchTermDB}%'":'';
      $types[] = isset($_GET['modelnumber'])?"`modelnumber` LIKE '%{$searchTermDB}%'":'';
      $types[] = isset($_GET['E'])?"`E` LIKE '%{$searchTermDB}%'":'';
      $types[] = isset($_GET['F'])?"`F` LIKE '%{$searchTermDB}%'":'';

      $types = array_filter($types, "removeEmpty"); // removes any item that was empty (not checked)

      if (count($types) < 1)
         $types[] = "`brandname` LIKE '%{$searchTermDB}%'"; // use the body as a default search if none are checked

          $andOr = isset($_GET['matchall'])?'AND':'OR';
      $searchSQL .= implode(" {$andOr} ", $types) . " ORDER BY `brandname`"; // order by title.

      $searchResult = mysql_query($searchSQL) or trigger_error("There was an error.<br/>" . mysql_error() . "<br />SQL Was: {$searchSQL}");

      if (mysql_num_rows($searchResult) < 1) {
         $error[] = "The search term provided {$searchTerms} yielded no results.";
      }else {
         $results = array(); // the result array
         $i = 1;
         while ($row = mysql_fetch_assoc($searchResult)) {
            $results[] = "{$i}: <b>Brand Name:</b>  {$row['brandname']}<br /><b>Distributor:</b>  {$row['distributor']}<br /><b>Manufacturer:</b>  {$row['manufacturer']} <br /><b>Model Number:</b>  {$row['modelnumber']}<br /><b>Certifying Agency:</b>  {$row['E']} {$row['F']}<br /><br /><br />";
            $i++;
         }
4

2 回答 2

0

你应该更具体,我真的不明白你在问什么。但作为一个起点,从您的代码中可以看出,在您的最后一个“else”语句中缺少一个结尾大括号“}”,这可能会造成问题。你说:“我试图把它组织成一个有自己的列的表格,但每次我尝试都会得到一些非常混乱或完全错误的东西”,但是“这个”是什么,如果你不这样做,我们应该如何提供帮助' t描述您所说的“完全错误的混乱”是什么意思?这可能意味着什么...

于 2013-03-21T06:23:06.063 回答
0

错误在于在$andOr变量处提供空间,请参阅

$andOr = isset($_GET['matchall'])?'AND':'OR'; // You cannot implode them without giving space
$searchSQL .= implode(" {$andOr} ", $types) . " ORDER BY `brandname`";

固定代码是:

$andOr = isset($_GET['matchall'])?' AND ':' OR '; // I give space to them
$searchSQL .= implode($andOr, $types) . " ORDER BY `brandname`"; // I call $andOr variable directly without `{}`

那么,是否仍然发现任何错误?

于 2013-03-21T06:38:54.610 回答