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 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)