[英]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....
这使我开始思考...。如果我使用max并执行+1并将其插入,乔布斯就做对了!
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. 这是我所做的,当我这样做时,我只会得到1作为答案。
$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. 它只在$ sql2所在的位置将0插入数据库。 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 您应该使用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 每当您插入新行时,列Nummer的值都会增加,而无需计算MAX值即可增加新行的Nummer
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.