简体   繁体   中英

Insert Into Database From Array Using 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.

Try to pass Connection reference and loop as:

foreach($new_array as $each){
    mssql_query($conn, "INSERT INTO A(hotel_id, order_type, description) 
    VALUES('{$each['hotel']}', '{$each['order_type']}', '{$each['description']}')");
}

Does this help?

foreach($new_array as $item) {
    mssql_query("INSERT INTO A (hotel_id, order_type, description) 
    VALUES('". $item['hotel'] ."', '". $item['order_type'] ."', '". $item['description']. "')");
}

foreach is very similar to for loop but it simply iterates over all array elements. You don't need to care about the array size.

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']}')");
}
foreach ($new_array as $val){
    mysql_query("INSERT INTO A(order_type, description) VALUES($val['order_type'], $val['description'])");
}

This code doesn't insert hotel_id because it is primary key of the table A.

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

This should work I guess, haven't tested it though.

Try this

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

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