0

I am inserting values in a table but it's not working in sequence, see screenshot:

Table

Code:

$query = "insert into msgs values(NULL,'".$sender."','".$receiver."','".$message."','".$state."','".$time."')";
mysql_query($query);
calcinai
  • 2,567
  • 14
  • 25
Musawar
  • 93
  • 3
  • 10
  • dont user mysql it's deprecated since php 5.5.0 use mysqli_ functions instead – Blueblazer172 Nov 07 '16 at 15:25
  • I think this is beacuse some queries fail to insert, that why you auto increment is incresing so far... Try to use PDO instead of mysql_* ! – Weenesta - Mathieu Dormeval Nov 07 '16 at 15:26
  • In phpMyAdmin have you clicked over any column name? If so the data will be sorted by the column and displayed there alone. You can confirm this after clicking the *browse* link. It will show the query such as `SELECT * FROM {YOUR_TABLE_NAME} ORDER BY {SOME_COLUMN_COLUMN} {ASC/DESC}`. You can click on the `id` column to sort by it. – Sarvap Praharanayuthan Nov 07 '16 at 15:37
  • 1
    What about this tells you they aren't in sequence? Look at the timestamps... they are in sequence, you just haven't ordered them in your select. – Devon Bessemer Nov 07 '16 at 15:37
  • 1
    @RST You're supposed to supply null if you want to use the auto increment value – Devon Bessemer Nov 07 '16 at 15:38
  • Comment from @RST is unhelpful, and is just plain wrong. OP isn't supplying column list in INSERT statement, so values must be provided for all columns, and NULL constant is the preferred value to supply to get value automatically assigned AUTO_INCREMENT behavior. Devon is correct. Without an ORDER BY clause on the SELECT statement, MySQL is free to return rows in *any* sequence. (And, we expect the client program to present rows in the same order as fetched from the database, but this behavior really depends on the client program.) – spencer7593 Jan 31 '17 at 22:33

1 Answers1

1

First of all don't use mysql, use mysqli. Please check this: The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead

Using null when inserting autoincrement is fine (see Devon's first comentary).

When looking at your timestamp (msg_time) i can see that your table is short by another column because they are not in order anyway. This is only an visual shorting and don't affect entries structure, so don't worry.

Community
  • 1
  • 1
TheBosti
  • 1,354
  • 13
  • 19
  • 2
    Using null is fine: http://stackoverflow.com/questions/8753371/how-to-insert-data-to-mysql-having-auto-incremented-primary-key as documented by MySQL – Devon Bessemer Nov 07 '16 at 15:42
  • Why this should be corect anymore? I think your auto increment is never null so why insert it to be null ? – TheBosti Nov 07 '16 at 15:46
  • 2
    Because when you specify `NULL` or `0` on an auto increment column, MySQL automatically disregards the value you sent. This is required if you don't define the columns in your insert statement. – Devon Bessemer Nov 07 '16 at 15:47
  • Ok pretty close to me, i'll update my answer. Thanks & Have a nice day Devon. – TheBosti Nov 07 '16 at 15:48
  • The difference is their insert statement doesn't have the columns stated, so the query would fail (insert bad values) without the `NULL` because it would think `$sender` would be the `id`. – Devon Bessemer Nov 07 '16 at 15:49
  • I know this, but 0 or '' seems better for my then a NULL value that si at oposite side to auto increment. – TheBosti Nov 07 '16 at 15:54
  • I see that now, Thanks you so much. – Musawar Nov 07 '16 at 21:24