我正在使用PDO连接到运行在azure中的Microsoft Server,并且我一直收到错误:
这是我写的函数(insertUser):
private function generateSalt()
{
return dechex(mt_rand(0, 4453456454)) . dechex(mt_rand(0, 543545435));
}
private function hashPassword($originalPassword, $salt)
{
for($round = 0; $round < 65536; $round++)
{
$password = hash('sha256', $password . $salt);
}
return $password;
}
private function insertUser()
{
$salt = $this->generateSalt();
$password = $this->hashPassword($_POST['password'], $salt);
$query = "INSERT INTO dbo.[User]
( Title ,
FirstName ,
MiddleName ,
LastName ,
Gender ,
DOB ,
Email ,
Phone ,
Mobile ,
HomeAddress ,
HomePostCode ,
HomeSuburb ,
HomeState ,
HomeCountry ,
BrowserDetails ,
IsActive ,
Password ,
Salt ,
LastLogin ,
CompanyID ,
Created ,
CreatedBy ,
LastModified ,
LastModifiedBy ,
Username ,
UserRole
)
VALUES ( @title , -- Title - varchar(50)
@firstname , -- FirstName - varchar(100)
@middlename , -- MiddleName - varchar(100)
@lastname , -- LastName - varchar(100)
@gender , -- Gender - varchar(20)
@dob , -- DOB - datetime
@email , -- Email - varchar(200)
@phone , -- Phone - varchar(50)
@mobile , -- Mobile - varchar(50)
@homeaddress , -- HomeAddress - varchar(100)
@homepostcode , -- HomePostCode - varchar(10)
@homesuburb , -- HomeSuburb - varchar(50)
@homestate , -- HomeState - varchar(20)
@homecountry , -- HomeCountry - varchar(200)
@browserdetails , -- BrowserDetails - varchar(500)
1 , -- IsActive - bit
@password , -- Password - varchar(500)
@salt , -- Salt - varchar(50)
GETDATE() , -- LastLogin - datetime
@companyid , -- CompanyID - int
GETDATE() , -- Created - datetime
user , -- CreatedBy - varchar(50)
NULL , -- LastModified - datetime
NULL , -- LastModifiedBy - varchar(50)
@username , -- Username - varchar(50)
'user' -- UserRole - varchar(30)
)";
$query_params = array( ':title' => $_POST['title'] ,
':firstname' => $_POST['firstname'] ,
':middlename' => $_POST['middlename'] ,
':lastname' => $_POST['lastname'] ,
':gender' => $_POST['gender'] ,
':dob' => $_POST['dob'] ,
':email' => $_POST['email'] ,
':phone' => $_POST['phone'] ,
':mobile' => $_POST['mobile'] ,
':homeaddress' => $_POST['homeaddress'] ,
':homepostcode' => $_POST['homepostcode'] ,
':homesuburb' => $_POST['homesuburb'] ,
':homestate' => $_POST['homestate'] ,
':homecountry' => $_POST['homecountry'] ,
':browserdetails' => $_POST['browserdetails'] ,
':password' => $password ,
':salt' => $salt ,
':companyid' => $_POST['companyid'] ,
':username' => $_POST['username'] );
try
{
$stmt = $this->db->prepare($query);
$result = $stmt->execute($query_params);
}
catch(PDOException $ex)
{
die("Failed to run query: " . $ex->getMessage());
}
// display json encode on the page for AJAX to read
echo json_encode(array('error' , $result));
}
我收到的错误是:
无法运行查询:SQLSTATE [42000]:[Microsoft] [SQL Server Native 客户端11.0] [SQL Server]')'附近的语法不正确。
这让我很困惑的原因是我在SQL Management Studio中使用我发送的帖子数据手动重新创建了查询,一切正常。我甚至尝试过创建一个临时表,并插入那里,但我仍然没有成功。救命啊!
对于上下文,这是我尝试使用的帖子数据:
类别:1
这就是桌面设计:
USE [junglegymSQL] GO
/****** Object: Table [dbo].[User] Script Date: 19/12/2013 8:28:32 PM ******/ SET ANSI_NULLS ON GO
SET QUOTED_IDENTIFIER ON GO
SET ANSI_PADDING ON GO
CREATE TABLE [dbo].[User](
[UserId] [int] IDENTITY(1,1) NOT NULL,
[Title] [varchar](50) NULL,
[FirstName] [varchar](100) NOT NULL,
[MiddleName] [varchar](100) NULL,
[LastName] [varchar](100) NOT NULL,
[Gender] [varchar](20) NULL,
[DOB] [datetime] NULL,
[Email] [varchar](200) NOT NULL,
[Phone] [varchar](50) NULL,
[Mobile] [varchar](50) NULL,
[HomeAddress] [varchar](100) NULL,
[HomePostCode] [varchar](10) NULL,
[HomeSuburb] [varchar](50) NULL,
[HomeState] [varchar](20) NULL,
[HomeCountry] [varchar](200) NULL,
[BrowserDetails] [varchar](500) NULL,
[IsActive] [bit] NULL,
[Password] [varchar](500) NOT NULL,
[Salt] [varchar](50) NOT NULL,
[LastLogin] [datetime] NULL,
[CompanyID] [int] NULL,
[Created] [datetime] NULL,
[CreatedBy] [varchar](50) NULL,
[LastModified] [datetime] NULL,
[LastModifiedBy] [varchar](50) NULL,
[Username] [varchar](50) NOT NULL,
[UserRole] [varchar](30) NULL,
CONSTRAINT [PK_users] PRIMARY KEY CLUSTERED (
[UserId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) )
GO
SET ANSI_PADDING OFF GO
ALTER TABLE [dbo].[User] ADD DEFAULT ('User') FOR [UserRole] GO
ALTER TABLE [dbo].[User] WITH CHECK ADD CONSTRAINT [FK_User_Company] FOREIGN KEY([CompanyID]) REFERENCES [dbo].[Company] ([CompanyID]) GO
ALTER TABLE [dbo].[User] CHECK CONSTRAINT [FK_User_Company] GO
答案 0 :(得分:0)
检查单引号make(')的值语法是否正常