0

I have an array in the following format

Array
(
[0] => Array
    (
        [name] => Product 1
        [weight] => 0.3000
        [Price] => 31.4400
    )

[1] => Array
    (
        [name] => Product 2
        [weight] => 0.2000
        [Price] => 32.4400
    )

)

My pdo sql query is as follows:

$sql = "INSERT INTO products(name,weight,price) VALUES (?,?,?)"; 
$stmt = $conn->prepare($sql); 
foreach ($new_items as $v) {    
    $stmt->execute(array_values($v)); 
}

Receive error:

PHP Notice: Array to string conversion on $stmt->execute(array_values($v));

Update:

Tried this code too provided by @user1978142

// insert to database
foreach($new_items as $key => $value) {
    $stmt = $conn->prepare("INSERT INTO products (name, weight, price) VALUES (:name, :weight, :price)");
    $stmt->bindParam(':name', $value['name']);
    $stmt->bindParam(':weight', $value['weight']);
    $stmt->bindParam(':price', $value['Price']);
    $stmt->execute();
}

Error: Invalid parameter number: number of bound variables does not match number of tokens.

What is wrong with both the above code ?? I am a newbie.

4
  • @ kevinabelita It is Not working Commented Jun 18, 2014 at 12:32
  • I really do not like it when a computer is obviously lying. You code looks fine. It obviously prepares the query ok. It tries to bind, what are obviously sensible things, and gets confused. Things to check: First, there are implied parameter types: pdo.constants.php. Check that your table definition has simple data column definitions of integer or string. Next, var_dump($key, $value) and check that the array names and values are what you expect. Update your answer with any useful information. Commented Jun 18, 2014 at 13:29
  • @RyanVincent computer never lies. Commented Jun 18, 2014 at 13:54
  • @YourCommonSense, i agree completely! I should have made it clearer that it is getting confused rather than telling lies. I treat it as the point to start the process of checking all my assumptions about the situation as i am obviously the one who does not understand the situation. Was meant to be 'tongue in cheek' rather than a serious point. Commented Jun 18, 2014 at 13:57

3 Answers 3

0

after $stmt = $conn->prepare($sql); add this code:

 $stmt -> bind_param("ssd", $name, $weight,$price);
Sign up to request clarification or add additional context in comments.

Comments

0

For the present data structure your code is all right.

The error indicates that your array has some different structure, at least that array has one more nested level

check your input data.

Comments

-1

after prepare stament, first use bind_param, then proceed further.

1 Comment

foreach($new_items as $key => $value) { $stmt = $dbh->prepare("INSERT INTO products (name, weight, price) VALUES (:name, :weight, :price)"); $stmt->bindParam(':name', $value['name']); $stmt->bindParam(':weight', $value['weight']); $stmt->bindParam(':price', $value['Price']); $stmt->execute(); } .....Not Working ???

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.