problem inserting data into table

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hybris
Forum Contributor
Posts: 172
Joined: Wed Sep 25, 2013 4:09 am

problem inserting data into table

Post by hybris »

Hi,
How do I insert data to a table where the third column is autoincreaseing? Like
*age*nr*id*name*surname * time
Where id is autoincreasing.

Code: Select all

IF($stmt=$mysqli->prepare("INSERT INTO user (age, nr, name, surname, time) VALUES (?, ?, ?, ?, ?)" )) {
$stmt->bind_param('iisss' $age, $nr, $name, $lname, $time);
$stmt->execute();
}
If I just set age and nr the db is inserting age nr and id but as soon as I try to insert after if it doesn't work? What am I missing?
Thanks
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: problem inserting data into table

Post by social_experiment »

from what i've experienced you just leave the column out of the query and it auto-increases as it should
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
priyankagound
Forum Commoner
Posts: 27
Joined: Thu Sep 19, 2013 2:53 am

Re: problem inserting data into table

Post by priyankagound »

Try out with the below example:
suppose below is table created:
CREATE TABLE Persons
(
ID int NOT NULL AUTO_INCREMENT,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
PRIMARY KEY (ID)
)

so the insert qury will be as foolows for it:
INSERT INTO Persons (FirstName,LastName)
VALUES ('Lars','Monsen')

By default, the starting value for AUTO_INCREMENT is 1, and it will increment by 1 for each new record.

To let the AUTO_INCREMENT sequence start with another value, use the following SQL statement:

ALTER TABLE Persons AUTO_INCREMENT=100

Hope this helps.
Post Reply