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

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

报告相同问题?

悬赏问题

  • ¥100 如何修改别人网站内的数字,各位接单的私我
  • ¥15 poi怎么在指定位置之后生成多个表格
  • ¥15 nginx在location设置了client_max_body_size依旧报错413
  • ¥15 C#添加WCF服务引用客户端调用方法没有反应
  • ¥15 stm32f103c8t6与esp8266
  • ¥15 使用Hadoop的MapReduce program
  • ¥15 Python发生IndexError错误如何解决?
  • ¥15 如何用matlab搭建激光器
  • ¥15 51单片机两路互补SPWM输出全桥逆变
  • ¥15 outlook左边用户名下拉为啥打不开了