Home MySQL query not outputting result only heading
Reply: 2

MySQL query not outputting result only heading

BobK
1#
BobK Published in 2017-12-08 00:03:17Z

I am currently practicing PHP and AJAX and I am trying to create an application that allows a user to search a database for product stock in real time. When the user types in a letter within one of the entries the headings show up but not the data. I'm not sure if I am doing it wrong but I'll let you guys be the judge of that. Code below:

Results.php - Below

 <!DOCTYPE html>
<html>
<head>
<title>Results</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>

<?php
$q = intval($_GET['q']);

$con = mysqli_connect("localhost", "username", "password", "databaseName");
//edited out for safety 

if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}

mysqli_select_db($con,"jakeholc_ajaxtest");

$sql="SELECT * FROM stock WHERE name LIKE '%{$q}%'";
$result = mysqli_query($con,$sql);

echo "<table>
<tr>
<th>Product name</th>
<th>Description</th>
<th>Price</th>
<th>Quantity</th>
</tr>";

if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['descr'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "<td>" . $row['quantity'] . "</td>";
echo "</tr>";
}
}


echo "</table>";
mysqli_close($con);
?>

Nerds of Technology
2#
Nerds of Technology Reply to 2017-12-08 00:13:48Z

$q = intval($_GET['q']); attempts to turn your input into an integer. Remove this line.

not_null
3#
not_null Reply to 2017-12-08 08:50:59Z

you may try my code. I already tested this one. And everytime you entered a text, entered text will match some record from your database then if there's a record matched, then it will display the record matched on your entered text. Hope this helps :) Please note that the below code are the content of "ajax_search.php".

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("#search").keyup(function() {
var name = $('#search').val();
if (name == "") {
$("#display").html("");
}
else {
$.ajax({
type: "POST",
url: "ajax_search.php",
data: {
search: name
},
success: function(html) {
$("#display").html(html).show();
}
});
} 
}); 
});
</script>   
</head>

<body>
<input type="text" id="search" placeholder="Search Product Here..." />
<?php
$con = mysqli_connect("localhost","username","password","databasename"); 
if (mysqli_connect_errno()) {
echo "Database Connection Failed: " . mysqli_connect_error();
}
?>
<?php
if (isset($_POST['search'])) {
$key = $_POST['search'];
$query = "SELECT * FROM stock  WHERE name LIKE '%$key%' OR descr LIKE '%$key%' OR price LIKE '%$key%' OR quantity LIKE '%$key%' ";
$execute_query = mysqli_query($con, $query);
echo "<table border='3px' cellpadding='3px' cellspacing='3px' bgcolor='skyblue' align='center'>";
echo "<tr>";
echo "<td>Product Name</td>";
echo "<td>Description</td>";
echo "<td>Price</td>";
echo "<td>Quantity</td>";
echo "</tr>";
while ($result = mysqli_fetch_array($execute_query)) {
echo "<tr>";
echo "<td>".$result['name']."</td>";
echo "<td>".$result['descr']."</td>";
echo "<td>".$result['price']."</td>";
echo "<td>".$result['quantity']."</td>";
echo "</tr>";
}}
mysqli_close($con);
?>
</table>
<div id="display">
<!-- Ouput will display here -->
</div>
</body>
</html>
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.303625 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO