douxiao0400 2015-12-29 20:47
浏览 191
已采纳

如何从PHP中的sql获取最大值

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)

  • 写回答

1条回答 默认 最新

  • drogon982007 2015-12-29 20:59
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 chaquopy python 安卓
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题