I know this may be a stupid question to ask but I have really forgotten how to do it.
How do I auto increment a photo ID whenever a record is inserted into mySQL database ?
I would want it to start with 1 for the first record and then subsequently +1 for the next. Thanks
I have set my database column photoID to PK, NN and AI.
The MySQL documentation for AUTO_INCREMENT has a nice, clear example. In brief, if your field has been defined as auto incrementing, e.g.:
CREATE TABLE tablename (
photoID INT NOT NULL AUTO_INCREMENT,
anotherField VARCHAR(50),
....
);
then when you do an INSERT, if you do not specify a value for photoID, the value will auto increment (if you explicitly specify a value of NULL or 0 in the values list, the value will also auto increment):
INSERT INTO tablename (anotherField) VALUES ('something');
A SELECT would result in photoID value of 1, anotherField value of something.
Set the 'photoID' column on the database to 'autoincrement' using phpmyadmin. It is one of the options on it. It will automatically then do it for you.
CREATE TABLE foto ( ID int auto_increment, UNIQUE(ID) );
Related
I have a database table in phpMyAdmin that has:
[id, username, password, email, firstname, lastname, lastlogin]
Works just fine. The problem is it doesn't give each user an id number (they are all zeros and I want 1,2,3...). Every time a user submits their information from a form to the database, they should be assigned a number. First person to submit should be ID 1, second should be ID 2, and so on. How can I fix this in phpMyAdmin?
Thank you.
In phpMyAdmin, select your table and go to the "Structure" tab. Then on the row for your ID field, check the box under "A_I" to turn on auto increment.
To force all your records to update so they have different ID numbers, delete the ID column and then add it back again with the incrementing enabled.
Make sure that under "Index" you select "PRIMARY KEY", or else it will throw an error.
AUTO_INCREMENT should work just fine to solve your problem. Dagon posted a solution on how to alter your table which should work perfectly. If you prefer to create the table again but with AUTO_INCREMENT in place, you can use the following code:
CREATE TABLE myTable (
id INT NOT NULL AUTO_INCREMENT,
username ...,
password ...,
email ...,
firstname ...,
lastname ...,
lastlogin ...,
PRIMARY KEY (id)
)
// '...' represents the datatypes and other characteristics of the
// specific columns/fields
Let me know if that worked for you!
to add auto increment if required:
ALTER TABLE `YOUR_TABLE` CHANGE `id` `id` INT( 11 ) NOT NULL AUTO_INCREMENT
to fix the existing lack of id's
SET #count = 0;
UPDATE `YOUR_TABLE` SET `YOUR_TABLE`.`id` = #count:= #count + 1;
then
ALTER TABLE `YOUR_TABLE` AUTO_INCREMENT = 1;
I am trying to create a login-registration system with PHP mysql and HTML. I want that, when user will register with his email and password, an unique ID will generate into database only for him. Example: when a user register an unique number auto create like 1, 2, 3, 4... I want an unique ID of 5 digits instead of 1, 2, 3 auto increment number.
Make your id (primary) column auto-increment.
Also, add a record with id 10000 either manually or with code.
Next record added will have id 10001.
This solves your question.
To start with an AUTO_INCREMENT value other than 1, set that value with CREATE TABLE or ALTER TABLE, like this:
ALTER TABLE tbl AUTO_INCREMENT = 100000;
You can define auto increment value when you create table.
Fist make that field primary key and assign auto increment value to your desire number.
CREATE TABLE IF NOT EXISTS tableName (
id int(11) NOT NULL PRIMARY KEY,
.........
) ENGINE=InnoDB AUTO_INCREMENT=10001 DEFAULT CHARSET=utf8;
I hope you need to do this with your phpmyadmin UI. So go to phpmyadmin and follow the steps below.
(If you have already selected autoincrement field start from step 4).
1. In "Structure" tab of your table
2. Click on edit on column you want auto_increment
3. under "Extra" tab choose "auto_increment"
4. then go to "Operations" tab of your table
5. Look for "Table options" -> auto_increment Then type 10000
You can disable the auto increment and use uniqid() function then save it in the ID Section.
For this you can use ZEROFILL it will prepand required 0's to number so try this query:
ALTER TABLE `tbl_teacher` CHANGE `id` `id` INT(11) UNSIGNED ZEROFILL NOT NULL AUTO_INCREMENT;
;)
I have a table with 5 rows. Every time a user enters data into a form, it is entered into the table. My first column is called id and holds the number of the post. What I want to do is get the value of id from the previous row, add one to it and set it as the value in the current post's id field. How do I do this?
Just set that field as primary key and auto-increment, it will automatically do this for you. You won't have to fetch the previous row and add that field value to next one.
The SQL query you need is:
SELECT max(id)
FROM tableName;
Set attribute auto increment for "ID" field in the table that contains 5 columns.
You can use sql query like
"INSERT INTO my_table (id auto_increment,primary key(id))";
then you can get...
and eachtime you need not worry to insert id ,it will automatically increments
I would not recomend doing this as it could lead to a race condition.
Change the table structure and set the id field to be the primary key and set it to auto increment. This way anytime a new row is added, it will auto-magically be assigned the next ID.
see this answer on details of how to set auto increment.
here is the query to alter your table and it will set your field or column as primary key and also auto increment it.
ALTER TABLE tbl ADD id INT PRIMARY KEY AUTO_INCREMENT;
I have a db table created like this
CREATE TABLE products(id INT,
name varchar(32),
PRIMARY KEY(id,name),
quantity int,
avail varchar(5) );
If I use the following command on the command prompt, the value is inserted properly:
INSERT INTO products(name,quantity,avail) VALUES('stuffed bear doll',100,'OF_ST');
although the id is duplicated
but when I leave it inside the function like this
$query=sprintf("INSERT INTO products(name,quantity,avail) VALUES('%s',%d,'%s');",
$name,
$quan,
$avail);
mysql_query($query);
then there is no insertion done at all.
You needed to set auto_increment on the id field in your create table syntax. You can edit the column to add it.
Also, if $quan is not valid your SQL syntax will give you an error. Put quotes around it: VALUES('%s','%d','%s')
there is no problem in Your query but i think value of name field is duplicate. As your table structure id-name is primary mean two rows can not have same value of both id and name field. One time one field value can be same but not for both. And here in your table value of id field is always 0 so if in any row value of name field repeat it will not insert that row. And please make id as primary and auto increment so it will be better.
thanks
I'm using a simple form to insert posts to my MySQL database, I want each post to have an unique ID but don't know the proper way to do it.
I already created a column named "PID" and set it as a primary key however I don't know how to make each INSERT generate the PID. The only way I can think of is to make PHP look for the higher PID and add +1 to it. Is there an easier way to do it? Thanks
If you manage your MySQL database with a software like e.g. phpMyAdmin then you can set the "auto_increment" flag on this column.
Else you have to set it manually as described in this manual.
You need to set PID to be an Identity field
http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html
CREATE TABLE posts(
PID MEDIUMINT NOT NULL AUTO_INCREMENT,
someotherfield CHAR(30) NOT NULL,
...etc...,
PRIMARY KEY (PID)
)
You can make the field 'auto_increment'. That way, MySQL creates a unique id for each record. You can use mysql_insert_id to get the last inserted id.
Make the PID column auto_increment
Set that PID field to AUTO_INCREMENT. In PHPMyAdmin there's a INDEX field you can set to AI, if I remember correctly.
Using AUTO_INCREMENT
Use auto_increment on the field.
E.g. pid int primary key auto_increment
You can use auto increment on Id field.
Make the field auto_increment in your mysql table.