Jump to content

Auto Increment Help Needed


refiking

Recommended Posts

I am trying to use the auto increment feature and I am completely stuck.  I am trying to automatically give each form a new record number.  What am I missing?  Here is the code:

 

$query1 = "SELECT last_insert_id(record_id) FROM Records";

$record1 = mysql_query($query1);

$record_id = $record1 + 1;

Link to comment
https://forums.phpfreaks.com/topic/54517-auto-increment-help-needed/
Share on other sites

If you use mysql_isert_id() immediately after inserting a record you'll get the last index number of the record added.

mysql_query("INSERT INTO `table` (`name`) VALUES ('".$name."')");
$recordid=mysql_insert_id();

$recordid would then represent the last entry.

 

If you've not inserted a record and want to get the last record number you can use this:

$fetch=mysql_fetch_assoc(mysql_query("SELECT `recordid` FROM `table` ORDER BY `recordid` DESC LIMIT 1"));
$lastrecordid=$fetch['recordid'];

 

EDIT: corrected spelling of "SELCT" to "SELECT" - typo!

The following error message appears:

 

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/ctpwebco/public_html/leads/proc.php on line 60

 

 

Here is the code for line 60:

 

$fetch = mysql_fetch_assoc(mysql_query("SELCT `record_id` FROM `Records` ORDER BY `record_id` DESC LIMIT 1"));

 

 

Neither problem is fixed, yet?  Did I miss a step or is there something else to it?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.