0

*I'm trying to put a local array values $productname into a sqlite3 table , the array looks like this:

[0] usb 16gb
[1] monitor 16"
[2] dual batteries
[3] multi blender pro

and so on*

// Prepare INSERT statement into sqlite3
  $insert = "INSERT INTO products (name) 
            VALUES (:name)";

  $stmt = $db->prepare($insert);

  // Bind parameters to statement variables
  $stmt->bindParam(':name', $title);

  // Loop all product title and execute
   foreach ($productname as $i)
  {
        // Set values to bound variables
        $title = $i['name'];
        // Execute statement
         $stmt->execute();
  }

  $result= $db->query('SELECT * FROM products');
    foreach($result as $row) 
     {
        echo "Id: " . $row['id'] . "\n";
        echo "Title: " . $row['name'] . "\n";
     }

Whilst this does work, it prints out

Id: 1
Title: U
Id: 2 
Title: M
Id:3 
Title: D
Id:4 
Title: M 

Where am I going wrong? It doesn't print out the entire string (product name)...? Also it iterates itself about 80 times, when really there are only 4 or so titles. Basically I want a php array to be put into an sqlite3 table column (productTitle)

user2436729
  • 37
  • 2
  • 6

1 Answers1

0

Just to be concise, the title variable is used before it is declared. It should be used inside of the loop, like so: $stmt->bindParam(':name', $i['name']);