简体   繁体   中英

PHP/MySQL update table rows from JSON array

I'm learning PHP and Laravel has spoilt me. I have a project that isn't using Laravel and my basics are pretty lost.

I am grabbing a JSON array from a webpage and want to update each affected row. For the life of me, I can't understand why this isn't working (The JSON file is definitely being pick up):

$conn = new mysqli($env_db['hostname'], $env_db['username'], $env_db['password'], $env_db['database']);

// check connection
if (mysqli_connect_errno()) {
    exit('Connect failed: '. mysqli_connect_error());
}

// exp_matrix_data = matrix table
// col_id_24 = sku code
// col_id_26 = stock

foreach(json_decode($file, true) as $item)
{
    $stock = $item['Stock'];
    $sku = $item['SKU'];

    $query = "UPDATE exp_matrix_data SET col_id_26=? WHERE col_id_24=?";
    $statement = $conn->prepare($query);

    //bind parameters for markers, where (s = string, i = integer, d = double,  b = blob)
    $results =  $statement->bind_param('ss', $stock, $sku);
}

example json

[
    {
        Series: "01000",
        SKU: "01000-1116",
        Stock: "98",
        id: 0
    },
    {
        Series: "01000",
        SKU: "01000-1132",
        Stock: "0",
        id: 1
    },
    {
        Series: "01000",
        SKU: "01000-116",
        Stock: "1000",
        id: 2
    }
]

Any pointers as to why this isn't working properly? No rows are being updated.

Thanks for taking the time to help this newbie!

绑定参数后,您必须执行查询,然后只有结果。

$result = $statement->execute();

传递完查询后,您需要执行查询。.将完成查询所需的结果完成。

$result = $statement->execute();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM