0

I have built my first php search form, it searches inside a mysql archive.

The archive contains items with a Name (Nome), Country (Paese), City (Città) and Area.

I have 5 items:

  • Iberflora - Valencia - Spagna - Europa
  • Intergift - Madrid - Spagna - Europa
  • Foodex Japan - Chiba - Giappone - Asia
  • Jewels China - Hong Kong - Cina - Asia
  • UsFauna - New York - USA - Nordamerica

There are two search fields, one for the area and the other for the country.

I am experimenting and I can't understand why this code will produce these results:

  • "asia" + "" => 2 results, correct
  • "europa" + "" => 2 results, correct
  • "europa" + "spagna" => 2 results, correct
  • "" + "spagna" => 2 results, correct

All other combinations, such as "nordamerica" "" or "asia" "cina" give 0 results, which is obviously wrong...

So this is the code, I hope someone can help me.

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$search_output = "";
if((isset($_POST['zonequery']) && $_POST['zonequery'] != "") || (isset($_POST['countryquery']) && $_POST['countryquery'] != "")){
$zonequery = preg_replace('#[^a-z 0-9?!]#i', '', $_POST['zonequery']);
$countryquery = preg_replace('#[^a-z 0-9?!]#i', '', $_POST['countryquery']);

        $sqlCommand = "SELECT Area, Paese, Città, Nome FROM fiere WHERE Area LIKE '%$zonequery%' AND Paese LIKE '%$countryquery%'";     

        // Connect to your MySQL database here
        // Create connection
        $link=mysqli_connect("127.0.0.1","root","","fiere");

        // Check connection
        if (mysqli_connect_errno($link))
          {
          echo "Failed to connect to MySQL: " . mysqli_connect_error();
          }
        $query = mysqli_query($link, $sqlCommand) or die(mysql_error());
$count = mysqli_num_rows($query);
if($count > 1){
$search_output .= "<hr />$count risultati per <strong>$zonequery</strong> o <strong>$countryquery</strong><hr />";
while($row = mysqli_fetch_array($query)){
   $zone = $row["Area"];
   $country = $row["Paese"];
   $city = $row["Città"];
   $name = $row["Nome"];
   $search_output .= "Fiera: $name - $city - $country - $zone<hr />";
                } // close while
} else {
$search_output = "<hr />0 risultati per <strong>$zonequery</strong> o <strong>$countryquery</strong>";
}
}
?>
<html>
<head>
</head>
<body>
<h2>Ma quante belle fiere Madama Doré</h2>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Cerca Area: 
  <input name="zonequery" type="text" size="44" maxlength="88">
  <br />
Cerca Paese:
  <input name="countryquery" type="text" size="44" maxlength="88">
  <br />
<input name="myBtn" type="submit">
<br />
</form>
<div>
<?php echo $search_output; ?>
</div>
</body>
</html>

EDIT: this is a dump (I hope) of the table, as requested:

Archive containing a dump of the table in sql format

EDIT 2: also, I added a line to print on screen the sql command which is actually executed. Here's what I got for, for instance, "" + "cina"

So what the heck are those percentage signs?

4

0 回答 0