PHP fetch_array和UNION中的MYSQLi搜索表单

时间:2019-01-27 22:40:41

标签: php mysql

我对MYSQL / PHP很陌生。此外,有些问题似乎对我的问题来说很熟悉,但是从我的角度来看,没有一个主题可以完全涵盖它。

我想创建一个搜索输入,它将搜索两个不同的表,并返回几对与输入匹配的对应值。这两个表包含在数据库“ mydb1”中。

请附加result I receive in the browser,mysqli_query()命令似乎有问题。

我的“ Test_SQL.php”包含以下内容:

<?php

error_reporting(E_ALL);
ini_set('display_errors', '1');

$search_output = "";


if(isset($_POST['search_8_1']) && $_POST['search_8_1'] != ""){
    $search_8_1 = preg_replace('#[^a-z]#i', '', $_POST['search_8_1']);
    $sqliCommand = "(SELECT type, price FROM provider1 WHERE location LIKE '%search_8_1') 
                        UNION (SELECT type, price FROM provider2 WHERE location LIKE '%search_8_1')";
    include_once("connect_to_mysql.php");
    $query = mysqli_query($sqliCommand) or die(mysqli_error());
    $count = mysqli_num_rows($query);
    if($count > 1){
     $search_output .= "<hr /> $count Results found <hr /> $sqliCommand <hr />";
     while($row = mysqli_fetch_array($query)){
         $type = $row ["type"];
         $price = $row ["price"];
         $search_output .= "Type: $type - $price<br />";
     }
    } else {
     $search_output = "<hr /> No Results found <hr /> $sqliCommand";
    }
}
?>

<html>

<head>

<title>Test_SQL</title>

</head>

<body>
<form action="Test_SQL.php" method="post"> 
     <input name="search_8_1">  
     <input type="submit" name="button_1" value="Go!">
</form>

<div>
<?php echo $search_output; ?>
</div>
</body>
</html>

我的“ connect_to_mysql.php”包含以下内容:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$database= "mydb1";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $database);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?> 

但是,一旦我在浏览器中打开“ Test_SQL.php”,它就已经看起来一团糟,搜索根本无法进行。

非常感谢您的任何暗示!

1 个答案:

答案 0 :(得分:-1)

从isset条件中删除include并像我向您展示的那样将其放在顶部,使用require,因为它可以被多次调用。

<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);
require("connect_to_mysql.php");

if(isset($_POST['search_8_1']) && $_POST['search_8_1'] != ""){
    $search_8_1 = preg_replace('#[^a-z]#i', '', $_POST['search_8_1']);
        $sqlCommand = "(SELECT type, price FROM provider1 WHERE location LIKE '%search_8_1') 
                        UNION (SELECT type, price FROM provider2 WHERE location LIKE '%search_8_1')";


    $query = mysqli_query($sqlCommand) or die(mysqli_error());
    $count = mysqli_num_rows($query);
    if($count > 1){
     $search_output = "<hr /> $count Results found <hr /> $sql_command";
     while($row = mysqli_fetch_array($query)){
         $type = $row ["type"];
         $price = $row ["price"];
         $search_output .= "Type: $type - $price<br />";
     }
    } else {
     $search_output = "<hr /> No Results found <hr /> $sql_command";
    }
}
?>

<?php
$servername = "localhost";
$username = "root";
$password = "";
$database= 'mydb1';

// Create connection
$conn = mysqli_connect($servername, $username, $password,$database);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>