简体   繁体   中英

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. 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.

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

Some remarks:

Like several others (eg @wintercounter, @user1909426 ) have pointed out you are using restricted words in 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. This column is probably an integer column with auto_increment. See @wintercounter answer.

Fortunately date is not a restricted name. BTW you could use use a date value instead of you varchar value now.

With regard to the comments from @tadman using mysql instead of mysqli or PDO is not recommended. The mysql library is depreciated from version PHP 5.5 onwards, see the php manual. You will also need to include error handling.

For completeness sake, this is the php code when using MySQLi:

  $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:

  $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:

try setting error_reporting to 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.

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.

EDIT: I've changed NULL to ''. 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.

尝试一下:

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

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