hi im trying to insert data in mysql using array, can someone please look at my code, i cant seem to make it work.
this is my post.php
/* POST.PHP */
$p
Construct the array with a key and a value
$myarray = array("id"=>'',"title"=>$title,"body"=>$body,"pid"=>$rowId);
and Use PDO instead of mysql_* functions (depreciated).
$sql=$dbh->prepare("INSERT INTO `posts`(`id`, `title`, `body`, `post_id`) VALUES (:id,:title,:body,:pid)");
foreach($myarray as $row=>$value){
$sql->bindValue(":".$row,$value);
}
$sql->execute();
More About PDO : http://www.php.net/manual/en/book.pdo.php
in
$myarray = array('','$title','$body','$rowId');
code, you cannot use single quotations to read variables values in to string. you must use double quatations for that, because if you use single quotation, it not reading variable value, and php thinks its the value itself,
try
$myarray = array("","$title","$body","$rowId");
this may help you...
The thing is:
insertToDB
neither appears to be properly closed (maybe it is, at the end of the code section you provide) nor have a return
. If it is properly closed, it still lacks a return
(it may help you diagnose what is going on have conditional return for success and error cases.$query
but do not use it later. Unless you append to that string the result of converting your array into another string (a thing that I dont see either), I dont think you are going to INSERT
anything to your database.Maybe I'm missing something...