How to Insert an Auto-Numeric Key in PHP
An auto-numeric key provides databases with a method to create a unique number for each table record. The number key auto-increments, based on the last number used in a previously inserted row. You insert a new record, and the auto-numeric field automatically increments the identification number and inserts the new number into the table.
Instructions
-
-
1
Right-click the PHP file you want to use to insert the auto-number record. Click "Open With," and then click your PHP editor in the list of programs.
-
2
Create the MySQL connection to the database server. The following code creates a connection to your database server:
$connection = mysql_connect("server","yourusername","yourpass");
mysql_select_db("database", $connection);
Replace "server" with your own database server name. Replace the username and password with your own. The "database" is the database name that contains your tables.
-
-
3
Set up the query. The SQL "insert" query inserts a new record into the database. The auto-numeric field is not listed in the query, because the database engine handles the numeric increment. The following code inserts a record:
$query = "insert into customers (name) values ('joe customer')";
-
4
Create the new record. The following code inserts the new record:
mysql_query($query);
-
5
Close the SQL connection. After you finish using the database, you must close the connection. The following code closes the MySQL connection:
mysql_close($connection);
-
1