简体   繁体   English

PHP MySQL-我做错什么了吗?

[英]PHP MySQL - Am I doing anything wrong?

For two hours now, I'm trying to insert a value into a table. 现在两个小时,我正在尝试在表中插入一个值。 I don't get any error and I can't find out the problem! 我没有任何错误,我也找不到问题!

The value that I'm trying to insert: 我要插入的值:

$query = "INSERT INTO banlist (banid, active, ip, by, date, reason) VALUES (NULL, 1, '10.25.47.88', 'AUTOBAN', '12-12-45', 'test')";
mysql_query($query);

An example value that works perfectly: 完美工作的示例值:

$query = "INSERT INTO accounts (username, password, email, regdate) VALUES ('test', 'test', 'test@test.test', 't-t-t t:t:t')";
mysql_query($query);

I can't find the problem! 我找不到问题! Am I missing anything? 我有什么想念的吗? Both tables exist. 两个表都存在。

The issue is that the name you've chose for a field "by" is a reserved word. 问题是您为“ by”字段选择的名称是保留字。 You'll have to update it to a word that's not on this list . 您必须将其更新为不在此列表中的单词。

Also, in future you can easily see what's wrong by checking if mysql_query() returned false, and then calling mysql_error() for an error message. 另外,将来您可以通过检查mysql_query()返回false,然后调用mysql_error()以获得错误消息来轻松查看问题所在。

Try this: 尝试这个:

    CREATE TABLE ban (
    banid int auto_increment primary key, 
    active int,
    ip varchar (20), 
    `by` varchar (20),
    `date` varchar(8),
    reason varchar(20)                                             
        );                                         

    INSERT INTO ban (active, ip, `by`, `date`, reason)
    VALUES 
    (1, '10.25.47.88', 'AUTOBAN', '12-12-45', 'test')
    ;

    SELECT * FROM ban;

http://www.sqlfiddle.com/#!2/1959f/1 http://www.sqlfiddle.com/#!2/1959f/1

Some remarks: 一些说明:

Like several others (eg @wintercounter, @user1909426 ) have pointed out you are using restricted words in MySQL. 就像其他几个人(例如@ wintercounter,@ user1909426)指出的那样,您在MySQL中使用受限词。 If you do use a restricted word then use `` (back ticks) or just use them on every column. 如果您确实使用了限制词,请使用``(倒勾)或仅在每列上使用它们。

I think that using a null in your first part of you insert gives a problem. 我认为在插入的第一部分使用null会带来问题。 This column is probably an integer column with auto_increment. 该列可能是带有auto_increment的整数列。 See @wintercounter answer. 请参阅@wintercounter答案。

Fortunately date is not a restricted name. 幸运的是date不是受限制的名称。 BTW you could use use a date value instead of you varchar value now. 顺便说一句,您现在可以使用日期值代替varchar值。

With regard to the comments from @tadman using mysql instead of mysqli or PDO is not recommended. 关于@tadman中的注释,建议不要使用mysql而不是mysqli或PDO。 The mysql library is depreciated from version PHP 5.5 onwards, see the php manual. mysql库从PHP 5.5版本开始不再使用,请参阅php手册。 You will also need to include error handling. 您还需要包括错误处理。

For completeness sake, this is the php code when using MySQLi: 为了完整起见,这是使用MySQLi时的php代码:

  $link = mysqli_connect($hostname, $username, $password, $database);
  if (!$link){ 
  echo('Unable to connect to database');
  }

  else{
  mysqli_query("INSERT INTO ban (active, ip, `by`, `date`, reason) VALUES (1,'10.25.47.88', 'AUTOBAN', '12-12-45', 'test'))", $link); 
  }

  mysqli_close($link);

For mysql version: 对于mysql版本:

  $hostname = "hostname";
  $username = "username";
  $username = "password";
  $database = "database";

  $link = mysql_connect($hostname, $username, $password);
  mysql_database ($database)
  if (!$link){ 
  echo('Unable to connect to database');
  }

  else{
  mysql_query("INSERT INTO ban (active, ip, `by`, `date`, reason) VALUES (1,'10.25.47.88', 'AUTOBAN', '12-12-45', 'test')"); 
  }

  mysql_close($link);

在每个变量中使用mysql错误语句,以了解您的错误发生在哪一行。

The query probably doesn't display an error because error_reporting is turned of in your php.ini: 该查询可能不会显示错误,因为在php.ini中已打开error_reporting:

try setting error_reporting to E_ALL. 尝试将error_reporting设置为E_ALL。

Also the query might not work because you are sending "NULL" as value for banid which is probably a either a primary key or a foreign key / index that doesn't allow a NULL value. 此外,查询可能不起作用,因为您要发送“ NULL”作为banid的值,这可能是不允许NULL值的主键或外键/索引。

Try this: 尝试这个:

INSERT INTO `banlist` (`banid`, `active`, `ip`, `by`, `date`, `reason`) VALUES ('', 1, '10.25.47.88', 'AUTOBAN', '12-12-45', 'test')

As stated, 'by' is reserved keyword, but you can help to MySQL in the parse so it'll know if it's a field name or a command. 如前所述,'by'是保留关键字,但是您可以在语法分析中帮助MySQL,以便它知道它是字段名还是命令。

EDIT: I've changed NULL to ''. 编辑:我已将NULL更改为“。 I'm not sure in this, never tried, but if it's an AI field, maybe you can't use NULL there, just use an empty content as a placeholder for ID field. 我不确定这一点,从未尝试过,但是如果它是一个AI字段,也许您不能在那里使用NULL,只需将空内容用作ID字段的占位符即可。

尝试一下:

INSERT INTO banlist VALUES (NULL, 1, '10.25.47.88', 'AUTOBAN', '12-12-45', 'test')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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