doufen1933 2013-10-31 07:46
浏览 124
已采纳

使用PHP从数组插入数据库

I have database A with field hotel,order_type and description. I want insert value to database A from array $new_array.

//array $new_array

Array
(
[0] => Array
    (
        [order_type] => 1
        [currency] => 26
        [net] => 
        [rate] => 
        [amount] => 
        [bank_surcharge] => 
        [ticket] => 
        [profit] => 
        [selling_price] => 
        [description] => a
        [hotel] => 1
        [vendor] => 11
    )

[1] => Array
    (
        [order_type] => 2
        [currency] => 27
        [net] => 
        [rate] => 
        [amount] => 
        [bank_surcharge] => 
        [ticket] => 
        [profit] => 
        [selling_price] => 
        [description] => b
        [hotel] => 1
        [vendor] => 11
    )
...
...
}

i tried this script but nothing happened :(

for($i = 0; $i < sizeof($new_array['order_type']); $i++){
    mssql_query("INSERT INTO A(hotel_id, order_type,    description) 
    VALUES('$new_array[$i]', '$new_array[$i]', '$new_array[$i]')");
}

How to do this with php? please anyone help me.

  • 写回答

6条回答 默认 最新

  • duanbi9202 2013-10-31 07:54
    关注

    1) your loop is invalid, should be sizeof($new_array)

    2) your array data access for SQL statement is invalid

    for($i = 0; $i < sizeof($new_array); $i++){
        mssql_query("INSERT INTO A(hotel_id, order_type, description) VALUES({$new_array[$i]['hotel']}, '{$new_array[$i]['type']}', '{$new_array[$i]['description']}')");
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应