dongren1353 2014-07-24 10:45
浏览 164
已采纳

PHP / MySQL更新来自JSON数组的表行

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!

  • 写回答

2条回答 默认 最新

  • dpdhsq0783 2014-07-24 10:49
    关注

    After bind the params you have to execute your query and then only you get the result.

    $result = $statement->execute();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 无源定位系统的时差估计误差标准差
  • ¥15 请问这个代码哪里有问题啊
  • ¥20 python--version在命令端输入结果Python is not defined怎么办?还有pip不是exe格式是不是没安装成功?
  • ¥15 通过GaussianView进行结构微调消除虚频
  • ¥15 调用transformers库
  • ¥15 由于导出的数据名字中带有/,导致Matlab打不开,怎么办?
  • ¥15 新硬盘安装的程序总是崩溃,提示遇到错误
  • ¥15 openpcdet自制数据集评估bev精度和3d精度相同
  • ¥15 excel 上下按钮 显示行
  • ¥20 云卓h12pro 数传问题