Pregunta

I'm writing a script, which shows records from database in a table. Last time i wanted to improve my script by adding "delete" option next to each table's tr. When i want to delete a record i use delete query and everything is fine. The problem is that when i want to add a record, it's getting duplicate(probably, because that i use counting rows in database and then getting id from this). I started to think - "How to make a proper way to keep adding next records without interfereing other ID's" and then i got an idea to get a MAX value from the table and than increament it and here is the problem. I have an operation like this:

$sql = "SELECT Id FROM `table` where Id = (SELECT max(Id) FROM table)"

In PHPmyAdmin it works, it returns me MAX Id, but i can't assign returned value to variable and then increment it(STRING error or smth can't really remember).
What to do next? when i want to get only one value, make a variable with this value and increment it to get next ID in database? Any other and better ideas how to make optimal adding and deleting records through the page?

UPDATE: I'm trying to make an autoincrement column but when i make smth like this:

$sql="CREATE TABLE table(Id INT NOT NULL AUTO_INCREMENT, Nick CHAR(30), Xpos INT, Ypos INT, Punkty INT, Ratuszlvl INT, Koszarylvl INT, Stajnialvl INT, Warsztatlvl INT, Kuznialvl INT, Ryneklvl INT, Tartaklvl INT, Cegielnialvl INT, Hutalvl INT, Zagrodalvl INT, Spichlerzlvl INT, Schoweklvl INT, Murlvl INT, Palaclvl INT, Data DATE, Czas TIME)";

I got the result

Incorrect table definition; there can be only one auto column and it must be defined as a key

¿Fue útil?

Solución

Let the id be autoincremental ni the database-schema (would be {value for column a}). (thats why it's not assigned in the insert-statement.)

Just do like this:

INSERT INTO `table` VALUES({value for column b}, {value for column c})

Here's an example:

DB-Schema:

Name of table: 
    groovy_machines

Columns:
    id (autoincrement, primary key)
    name (varchar, name of groovymachine)
    color (int, color of groovy machine)

Insertion

INSERT INTO 'groovy_machines' (
    'wonderhelicopter',5, 
    'grasshopperbike', 10,
    'dontgemewrongstoppermachine', 12
);

UPDATE: When creating an autoincremental column you must also assign it as a primary key, just as the mysql error states for you.

CREATE TABLE `groovy_machines` (
    `id` int( 11 ) NOT NULL AUTO_INCREMENT ,
    PRIMARY KEY ( `id` )
)

Your creation of table could look something like this:

CREATE TABLE `table` (
    `id` int( 11 ) NOT NULL AUTO_INCREMENT ,
    `Nick` CHAR(30), 
    `Xpos` INT, 
    `Ypos` INT, 
    `Punkty` INT, 
    `Ratuszlvl` INT, 
    `Koszarylvl` INT, 
    `Stajnialvl` INT, 
    `Warsztatlvl` INT, 
    `Kuznialvl` INT, 
    `Ryneklvl` INT, 
    `Tartaklvl` INT, 
    `Cegielnialvl` INT, 
    `Hutalvl` INT, 
    `Zagrodalvl` INT, 
    `Spichlerzlvl` INT, 
    `Schoweklvl` INT, 
    `Murlvl` INT, 
    `Palaclvl` INT,
    `Data` DATE, 
    `Czas` TIME,    
    PRIMARY KEY ( `id` )
)

Otros consejos

Use max(Id)+1 so it will gives you directly incremental value and store into variable directly.

Put:

SELECT max(Id)+1 FROM table
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top