Ryan
Ryan

Reputation: 10049

PHP: Prepared statements (newbie) getting this error

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

function 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?

Upvotes: 1

Views: 158

Answers (3)

KingCrunch
KingCrunch

Reputation: 131831

"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

Upvotes: 3

ben
ben

Reputation: 1936

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)'

Upvotes: 3

martynthewolf
martynthewolf

Reputation: 1708

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.

Upvotes: 4

Related Questions