I’m currently using jquery’s ajax feature or whatever they call it. To load data from mysql database. Its working fine, but one of the built in features of this one is to load all the data which is on the database when you press on backspace and there’s no character left on the text box.
Here’s my query:
SELECT * FROM prod_table WHERE QTYHAND>0 AND PRODUCT LIKE '$prod%' OR P_DESC LIKE '$desc%' OR CATEGORY LIKE '$cat%'
As you can see I only want to load the products which has greater than 0 quantity on hand.
I’m using this code to communicate to the php file which has the query on it:
$('#inp').keyup(function(){
var inpval=$('#inp').val();
$.ajax({
type: 'POST',
data: ({p : inpval}),
url: 'querys.php',
success: function(data) {
$('.result').html(data);
}
});
});
Is it possible to also filter the data that it outputs so that when I press on backspace and there’s no character left. The only products that’s going to display are those with greater than 0 quantity?
I think all you have to do is to change your query to:
Not 100% sure if it is the same in SQL, but most often,
ANDhas precedence overOR. So your original query would read like:Now, if you have an empty string, then
"something LIKE '%'"will always match and only oneORhas to match to include the record in the result set.