0
Code ok !
-:PDO:- correct code working
header('Content-type: application/json; charset=UTF-8');
$response = array();
if ($_POST['delete']) {
require_once 'dbconfig.php';
$pid = intval($_POST['delete']);
$query = "DELETE FROM tbl_products WHERE product_id=:pid";
$stmt = $DBcon->prepare( $query );
$stmt->execute(array(':pid'=>$pid));
if ($stmt) {
$response['status'] = 'success';
$response['message'] = 'Product Deleted Successfully ...';
} else {
$response['status'] = 'error';
$response['message'] = 'Unable to delete product ...';
}
echo json_encode($response);
}
Bug code does not work
header('Content-type: application/json; charset=UTF-8');
$response = array();
if ($_POST['delete']) {
require_once 'dbconfig2.php';
$pid = intval($_POST['delete']);// return form js
$query = "DELETE FROM tbl_products WHERE product_id=:pid";
$rs = mysqli_query($conn,$query);
$resArr = array();
while($row = mysqli_fetch_assoc($rs)) {
$rs= (array(':pid'=>$pid));
}
if ($rs) {
$response['status'] = 'success';
$response['message'] = 'Product Deleted Successfully ...';
} else {
$response['status'] = 'error';
$response['message'] = 'Unable to delete product ...';
}
echo json_encode($response);
}
Switching from PDO to Mysql is a great initiative, only the methodology changes a little. One of the things you need to do is change the placeholder
:pid
for?
, and instead ofmysqli_query
, needs amysql_prepare
and a "bind" to associate the?
at the$pid
. A tip in the PHP manual itself will have a number of explanations and examples. As it became too wide in the way you posted (various problems in the code instead of a specific problem) I will try to locate a website link that can help.– Bacco
If the link post example is not enough for you to adapt to your case, please [Dit] the post explaining which part of the code exactly is in doubt.
– Bacco
Has a material also that I used at the beginning (learning PDO) and helped me a lot. Who knows help you too. Note: is in English.
– João Pedro Schmitz