Solved

PHP PDO Insert Error

Posted on 2012-03-29
3
493 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
[X]
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
3 Comments
 
LVL 34

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

696 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