Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does MySQL Insert allow not specifying all fields?

I have two mysql insert statements. The one with all the fields specified in insert statement works fine and insert record to testTable.(Even when http_referer is empty the insert statement insert records to table with referer field empty)

First Insert statement with all fields specified:

mysql_query("INSERT INTO testTable VALUES('$ID','".$_SERVER['REMOTE_ADDR']."',NOW(),'Page1','".$_SERVER['HTTP_REFERER']."')");

The problem is with second insert statement that doesn't insert any record to testTable! Could you guys tell me why my second insert statement doesn't insert any record to testTable?

Second insert Statment:

mysql_query("INSERT INTO testTable VALUES('$ID','".$_SERVER['REMOTE_ADDR']."',NOW(),'Page1')");

Create Table:

CREATE TABLE IF NOT EXISTS `testTable` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `ip` varchar(32) DEFAULT NULL,
  `date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `Title` varchar(32) NOT NULL,
  `Ref` varchar(250) NULL default '',
  PRIMARY KEY (`ID`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1784 ;
like image 212
user1788736 Avatar asked Oct 16 '25 18:10

user1788736


1 Answers

Yes, by using a column list.

$sql = "INSERT INTO table (`ip`, `date`, `Title`) VALUES ('".$_SERVER['REMOTE_ADDR']."', NOW(), 'Page 1')";

Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which.

like image 198
Kermit Avatar answered Oct 18 '25 11:10

Kermit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!