0

im very new to PDO, can you please spot whats wrong with my code? im trying to insert a record into my mysql with an ARRAY.

this is my array

$myarray = array("id"    =>'', 
                 "title" =>$title,
                 "body"  =>$body);

this is my function

function insertdata($myarray) {
$localhost = "localhost";
$user = "user";
$pass = "pass";
$dba = "sample";

$db = new PDO('mysql:host=$localhost;dbname=$dba', $user, $pass);

$sql=$db->prepare("INSERT INTO `posts`(`id`, `title`, `body`) VALUES (:id,:title,:body)");

foreach($myarray as $row=>$value) {
 $sql->bindValue(":".$row,$value);
 }
 $sql->execute();
}
bobbyjones
  • 2,029
  • 9
  • 28
  • 47
  • What error do you get? – Marcel Korpel Oct 20 '13 at 15:58
  • i just dont see any records being stored in my mysql. how can i debug properly? – bobbyjones Oct 20 '13 at 16:00
  • You should be getting an error message. If you aren't, try this: `$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );` Might have something to do with `id` being the empty string? If you want to let the db set it, just leave it out entirely. – Mark Reed Oct 20 '13 at 16:01

0 Answers0