Reputation: 311
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.
Upvotes: 0
Views: 2634
Reputation: 2437
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']}')");
}
Upvotes: 3
Reputation: 365
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.
Upvotes: 0
Reputation: 1950
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']}')");
}
Upvotes: 2
Reputation: 3218
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.
Upvotes: 1
Reputation: 10148
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.
Upvotes: 2
Reputation: 534
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']."')");
}
Upvotes: -1