Solved

PHP PDO Insert Error

Posted on 2012-03-29
3
479 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

832 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