Pdo code help for mysqli

Asked

Viewed 29 times

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);
}
  • 1

    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 of mysqli_query, needs a mysql_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.

  • 1

    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.

  • 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.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.