Ich versuche, ein Programm zu entwickeln, die in MySQL-Datenbank suchen und die Suchergebnisse zurückgeben, aber wenn ich das Senden trifft, zeigt es alle Ergebnisse an, die ich nicht weiß, warum das ist geschieht, so wird jede HilfePHP-Suchskript in Mysql-Datenbank dosen't Arbeit
<?php
include('connect2.php');
?>
<?php
echo "<center>";
echo "<table border = '3'>";
echo "<thead>";
echo "<tr>";
echo "<th><u>id</u></th>";
echo "<th><u>name</u></th>";
echo "<th><u>countrycode</u></th>";
echo "<th><u>district</u></th>";
echo "<th><u>population</u></th>";
echo "</center>";
echo "</tr>";
echo "</thead>";
$name = isset($_POST['search']) ? $_POST['search'] : '';
$result = mysqli_query($conn, "SELECT id, name, district, population, countrycode FROM city WHERE concat(id, name, district, population, countrycode) LIKE '%$name%' ");
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_array($result)) {
echo "<tr><td>" . $row['id'] . "</td><td>" . $row['name'] . "</td><td>" . $row['countrycode'] . "</td><td>" . $row['district'] . "</td><td>" . $row['population'] . "</td></th>";
}
}
else {
header("Location:error page.html"); die;
}
echo "</table>";
mysqli_close($conm);
?>
die Suchseite search.php
<?php
include('connect2.php') ;
?>
<!DOCTYPE html>
<head>
<title>city results</title>
<link href="style-table.css" rel="stylesheet">
<link href="animate.css" rel="stylesheet">
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
<script>
function validateform() {
var x = document.forms["myform"]["input"].value;
if (x == "") {
alert("input must be filled out");
return false;
}
}
</script>
</head>
<body>
<form method="POST" action="display-results-city.php" name="myform" onsubmit="return validateform()">
<input type="text" name="input" placeholder="search.........">
<button name="submit" type="submit">go</button>
</form>
<?php
echo '<div class="animated fadeInUp">';
echo '<a href="search.php"><i class="fa fa-arrow-left fa-lg" aria-hidden="true" ></i></a>';
echo "<div id='records'>";
echo "<table border = '0'>";
echo "<tr>";
echo "<th>ID</th>";
echo "<th>name</th>";
echo "<th>countrycode</th>";
echo "<th>district</th>";
echo "<th>population</th>";
echo "</tr>";
echo "</div>";
echo '</div>';
$sql = "select * from city limit 50";
$result = $conn->query($sql);
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row['ID']. "</td><td>" . $row['Name']. "</td><td>" . $row['CountryCode'] . "</td><td>" . $row['District'] . "</td><td>" . $row['Population'] . "</td></tr>";
}
} else {
echo " 0 results";
}
echo '</table>';
$conn->close();
?>
</body>
</html>
Siehe 'name =" input "' vs. '$ _POST ['search']'. Verwenden Sie Fehlerberichte und ich denke, dass Sie eine undefinierte variable Notiz erhalten werden. Sie sind auch offen für SQL-Injektionen. Verwenden Sie parametrisierte Abfragen. Ihr 'header' wird auch fehlschlagen, weil Sie vor dem' header'-Aufruf ausgeben. Schlussbemerkung, https://dev.mysql.com/doc/refman/5.7/en/fulltext-search.html. – chris85
** WARNUNG **: Wenn Sie 'mysqli' verwenden, sollten Sie [parametrisierte Abfragen] (http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) und [' bind_param'] (http://php.net/manual/en/mysqli-stmt.bind-param.php), um Benutzerdaten zu Ihrer Abfrage hinzuzufügen. ** Verwenden Sie NICHT ** String-Interpolation oder Verkettung, um dies zu erreichen, weil Sie einen schwerwiegenden [SQL injection bug] erstellt haben (http://bobby-tables.com/). ** NIEMALS ** $ _POST ',' $ _GET' oder ** irgendwelche ** Benutzerdaten direkt in eine Abfrage einfügen, es kann sehr schädlich sein, wenn jemand versucht, Ihren Fehler auszunutzen. – tadman
Mögliches Duplikat von [PHP: "Notice: Undefinierte Variable", "Notice: Undefinierter Index" und "Notice: Undefined Offset"] (http://stackoverflow.com/questions/4261133/php-notice-undefined-variable- notice-undefined-index-und-notice-undef) – chris85