I am having a hard time binding my SQL query and I only have a few braincells left.
Basically, this code works but prone to SQL injection:
return DB::connection('sqlsrv_rfo_user')
->table('dbo.tbl_rfaccount')
->insert([
'Email' => $email,
'id' => DB::raw("CONVERT(binary, '$username')"),
'password' => DB::raw("CONVERT(binary, '$password')"),
'birthdate' => $birthday,
'accounttype' => 0,
'BCodeTU' => 1
]);
I am trying to figure out how I can bind these lines of code:
'id' => DB::raw("CONVERT(binary, '$username')"),
'password' => DB::raw("CONVERT(binary, '$password')"),
I did attempt this:
'id' => DB::raw("CONVERT(binary, ?)", [$username]),
'password' => DB::raw("CONVERT(binary, ?)", [$password]),
and got this error:
SQLSTATE[07002]: [Microsoft][ODBC Driver 13 for SQL Server]COUNT field incorrect or syntax error (SQL: insert into [dbo].[tbl_rfaccount] ([Email], [id], [password], [birthdate], [accounttype], [BCodeTU]) values (user@example.com, CONVERT(binary, 2011-11-11 00:00:00), CONVERT(binary, 0), 1, ?, ?))
and this:
'id' => DB::raw("CONVERT(binary, :username)", ['username' => $username]),
'password' => DB::raw("CONVERT(binary, :password)", ['password' => $password]),
and got this error:
SQLSTATE[IMSSP]: An error occurred substituting the named parameters. (SQL: insert into [dbo].[tbl_rfaccount] ([Email], [id], [password], [birthdate], [accounttype], [BCodeTU]) values (user@example.com, CONVERT(binary, :username), CONVERT(binary, :password), 2011-11-11 00:00:00, 0, 1))
And if I try the full raw:
return DB::connection('sqlsrv_rfo_user')
->insert("
INSERT INTO [dbo].[tbl_rfaccount]
([id]
,[password]
,[accounttype]
,[birthdate]
,[BCodeTU]
,[Email])
VALUES
((CONVERT(binary, ?)), (CONVERT(binary, ?)), ?, ?, ?, ?)
", [$username, $password, 0, $birthday, 1, $email]);
I get this error:
SQLSTATE[22001]: [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]String or binary data would be truncated. (SQL: INSERT INTO [dbo].[tbl_rfaccount] ([id] ,[password] ,[accounttype] ,[birthdate] ,[BCodeTU] ,[Email]) VALUES ((CONVERT(binary, user01)), (CONVERT(binary, password01)), 0, 2011-11-11 00:00:00, 1, user@example.com)
I've been using Eloquent since the time I started learning Laravel but I have a project that forces me to do these way of coding, so I have no choice.