I have made a MySQL script to query a table with a set of id using an IN clause and after some reading I found out that there are security issues with it like prone to SQL injections and the likes.
Are there other ways to write this code with a more secure way? Although I'm using PDO, is that really enough?
public function GetAllCustomerFilter($search, $cat_id)
{
$stmt = $this->db->prepare("SELECT * from users_cards WHERE cat_id IN ($cat_id) AND username LIKE :search ORDER BY id DESC LIMIT 60";
$stmt->bindParam(":search", $search);
if($stmt->execute()) {
$products = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
}
Data is received from an Ajax:
loadProductsListPerCat = function(form,data_serve) {
var data_fed = new Array();
$.each(data_serve, function(i,obj)
{
data_fed[i] = obj.cat_id;
});
var raw = data_fed.join();
var encoded = encodeURIComponent(raw);
var formData = "data_serve="+encoded+"&"+ $(form).serialize();
var requestURL = 'searchUsers';
console.log(formData);
$.ajax({
url: requestURL,
data: formData,
crossDomain: true,
dataType: 'json',
cached: false,
async: true,
success: function(data) {
console.log(data);
},
complete: function() {
if(isAppend) {
$('.btn-toggle-load-more-users').attr('disabled', false);
}
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
}
});
};
The variable data_serve is taken by another jQuery function on which it counts input elements that has data-on set to true:
var PCatVal = (function(){
var cats = new Array();
$("#main_cat").find(".option_").each(function(index) {
$(this).removeClass("option_");
$(this).addClass("ctrls option_"+index);
$(".option_"+index).on("click", function(e){
var opt = $(this).data('parentname');
var opt_id = $(this).val();
var data_serve = [];
//array check if exists
var idx = $.inArray(opt_id, cats);
if (idx == -1) {
cats.push(opt_id);
} else {
cats.splice(idx, 1);
}
//loop the array so to encode to a json format
$.each(cats, function(i,obj){
data_serve.push({"cat_id": obj});
});
//Switch for the category buttons
var swtch_var = $(this).attr('data-on');
if(swtch_var == 0)
{
swtch_var = 1;
}
else
{
swtch_var = 0;
}
$(this).attr('data-on',swtch_var);
loadUsers($('#frmSearchUsers'),'',data_serve);
e.preventDefault();
});
});
});
This is how the $_GET parameters are received:
public function searchUser()
{
$cat_id = (isset($_GET['data_serve']) && !empty($_GET['data_serve'])) ? $_GET['data_serve'] : null;
$search_filter = (isset($_GET['search_filter']) && !empty($_GET['search_filter'])) ? $_GET['search_filter'] : null;
$products = $this->model->GetAllCustomerFilter($search_filter, $cat_id);
return $products;
}
$cat_idis put together. Please add more context. My natural advice now would be to replace the$cat_idstring with an array and work with that. It's possible that that should be done outside the current function. Or perhaps the current function should be rewritten not to use$cat_idor ... \$\endgroup\$$_POSTdata retrieved and then fed to the function? \$\endgroup\$