数据库帮助? PHP初学者?

时间:2017-05-21 12:33:23

标签: php mysql

我有一份到期的作业,我似乎无法进行搜索'数据库。它说我的所有值都是未定义的索引。有人可以帮帮我吗?我是一个初学者

的index.php

<!DOCTYPE html>
<?php require_once "queryDb.php"; 

if (array_key_exists('buisnessname', $_GET))
{
$search = $_GET['buisnessname'];
}
else 

{
  $search = '';
}

$data = getCustomers($search);
?>

  <html>
  <head>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <title>Customers</title>
    </head>
  <body>
    <div class="jumbotron">
      <div class="container">
        <h1>Customers</h1>
      </div>
    </div>
    <div class="container" id="search">
      <form class="form-inline" method=get action="index.php">
        <fieldset>
          <lable>Search:</lable>
          <input type="text" class="form-control" id="title" name="type">
          <input type= submit value="Search" class="btn btn-default">
        </fieldset>
      </form>
    </div>
    <div class="container" id="data">
      <table class="table">
        <thead>
          <th>BUISNESS NAME </th>
          <th>EMAIL</th>
          <th>TYPE</th>
          <th>BUISNESS FEEDBACK</th>

        </thead>

        <?php foreach ($data as $datas)
        {
         echo "<tr>";
         echo "<td>" . $datas["buisnessname"] . "</td>";
         echo "<td>" . $datas["email"] . "</td>";
         echo "<td>" . $datas["type"] . "</td>";
         echo "<td>" . $datas["buisnessfeedback"] . "</td>";
         echo "</tr>";
        }

      ?>

      </table>
    </div>
  </body>
  </html>

queryDB.php

    <?php

       function getCustomers($searchTerm = null) {

          $db = new mysqli("localhost","root","","1621ict");
          $array = [];
          if(!$db){
             echo '<script type="text/javascript">alert("'.$db->error.'");</script>';
          } else {
             //echo "Opened database successfully\n";
          }
          if(!$searchTerm) {
             $sql ='SELECT * from CUSTOMERS;';
          } else {
             $sql ='SELECT * FROM CUSTOMERS WHERE BUISNESSNAME LIKE "'.$buisnessname.'" OR EMAIL LIKE "'.$email.'" OR TYPE LIKE "'.$type.'" OR BUISNESSFEEDBACK  LIKE "'.$buisnessfeedback.'"';
          }
          $ret = $db->query($sql);
          if(!$ret){
            echo $db->error;
            return [];
          } else {
             while($row = $ret->fetch_assoc() ){
                $array[] = $row;
             }
             $db->close();
             return $array;
          }
       }


       function addCustomer($buisnessname, $email, $type, $buisnessfeedback) {

          $db = new mysqli("localhost","root","","1621ict");
          if(!$db){
             echo '<script type="text/javascript">alert("'.$db->error.'");</script>';
          } else {
             //echo "Opened database successfully\n";
          }

          $sql ='INSERT INTO CUSTOMERS (BUISNESSNAME, EMAIL, TYPE, BUISNESSFEEDBACK) VALUES ("'.$buisnessname.'", "'.$email.'", "'.$type.'", "'.$buisnessfeedback.'");';
          $db->query($sql);
       }




?>

DB_Create_Script.sql

CREATE DATABASE IF NOT EXISTS 1621ict;

USE 1621ict;

DROP TABLE IF EXISTS CUSTOMERS;

CREATE TABLE CUSTOMERS
(
CUSTID INTEGER PRIMARY KEY AUTO_INCREMENT,
BUISNESSNAME varchar(64),
EMAIL varchar(64),
TYPE varchar(255),
BUISNESSFEEDBACK varchar(16)
);

INSERT INTO CUSTOMERS (BUISNESSNAME, EMAIL, TYPE, BUISNESSFEEDBACK) 
VALUES ('Motezumas', 'montezumas@gmail.com', 'Promotion', 'Would love to see 
your review! 20% off'),
('Hungry Jacks', 'hungryjacks@hotmail.com', 'Sponsorship', 'Heres a promo 
code for 50% small meals'),
('TacoBell', 'tacobell@gmail.com', 'Promotion', 'We are having 30% off all 
items come join us!');
?>

2 个答案:

答案 0 :(得分:0)

您正在使用未定义的变量。

将搜索词的值分配给每个搜索条件

 if(!$searchTerm) {
             $sql ='SELECT * from CUSTOMERS;';
          } else {
             $sql ='SELECT * FROM CUSTOMERS WHERE BUISNESSNAME LIKE "'.$searchTerm.'" OR EMAIL LIKE "'.$searchTerm.'" OR TYPE LIKE "'.$searchTerm.'" OR BUISNESSFEEDBACK  LIKE "'.$searchTerm.'"'; //<--------CHANGE THIS
          }

答案 1 :(得分:0)

只需将您的功能更改为此功能,并在调用时传递相关参数

index.php

然后在<!DOCTYPE html> <?php require_once "queryDb.php"; if (isset($_GET) && isset($_GET['type'])){ $data = getCustomers($_GET['type']); }else { $data = getCustomers(); } ?> <html> 的顶部将代码更改为如下所示。

foreach

将index.php中的<?php foreach ($data as $datas) { echo "<tr>"; echo "<td>" . $datas["BUISNESSNAME"] . "</td>"; echo "<td>" . $datas["EMAIL"] . "</td>"; echo "<td>" . $datas["TYPE"] . "</td>"; echo "<td>" . $datas["BUISNESSFEEDBACK"] . "</td>"; echo "</tr>"; } ?> 循环更改为

   0:   31 c0                   xor    %eax,%eax
   2:   85 ff                   test   %edi,%edi
   4:   0f 95 c0                setne  %al
   7:   c3                      retq