PHP:将搜索项插入数据库表的搜索引擎

时间:2016-07-19 21:30:28

标签: php mysqli

我是初学程序员。我正在尝试构建一个PHP搜索引擎,可以将用户发布的搜索词插入到MySQL的另一个表中。

此搜索引擎代码搜索数据库,而不将搜索词插入表中。它运作得很好:

<?php

include ("dbconnect.php");
    $searchtext = trim(mysqli_real_escape_string($con,$_GET['search']));
    $searchbutton = ($_GET['submit']);

    $sql = "SELECT * FROM beverage_db WHERE name LIKE '%".$searchtext."%' ORDER BY name";
    $query = mysqli_query($con, $sql);

    if (mysqli_num_rows($query)>0) {


        $results = mysqli_fetch_assoc($query);
        do {

        echo '<div>'.$results["name"].'</div>';

        }while ($results=mysqli_fetch_assoc($query));
    }else {
      $searchbit = explode("", $_GET['search']);
      foreach ($searchbit as $value) {
        $metabits = ($value);
        $sql = "SELECT * FROM beverage_db WHERE name LIKE '%".$metabits."%' ORDER BY name";
        $query = mysqli_query($con, $sql);
        $results = mysqli_fetch_assoc($query);
        do {

        }while ($results=mysqli_fetch_assoc($query));
     }
  }

?>

现在,当我尝试输入INSERT INTO函数时,它不会将搜索词输入数据库。这是我的尝试:

include ("dbconnect.php");
    $searchtext = trim(mysqli_real_escape_string($con,$_GET['search']));
    $searchbutton = ($_GET['submit']);

    $sql = "SELECT * FROM beverage_db WHERE name LIKE '%".$searchtext."%' ORDER BY name;
    INSERT INTO words (words_used) VALUES ('".mysqli_real_escape_string($con, $_GET['search'])."')";
    $query = mysqli_query($con, $sql);

    if (mysqli_num_rows($query)>0) {


        $results = mysqli_fetch_assoc($query);
        do {

        echo '<div>'.$results["name"].'</div>';

        }while ($results=mysqli_fetch_assoc($query));
    }else {
      $searchbit = explode("", $_GET['search']);
      foreach ($searchbit as $value) {
        $metabits = ($value);
        $sql = "SELECT * FROM beverage_db WHERE name LIKE '%".$metabits."%' ORDER BY name;
        INSERT INTO words (words_used) VALUES ('".mysqli_real_escape_string($con, $_GET['search'])."')";
        $query = mysqli_query($con, $sql);
        $results = mysqli_fetch_assoc($query);
        do {

        }while ($results=mysqli_fetch_assoc($query));
     }
  }

?>

不确定是使用正确的功能,还是正确编写代码。

请帮忙

0 个答案:

没有答案