douying4203 2013-08-07 02:35
浏览 84
已采纳

PDO INSERT INTO无效

I am having a heck of a time trying to figure out what is wrong with my query, and was not able to figure out. I have search the internet and S.O found few similar answers but none seems to work.

pLease see the attacked code and help me figure our what the problem is.

  public function add_page($page=array()){
global $db;

//define page properties
$page_name = $page['page_name'];
$page_title= $page['page_title'];
$page_meta_description = $page['page_meta_description'];
$page_meta_keywords = $page['page_meta_keywords'];
$page_meta_other = $page['page_meta_other'];
$page_meta_other_2= $page['page_meta_other_2'];
$page_url = $page['page_url'];
$page_summary = $page['page_summary'];
$page_content = $page['page_content'];
$page_nav_item = $page['page_nav_item']; 
$page_nav_name= $page['page_nav_name'];


    $q="INSERT INTO `simplecms`.`pages` (`page_id`, `page_name`, `page_title`, `page_meta_description`, `page_meta_keywords`, `page_meta_other`, `page_other_2`, `page_url`, `page_summary`, `page_content`, `page_nav_item`, `page_nav_name`) VALUES ( ?, ?, ?, ?, ?, ? , ? , ?, ?, ?, ?)";
    $query = $db->prepare($q);

    $query->bindValue(1,$page_name);
    $query->bindValue(2,$page_title);
    $query->bindValue(3,$page_meta_description);
    $query->bindValue(4,$page_meta_keywords );
    $query->bindValue(5,$page_meta_other);
    $query->bindValue(6,$page_meta_other_2);
    $query->bindValue(7,$page_url );
    $query->bindValue(8,$page_summary);
    $query->bindValue(9,$page_content );
    $query->bindValue(10,$page_nav_item);
    $query->bindValue(11,$page_nav_name);

    $query->execute();
    $r=$query->rowCount();

    echo $r;



    //VALUES (NULL, 'Page Name', 'Page Title', 'Meta  page description', 'google, ooogle ogle, userher', 'dymmy text qq', '', 'google.com', 'Small Summary', 'Ths is the big text', 'Yes', 'Google.com');


    /*
    $query= $db->prepare("INSERT INTO `pages`(`page_name`, `page_title`, `page_meta_description`, `page_meta_keywords`, `page_meta_other`, `page_other_2`, `page_url`, `page_summary`, `page_content`, `page_nav_item`, `page_nav_name`) VALUES (?,?,?,?,?,?,?,?,?,?,?)");
    $query->execute(array(
        $page_name,
        $page_title,
        $page_meta_description,
        $page_meta_keywords,
        $page_meta_other,
        $page_meta_other_2,
        $page_url,
        $page_summary,
        $page_content,
        $page_nav_item,
        $page_nav_name
        ));
    $query->execute();

    $row = $query->rowCount();
    echo $row;*/

    /*
    $query = "INSERT INTO `pages`(
                    `page_name`,
                    `page_title`,
                    `page_meta_description`,
                    `page_meta_keywords`,
                    `page_meta_other`,
                    `page_meta_other_2`,
                    `page_url`,
                    `page_summary`,
                    `page_content`,
                    `page_nav_item`,
                    `page_nav_name`
                    )  VALUES(
                    :name,
                    :title,
                    :desctription,
                    :keywords,
                    :meta1,
                    :meta2,
                    :url,
                    :summary,
                    :content,
                    :navitem,
                    :navname

                    )";
                                            //VALUE(?,?,?,?,?,?,?,?,?,?,?)";
    $q = $db->prepare($query);
    $q->execute(array(
                    ':name'                 =>$page_name,
                    ':title'                =>$page_title,
                    ':desctription'         =>$page_meta_description,
                    ':keywords'             =>$page_meta_keywords,
                    ':meta1'                =>$page_meta_other,
                    ':meta2'                =>$page_meta_other_2,
                    ':url'                  =>$page_url,
                    ':summary'              =>$page_summary,
                    ':content'              =>$page_content,
                    ':navitem'              =>$page_nav_item,
                    ':navname'              =>$page_nav_name
        ));
        */
    /*$q->bindValue(':name', $page_name, PDO::PARAM_STR);
    $q->bindValue(':title', $page_title, PDO::PARAM_STR);
    $q->bindValue(':desctription', $page_meta_description, PDO::PARAM_STR);
    $q->bindValue(':keywords', $page_meta_keywords, PDO::PARAM_STR);
    $q->bindValue(':meta1', $page_meta_other, PDO::PARAM_STR);
    $q->bindValue(':meta2', $page_meta_other_2, PDO::PARAM_STR);
    $q->bindValue(':url', $page_url, PDO::PARAM_STR);
    $q->bindValue(':summary', $page_summary, PDO::PARAM_STR);
    $q->bindValue(':content', $page_content, PDO::PARAM_STR);
    $q->bindValue(':navitem', $page_nav_item, PDO::PARAM_STR);
    $q->bindValue(':navname', $page_nav_name, PDO::PARAM_STR);
    $q->execute();*/

    /*$r = $q->rowCount();

    if($r<1){
        echo "Oops no article is added <pre>";
        print_r($page);
        echo "</pre>";
    }else{

        echo $r."Google";
    }
    */

}

I have tried pretty much any/ all methods i could think off. I have browsed the internet for hours and is not working,

In the same class, i am able to select and fetch all data from the database, but inserting into the database has became a nightmare.

Please help.

  • 写回答

1条回答 默认 最新

  • drfb52000 2013-08-07 02:47
    关注

    page_id is missing from the VALUES. You have 12 fields, but 11 placeholders and 11 bindValue.

    If page_id is AUTO_INCREMENT, don't include it in the field list.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度