Have been trying to debug this for hours and no outcome. Please help. Environment: PHP5.3 talking to MSSQL2005/8 with Microsoft MSSQL Driver for PHP SQL Query:
INSERT INTO [dbo].[Enquiry] ([FullName], [FirstName], [Surname], [ContactPhone],
[WorkPhone], [ContactMobile], [EmailAddress], [Callwhen], [LoanType], [EnquiryDate],
[Suburb], [State], [PostCode], [HiddenField], [CFIssue01], [CFIssue02], [CFIssue03],
[CFIssue04], [ProductID]) VALUES ('asdf asdf','asdf','asdf','03 12312312','02 12312312',
'','','10:12 AM 02/08/11','CF','2011-08-02 10:12:45',
'asdf','NSW','1231','245678','asdfasdf1222','','','','CF')
If I run this query with sqlsrv_query()
and it works well.
To prevent SQL Injection, I prefer to use prepared statement sqlsrv_prepare()
and sqlsrv_execute()
.
To do prepared statement, I have:
INSERT INTO [dbo].[Enquiry] ([FullName], [FirstName], [Surname], [ContactPhone],
[WorkPhone], [ContactMobile], [EmailAddress], [Callwhen], [LoanType], [EnquiryDate],
[Suburb], [State], [PostCode], [HiddenField], [CFIssue01], [CFIssue02], [CFIssue03],
[CFIssue04], [ProductID]) VALUES "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,)"
Then I have params:
$params = array( &$mm_fullname, &$mm_firstname, &$mm_surname开发者_运维百科, &$mm_contactphone,
&$mm_workphone, &$mm_contactmobile, &$mm_emailaddress, &$mm_callwhen, &$mm_loantype,
&$mm_enquirydate, &$mm_suburb, &$mm_state, &$mm_postcode, &$mm_hiddenfield,
&$mm_cfissue01, &$mm_cfissue02, &$mm_cfissue03, &$mm_cfissue04, &$mm_productid );
Then prepare the statement:
$stmt = sqlsrv_prepare( $conn, $tsql, $params))
Then run the statement:
sqlsrv_execute( $stmt)
......... ban! error here:
[0] => HY104
[SQLSTATE] => HY104
[1] => 0
[code] => 0
[2] => [Microsoft][SQL Server Native Client 10.0]Invalid precision value
[message] => [Microsoft][SQL Server Native Client 10.0]Invalid precision value
Not sure what am I doing wrong..
Update #1: convert all params to string - same error:
/* Assign parameter values. */
$mm_fullname = strval($firstname . " " . $lastname);
$mm_firstname = strval($firstname);
$mm_surname = strval($lastname);
$mm_contactphone = strval($homenumber);
$mm_workphone = strval($worknumber);
$mm_contactmobile = strval($mobilenumber);
$mm_emailaddress = strval($email);
$mm_callwhen = strval(date('h:i A d/m/y'));
$mm_loantype = strval("CF");
$mm_enquirydate = strval(date('Y-m-d H:i:s'));
$mm_suburb = strval($suburb);
$mm_state = strval($state);
$mm_postcode = strval($postcode);
$mm_hiddenfield = strval("245678");
$mm_cfissue01 = strval($creditissue1);
$mm_cfissue02 = strval($creditissue2);
$mm_cfissue03 = strval($creditissue3);
$mm_cfissue04 = strval($creditissue4);
$mm_productid = strval("CF");
$params = array( &$mm_fullname,
&$mm_firstname,
&$mm_surname,
&$mm_contactphone,
&$mm_workphone,
&$mm_contactmobile,
&$mm_emailaddress,
&$mm_callwhen,
&$mm_loantype,
&$mm_enquirydate,
&$mm_suburb,
&$mm_state,
&$mm_postcode,
&$mm_hiddenfield,
&$mm_cfissue01,
&$mm_cfissue02,
&$mm_cfissue03,
&$mm_cfissue04,
&$mm_productid );
OK. Thanks to Wrikken. Your comments worked!
From this page, I found how to define SQLSRV_SQLTYPE_* for each parameter.
Now, I updated my code to have:
$params = array(
array(&$mm_fullname, null, null, SQLSRV_SQLTYPE_VARCHAR(200)),
array(&$mm_firstname, null, null, SQLSRV_SQLTYPE_VARCHAR(100)),
array(&$mm_surname, null, null, SQLSRV_SQLTYPE_VARCHAR(100)),
array(&$mm_contactphone, null, null, SQLSRV_SQLTYPE_VARCHAR(50)),
array(&$mm_workphone, null, null, SQLSRV_SQLTYPE_VARCHAR(50)),
array(&$mm_contactmobile, null, null, SQLSRV_SQLTYPE_VARCHAR(50)),
array(&$mm_emailaddress, null, null, SQLSRV_SQLTYPE_VARCHAR(150)),
array(&$mm_callwhen, null, null, SQLSRV_SQLTYPE_VARCHAR(50)),
array(&$mm_loantype, null, null, SQLSRV_SQLTYPE_VARCHAR(50)),
array(&$mm_enquirydate, null, null, SQLSRV_SQLTYPE_DATETIME),
array(&$mm_suburb, null, null, SQLSRV_SQLTYPE_VARCHAR(100)),
array(&$mm_state, null, null, SQLSRV_SQLTYPE_VARCHAR(50)),
array(&$mm_postcode, null, null, SQLSRV_SQLTYPE_VARCHAR(20)),
array(&$mm_hiddenfield, null, null, SQLSRV_SQLTYPE_VARCHAR(2000)),
array(&$mm_cfissue01, null, null, SQLSRV_SQLTYPE_VARCHAR(2000)),
array(&$mm_cfissue02, null, null, SQLSRV_SQLTYPE_VARCHAR(2000)),
array(&$mm_cfissue03, null, null, SQLSRV_SQLTYPE_VARCHAR(2000)),
array(&$mm_cfissue04, null, null, SQLSRV_SQLTYPE_VARCHAR(2000)),
array(&$mm_productid, null, null, SQLSRV_SQLTYPE_VARCHAR(50))
);
It works great without any error!
精彩评论