PHP PDO Insert Error

Posted on 2012-03-29
Last Modified: 2012-04-03
I am trying to insert data into a mysql database using PDO. The code is:
$query = "INSERT INTO `Staff` (`email`, `fullname`, `mobile`, `telephone`, `work-phone`, `faddress`, `postcode`, `marital`, `children`, `dob`, `hobbies`, `health`, `gid`, `cv`, `wages`, `employment`) 
	(:email, :fullname, :mobile, :telephone, :work-phone, :faddress, :postcode, :marital, :children, :dob, :hobbies, :health, :gid, :cv, :wages, :employment)";
	$q = $dbh->prepare($query);
	$insert = array(':email'=>$contact,
		':fullname' => $fullname,
		':mobile' => $mofo,
		':telephone' => $telephone,
		':work-phone' => $workTelephone,
		':faddress' => $faddress,
		':postcode' => $pc,
		':marital' => $ms,
		':children' => $children,
		':dob' => $dob,
		':hobbies' => $hobbies,
		':health' => $health,
		':gid' => $gid,
		':cv' => $cv,
		':wages' => $wages,
		':employment' => $empl);

Open in new window

however, I am getting the following error:
SQLSTATE[HY093]: Invalid parameter number: parameter was not defined on line 67
SQLSTATE[HY093]: Invalid parameter number on line 67
I'm new to PDO so could someone please tell me what the problem is here.
Question by:kirkheaton25
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 34

Expert Comment

ID: 37782518
greetings kirkheaton25, , the way to format your insert SQL, seems incorrect to me, as you place unneeded single quotes around your table name and columns.
I have used this syntax, and it works for me -

$stmt = $dbo->prepare('INSERT INTO authors (author,csvId) VALUES (:aut,:csvId)');
$params = array(':aut'=>$auth,':csvId'=>$cid);

Accepted Solution

kirkheaton25 earned 0 total points
ID: 37783994
The quotes are mysql identifiers which are fine. The problem was the hyphen in :work-phone. I changed this to :work_phone and it's now working.

Author Closing Comment

ID: 37800045
No other answers provided resolution or pointed me towards the resolution.

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
What is key combo for a b with a forward slash through it? 12 62
phpmyadmin memory error 55 91
Link Stopped Working 7 30
Which is best Image Resizing Web service 11 39
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question