Hi Tarential,
The auto increment function is indeed very useful. It makes it very easy to update a specific row as each row will have a unique id number. The auto_increment field should always be your primary key, as no two rows can be the same.
So, lets say for the sake of argument, your table schematic is as follows:
CREATE TABLE tablename (
name varchar(150) NOT NULL,
address varchar(150) NOT NULL,
comments text,
PRIMARY KEY (name)
);
This stores info posted from a form. Name, Address, Comments.
To this you would add the following:
id int(4) NOT NULL auto_increment,
So, your new schematic is as follows:
CREATE TABLE tablename (
id int(4) NOT NULL auto_increment,
name varchar(150) NOT NULL,
address varchar(150) NOT NULL,
comments text,
PRIMARY KEY (id)
);
So, lets say someone posts some data. Your sql syntax would be something like:
$query = "INSERT INTO tablename (name,address,comments) VALUES ('$name', '$address', '$comments')";
$result = mysql_query($query) or die(mysql_error());
Notice that you don’t add anything for the id field. This increments automatically. When you view the schema for your database, you’ll see the id is there automatically. So your first row will have the id value of 1, the 2nd of 2 and so on. Each row has a different id.
View your rows by running a query and you`ll see what I mean.
$query = "SELECT * FROM tablename";
$result = mysql_query($query) or die(mysql_error());
while ($row=mysql_fetch_array($result))
{
echo $row['name'] . "<br>n";
echo $row['address'] . "<br>n";
echo $row['comments'] . "<br>n";
echo $row['id'] . "<br>n";
}
You would then use this id to update a certain row. Pull the data from your database and store the value of id in a hidden form field. When the info gets posted use this id to change a specific row. You can also pull a certain row from the database using the WHERE query.
$query = “SELECT * FROM tablename WHERE id = ‘2’”;
There is no need to create id numbers yourself when mysql can do it for you.
Check the mysql website for more information. Hope this made some sense.