0

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.

2
  • i think you don't have to pass sizeof($new_array['order_type'] you may try count($new_array) Commented Oct 31, 2013 at 7:48
  • thanks for all nice people out there who help me out.. you're all the best Commented Oct 31, 2013 at 8:09

6 Answers 6

3

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']}')");
}
Sign up to request clarification or add additional context in comments.

2 Comments

query using mssql but the connection created for mysql
Thanks @AllenChak, I missed db. It was MS-Sql. I though it is MySQL. kroseva please try again. changed for MS-Sql.
2

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']}')");
}

Comments

2

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.

Comments

1
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.

Comments

0
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.

1 Comment

invalid variable of $new_array['order_type'] for function sizeof()
-1

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']."')");
}

1 Comment

invalid variable of $new_array['order_type'] for function sizeof()

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.