donglu1881 2012-08-27 11:40
浏览 61
已采纳

如何将数组值插入数据库表的列?

I have an array with three values red, green and yellow. Now i should

insert red into column3 row1
insert green into column3 row2
insert yellow into column3 row3

How can i do that i tried writing the code

foreach ($output as $value)
{
    echo ($value.'<br>');
    $tstring = implode(',' , $output);
    $insert_col= "UPDATE INTO `5` (B) VALUES ('$tstring')";
    $insert_result = mysql_query($insert_col);
    if ($insert_result)
    { 
        echo ("RECORDED!")|
        exit();
    }
}

but it does not work. it is filling extra rows to the existing table with a value R.

Please help!

  • 写回答

2条回答 默认 最新

  • douhuan6157 2012-08-27 11:52
    关注

    if I remember correctly, an UPDATE statement should have a WHERE clause.

    Like:

     UPDATE table SET column_name='value' WHERE condition;
    

    You can find examples at w3schools.

    As for inserting the right value:

    foreach($output as $value){
         $tstring = $value;
         $insert_col = "UPDATE `5` SET B='" . $tstring . "' WHERE insert a condition here";
         $insert_result = mysql_query($insert_col);
    
        if ($insert_result) {
             echo ("RECORDED!") |
        }
    }
    

    You don't want to insert all your array in one row, which means that the implode is useless.

    And I repeat, you need a WHERE clause here. Without it, you'll update all your rows with the same values.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求
  • ¥20 antv g6 折线边如何变为钝角
  • ¥30 如何在Matlab或Python中 设置饼图的高度
  • ¥15 nginx中的CORS策略应该如何配置
  • ¥30 信号与系统实验:采样定理分析
  • ¥100 我想找人帮我写Python 的股票分析代码,有意请加mathtao
  • ¥20 Vite 打包的 Vue3 组件库,图标无法显示
  • ¥15 php 同步电商平台多个店铺增量订单和订单状态
  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别