Solved

PHP PDO Insert Error

Posted on 2012-03-29
3
470 Views
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`) 
	VALUES 
	(:email, :fullname, :mobile, :telephone, :work-phone, :faddress, :postcode, :marital, :children, :dob, :hobbies, :health, :gid, :cv, :wages, :employment)";
	
	$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
	$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);
	
	$q->execute($insert);

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.
Thanks.
0
Comment
Question by:kirkheaton25
  • 2
3 Comments
 
LVL 33

Expert Comment

by:Slick812
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);
$stmt->execute($params);
0
 
LVL 1

Accepted Solution

by:
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.
0
 
LVL 1

Author Closing Comment

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

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Creating and Managing Databases with phpMyAdmin in cPanel.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now