How to update an array using PHP

  arrays, php

I have an app that is written using procedural PHP. I’ve created an insert page where I take a buck of addresses and pass them as an array and insert them in the database. There I have the id of the row and then an orderId, the address type, and the address. Now I want to be able to update a specific one. Until now I’ve come up with the following:

    // update new supplier order
function updateSupplierOrder($conn, $orderDate, $datePickup, $dateDelivery, $timePickup, $timeDelivery, $car, $carType, $goodsDescription, $paletChange, $paletNo, $supplier, $orderObservation, $paymentDate, $value, $addressPickup, $addressDelivery, $userid, $orderID) {

    $sql1 = "UPDATE suppliersOrders SET supplierId = ?, date = ?, datePickup = ?, timePickup = ?, goodsDescription = ?, dateDelivery = ?, timeDelivery = ?, carType = ?, carNo = ?, paletChange = ?, paletNo = ?, value = ?, invoice = ?, observations = ?, operator = ? WHERE id = ?;";
    $stmt1 = mysqli_stmt_init($conn);
    if (!mysqli_stmt_prepare($stmt1, $sql1)) {
        header ("location: ../suppliersOrders?error=failedupdateorder");
        exit();
    }

    mysqli_stmt_bind_param($stmt1, "isssssssisisssii", $supplier, $orderDate, $datePickup, $timePickup, $goodsDescription, $dateDelivery, $timeDelivery, $carType, $car, $paletChange, $paletNo, $value, $paymentDate, $orderObservation, $userid, $orderID);
    mysqli_stmt_execute($stmt1);
    mysqli_stmt_close($stmt1);

    for ($i=0; $i<count($addressPickup); $i++) {
        
        $address = $addressPickup[$i];
        $type = '1';
    
        $sql2 = "UPDATE suppliersOrdersAddress SET address = ?, operator = ? WHERE orderId = ? AND addressType = ?;";
        $stmt2 = mysqli_stmt_init($conn);   
        if (!mysqli_stmt_prepare($stmt2, $sql2)) {
            header ("location: ../suppliersOrders?error=failedupdateaddress");
            exit();
        }

        mysqli_stmt_bind_param($stmt2, "siii", $address, $userid, $orderID, $type);
        mysqli_stmt_execute($stmt2);
        mysqli_stmt_close($stmt2);

    }

    for ($i=0; $i<count($addressDelivery); $i++) {
        
        $address = $addressDelivery[$i];
        $type = '2';
    
        $sql2 = "UPDATE suppliersOrdersAddress SET address = ?, operator = ? WHERE orderId = ? AND addressType = ?;";
        $stmt2 = mysqli_stmt_init($conn);   
        if (!mysqli_stmt_prepare($stmt2, $sql2)) {
            header ("location: ../suppliersOrders?error=failedupdateaddress");
            exit();
        }

        mysqli_stmt_bind_param($stmt2, "siii", $address, $userid, $orderID, $type);
        mysqli_stmt_execute($stmt2);
        mysqli_stmt_close($stmt2);

    }
        
    header("location: ../suppliersOrders-edit.php?id=$orderID");
}

But this will update all the addresses of an order and a type. How can I update based on the id from the table, this will make sure that the right address is updated.

Help would be appreciated.

Source: Ask PHP

LEAVE A COMMENT