Search code examples
phppdoinsert-into

PDO INSERT INTO [No error in BindingParam but no data is sent to the database


$sqlPickInsert = "INSERT INTO record_pickup(pickup_id, order_code, customer_id,
                                s_date, r_date, payment_amount, payment_mode, 
                                pickup_comments, branch, order_status) 
                               VALUES (?,?,?,?,?,?,?,?,?,?)";

    echo "$sqlPickInsert <br>"; 
    echo "pickupID $nullvalues" ."<br>";
    echo "ordercode $newPickupCode" ."<br>";
    echo "customer ID $finalexistCustID" ."<br>";
    echo "loginDate $login_Date" ."<br>";
    echo "pickup $finalpick_date" ."<br>";
    echo "amount $newExpenditures" ."<br>";
    echo "paymode $paymentMode" ."<br>";
    echo "notes$finalpick_notes" ."<br>";
    echo "login branch$login_Branch" ."<br>";
    echo "status $nullvalues" ."<br>";

    $stmt1 = $conn->prepare($sqlPickInsert);
    if (!$stmt) die ('prepare() failed!');

    $checkID = $stmt1->bindParam(1, $nullvalues,PDO::PARAM_INT); //id
    if (!$checkID) die ('bindParam() ID failed!<br>');

    $checkCODE=$stmt1->bindParam(2, $newPickupCode,PDO::PARAM_STR);//ordercode
    if (!$checkCODE) die ('bindParam() ID failed!<br>');

    $checkCUSTID= $stmt1->bindParam(3, $finalexistCustID,PDO::PARAM_STR);//customerid
    if (!$checkCUSTID) die ('bindParam() ID failed!<br>');

    $checkSDATE=$stmt1->bindParam(4, $login_Date,PDO::PARAM_STR);//s_date
    if (!$checkSDATE) die ('bindParam() ID failed!<br>');

    $checkRDATE=$stmt1->bindParam(5, $finalpick_date,PDO::PARAM_STR);//r_date
    if (!$checkRDATE) die ('bindParam() ID failed!<br>');

    $checkAMOUNT=$stmt1->bindParam(6, $newExpenditures,PDO::PARAM_STR);//amount
    if (!$checkAMOUNT) die ('bindParam() ID failed!<br>');

    $checkPAYMODE=$stmt1->bindParam(7, $paymentMode,PDO::PARAM_STR);//paymode
    if (!$checkPAYMODE) die ('bindParam() ID failed!<br>');

    $checkNOTE=$stmt1->bindParam(8, $finalpick_notes,PDO::PARAM_STR);//note
    if (!$checkNOTE) die ('bindParam() ID failed!<br>');

    $checkBRANCH=$stmt1->bindParam(9, $login_Branch,PDO::PARAM_STR);//branch
    if (!$checkBRANCH) die ('bindParam() ID failed!<br>');

    $checkSTATUS=$stmt1->bindParam(10, $nullvalues,PDO::PARAM_STR);//status
    if (!$checkSTATUS) die ('bindParam() ID failed!<br>');

    $stmt1->execute();

Sorry for just learning PDO, I am here to learn more regarding a problem I have encountered regarding INSERT into in this specific query. I can't find where did the code went wrong. Since my other query with bindparams INSERT into is working. Did I missed something out? All variables are showing the correct information. I don't get it why this doesn't work.

SS of Database


Solution

  • Try this code instead:

    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    $stmt1 = $conn->prepare($sqlPickInsert);
    
    $params = [
      $nullvalues,
      $newPickupCode,
      $finalexistCustID,
      $login_Date,
      $finalpick_date,
      $newExpenditures,
      $paymentMode,
      $finalpick_notes,
      $login_Branch,
      $nullvalues
    ];
    
    $stmt1->execute($params);
    

    Passing an array of values to execute() is easier than all those bindParam() calls. I don't know where so many PHP developers get the idea that bindParam() is necessary.

    Enabling ERRMODE_EXCEPTION is easier than writing checks after every call to a PDO function. this ensures that you will get an error automatically (an exception actually) if anything goes wrong during prepare or execute.

    As @Barmar commented, you will probably discover that the error on execute() is that you seem to be passing a NULL to order_status, but that column doesn't accept NULLs.

    At least we guess that $nullvalues has the value NULL, but you haven't said so.