Ich versuche, eine Live-Suche zu tun. Ich habe eine HTML-Seite mit einem Eingabewert. Die Eingabe übernimmt saleIDs. Dann habe ich eine PHP-Datei, die eine Verbindung zu einer MySQL-Datenbank herstellt. Ich möchte die Datenbank nach Verkaufs-IDs suchen und eine HTML-Tabelle basierend auf dem, was in der Eingabe eingegeben wurde, aktualisieren. Ich habe den Großteil des Codes jedoch nicht gefunden, es wurde nichts gesagt.Live Search PHP und MySQL
Zuerst werde ich meine HTML geben:
<html>
<head>
<title></title>
<script src="jquery-1.11.1.js"></script>
<script>
$(document).ready(function()
{
load_data();
function load_data(query)
{
$.ajax({
url:"test.php",
method:"POST",
data:{query:query},
success:function(data)
{
$('#result').html(data);
}
});
}
$('#saleID').keyup(function()
{
var search = $(this).val();
if(search != '')
{
load_data(search);
}
else
{
load_data();
}
});
});
</script>
</head>
<body>
</body>
</html>
<h1>Test</h1>
<br>
<br>
<form>
<p>Customer ID:</p>
<input type="text" id="saleID" name="Sale">
<br>
<br>
</form>
<div id="result"></div>
</body>
</html>
hier ist dann meine test.php Datei
<?php
header("Cache-Control: post-check=1, pre-check=2");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
$choice = $_GET['qu'];
$output = '';
$con = mysqli_connect("localhost", "milkiewiczr520", "", "milkiewiczr520") or
die("Failed to connect to database");
$sql_command = "SELECT * FROM SALES WHERE CUSTOMERID = " . $choice . ";";
$result = mysqli_query($con, $sql_command);
if(mysqli_num_rows($result) > 0)
{
$output .= '
<div>
<table>
<tr>
<th>Sale ID</th>
<th>Sale Date</th>
<th>Customer ID</th>
</tr>
';
while($row = mysqli_fetch_array($result))
{
$output .= '
<tr>
<td>'.$row["SALEID"].'</td>
<td>'.$row["SALEDATE"].'</td>
<td>'.$row["CUSTOMERID"].'</td>
</tr>
';
}
echo $output;
}
else
{
echo 'Data Not Found';
}
?>
Im immer gefunden keine Daten.
Irgendwelche Ideen?
Ihr Skript ist gefährdet von [SQL Injection Attack] (http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Werfen Sie einen Blick auf, was passiert ist [Little Bobby Tables] (http://bobby-tables.com/) Sogar [wenn Sie Eingaben entgehen, ist es nicht sicher!] (Http://stackoverflow.com/questions/5741187/sql-injection-that-gets -around-mysql-real-escape-string) Verwenden Sie [vorbereitete parametrisierte Anweisungen] (http://php.net/manual/en/mysqli.quickstart.prepared-statements.php). –