0

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
  • whats the error your having Commented Aug 7, 2013 at 2:46

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Nice catch on the page_id, that i just added to see if that would fix the issue but it did not and i removed it, i just forgot to remove from the column's. As you can see if my other codes, i tried many different ways, it just does not insert. I have a users class, code is almost identical but users class Insert works but Pages class insert would not work regardless of what i do.
Insert print_r($query->errorInfo()); right after $query->execute(), to see what MySQL complains about.
Thank you much, found what the issue was, i had the page_url field as a unique field while the data i was entering had no data in page_url causing duplicate entry error. Array ( [0] => 23000 [1] => 1062 [2] => Duplicate entry 'this is a unique field' for key 'page_url' ) 0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.