简体   繁体   中英

How to Insert in MySQLi in prepared statement

Hi! My table structure looks like this:

CREATE TABLE IF NOT EXISTS `search` (
  `key` varchar(255) NOT NULL,
  `id` int(15) NOT NULL auto_increment,
  UNIQUE KEY `key` (`key`),
  KEY `id` (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

And this is how I try to add data into my table :

$stmt = $mysqli->prepare("INSERT INTO search(key) VALUES (?)");
$stmt->bind_param('s',$keyword);
$stmt->execute();
$stmt->close();

This is what I got:

Call to a member function bind_param() on a non-object

But it works when I do this:

$stmt = $mysqli->prepare("INSERT INTO search VALUES (?,NULL)");
$stmt->bind_param('s',$keyword);
$stmt->execute();
$stmt->close();

Is there any other way besides putting NULL to the VALUES ?

is there any necessity that i should put NULL to auto increments?

No.
And there is no necessity in finding an answer by means of wild guess either.
You have to get the error message instead.

For which purpose always have this line right before connecting to mysqli:

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

speaking for the certain query, key is a mysql keyword and have to be quoted in backticks

INSERT INTO search(`key`) VALUES (?)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM