insert data into MYSQL using PHP array

前端 未结 3 1146
遇见更好的自我
遇见更好的自我 2021-01-26 08:07

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         


        
3条回答
  •  面向向阳花
    2021-01-26 08:45

    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

提交回复
热议问题