开发者

Simple INSERT query creates 2 records instead of one

You've got a simple table, a simple INSERT query and a quite weird result. Instead of a single empty query, the second mysql_query call creates 2 empty records. Why?

mysql_query("
    CREATE TABLE `users` (
      `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
      `email` varchar(255) NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8");

mysql_query("INSERT INTO `users` SET `users`.`id` = NULL");

Note: Running the query in phpMyAdmin gives t开发者_StackOverflowhe expected result - creates a single record.

Edit:

Adding the following mysql_query call to the beginning of the snippet fixes it.

mysql_query("DROP TABLE `users`");

Edit:

Turned out the problem is related to mod_rewrite (related question).


As I understand you run this script twice.

The first time script does:

  1. Create table.
  2. Insert row

The secont time:

  1. Tries to create table, but you get an error because table already exists, and nothing happens
  2. Insert second row

Try to add this line to check errors - 'echo mysql_error()."\n";'. For example -

mysql_query("
    CREATE TABLE `users` (
      `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
      `email` varchar(255) NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8");

echo mysql_error()."\n";


Try to use INSERT INTO users (id) VALUES (NULL)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜