开发者

PHP: Prepared statements (newbie) getting this error

开发者 https://www.devze.com 2023-03-13 21:37 出处:网络
I am a newbie to prepared statements and trying to get something simple to work. This is my DB table: `unblocker_users` (

I am a newbie to prepared statements and trying to get something simple to work.

This is my DB table:

`unblocker_users` (
  `uno` bigint(20) NOT NULL AUTO_INCREMENT,
  `user_email` varchar(210) DEFAULT NULL,
  `pw_hash` varchar(30) DEFAULT NULL,
  `email_confirmed` tinyint(4) DEFAULT NULL,
  `total_requests` bigint(20) DEFAULT NULL,
  `today_date` date DEFAULT NULL,
  `accessed_today` tinyint(4) DEFAULT NULL,)

and this is my function to insert some test data

fu开发者_如何学Pythonnction add_new_user($e_mail1)
    {
    require_once "db.php";
// ####### Below line is giving an error ########
$stmt = $mysqli->prepare("INSERT INTO unblocker_users VALUES ("",?, ?,0,0,?,0)");
$stmt->bind_param('sss', $e_mail1, $this->genRandomString(1),$this->today_date()); 

$stmt->execute();

$stmt->close(); 
$done = $stmt->affected_rows;

return $done;
    }

As you can see above, i have marked the line that is giving me an error. The error is" Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in..."

Where did I go wrong?


This line might be causing a problem

$stmt = $mysqli->prepare("INSERT INTO unblocker_users VALUES ("",?, ?,0,0,?,0)");

See you close and open the string again in the values, change "" to '' Thats all I could see from a quick glance.


You have " with a double quoted string. PHP thinks

"INSERT INTO unblocker_users VALUES ("",?, ?,0,0,?,0)"

is 2 strings:

"INSERT INTO unblocker_users VALUES ("

",?, ?,0,0,?,0)"

but doesn't what you want to do with them.

change the outside quotes to single quotes and it should work:

'INSERT INTO unblocker_users VALUES ("",?, ?,0,0,?,0)'


"INSERT INTO unblocker_users VALUES ("",?, ?,0,0,?,0)"

In php if you want to use double quotes within double quotes, you must escape the inner ones

"INSERT INTO unblocker_users VALUES (\"\",?, ?,0,0,?,0)"

Or use single quotes as outer ...

'INSERT INTO unblocker_users VALUES ("",?, ?,0,0,?,0)'

... or inner quotes

"INSERT INTO unblocker_users VALUES ('',?, ?,0,0,?,0)"

As far as I know mySQL use single quotes anyway

0

精彩评论

暂无评论...
验证码 换一张
取 消