简体   繁体   中英

How to get max from sql in php

So I'm buidlign a website currently and there is a form connected to a database.

You can put some information in the form and it will send you an email and will put the information into the database.

So... I wanted it also ad a number into the database and auto count the number. This lead me thinking.... If I use max and just do +1 and insert that, Jobs done right!!.... Well no....

The code isn't working for me and I have no idea where to start...

This is al I made and when I do this I only get 1 as an answer.

$sql2 = "SELECT MAX(Nummer) FROM Leerling" ;
$sql3 = $sql2 + 1;

echo $sql3;

The insert statement is

"INSERT INTO Leerling(Nummer, Naam, Klas, Email, Bericht) VALUES ('" . $sql3 . "', '" .$naam. "', '" .$klas. "' , '" .$email. "', '" .$bericht. "');" ;

Which only inserts 0 into the database on the place where it says $sql2. I'm totaly clueless so any help is much needed help.

(Don't mind my English if it's not correct >.< I'm Dutch)

You should use MySQL AUTO_INCREMENT

Your column Nummer will increase its value every time you insert a new row and no need for calculate the MAX value to increase Nummer for the new row

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM