简体   繁体   中英

No results displayed when querying database

I'm trying to create a searchable database using PHP and MySQL. I have a file called mission.html with the following code:

<html>
<body>
<form name="form1" method="post" action="mission1results.php" id="search">
 <input name="search" type="text"/>
<input type="submit" name="submit" vaule="Search"/>
</form> 


mission1results.php

<html>
  <body>

<?php
include 'login.php';
$connection = mysqli_connect(
$db_hostname, $db_username,
$db_password, $db_database);

if(mysqli_connect_error()){
die("Database Connection Failed: " .
  mysqli_connect_error() .
  " (" . mysqli_connect_errno() . ")"
);
}
?>
<?php
$q_cond = mysqli_real_escape_string($_GET['search']);

$query="SELECT * From Merchant Where MerchantName='".$q_cond."'";

$result=mysqli_query($connection,$query);

if ($result===false)
{
    die("Database Query Failed!")
};

while ($row=mysqli_fetch_assoc($result)){
    echo "MerchantName: ".$row["MerchantName"].",";
    echo "<hr/>";
}

mysqli_free_result($result);
?> 
<?php
mysqli_close($connection);
?>
</body>
</html>

When I hit submit and type in anything in the searchbar nothing appears. I don't get an error, I don't get results, its all blank. Can anyone tell me why this is?

You have a syntax error in mission1results.php

if ($result===false)
{
    die("Database Query Failed!")
};

must be changed for:

if ($result===false)
{
    die("Database Query Failed!");
}

First and foremost: mysqli_real_escape_string() requires a DB connection be passed, then there is your form where you are using a POST method in the form and GET for your query.

Consult the manual: http://php.net/manual/en/mysqli.real-escape-string.php

$q_cond = mysqli_real_escape_string($connection,$_POST['search']);

Plus, change

if ($result===false)
{
    die("Database Query Failed!")
};

to

if ($result===false)
{
    die("Database Query Failed!");
}

You also have a syntax error vaule="Search" change it to value


Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Error reporting should only be done in staging, and never production.

Also or die(mysqli_error($connection)) to mysqli_query() to find any possible errors.

  1. Instead $_GET['search'] use $_POST['search'] because your submit forms method is post .

  2. One of mysqli_real_escape_string parameters should be DB connection.

  3. syntax errors in HTML, for example, vaule="Search"

  4. syntax errors in PHP, for example, there shoudn't be ; after } in if

If you are getting a blank screen with the errors pointed out in previous answers you might want to take a look at the PHP error_reporting level on your system http://php.net/manual/en/function.error-reporting.php . You should be seeing PHP errors, on a development server I like to report PHP errors, warnings and notices.

Also, are you expecting users to enter an exact search term? You might want to consider something like:

$query="SELECT * From `Merchant` Where `MerchantName` like '%".$q_cond."%'";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM