Php插入并更新mysql,限制为三行 [英] Php insert and update mysql with a limit of three rows

查看:65
本文介绍了Php插入并更新mysql,限制为三行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个php脚本,可以将表单中的输入记录到mysql数据库表中。我只需要引用最后三个输入。为了避免表填充输入,我正在寻找插入此数据的方法,直到创建3行,之后它必须更新现有行,以便第一行更新为新输入,第二行到前一个输入,第三个到前一个输入。我怎么能做到这一点?



我尝试过:



i尝试:

I have a php script that logs inputs from a form into a mysql database table. I need a reference to only the last three inputs. To avoid the table to get filled up with inputs i'm looking for a way to insert this data untill 3 rows are created, after which it has to update the existing rows so that the first one updates to the new input, the second one to the former first input and the third one to the former second input. How can i accomplish this?

What I have tried:

i tried:

$q = "select value from inputs where userid = '$userid' and name = 'input' order by id desc";
$result = mysqli_query($conn, $q);

我可以使用的前三行:

for the first three rows i can use:

if(mysqli_num_rows($result) < 4) {
	$q = "insert into inputs (userid,name,value) values ('$userid','input','$value')";
}

之后必须成为:

after which it has to become:

else {
	$q = "update inputs set value = '$value' where userid = '$userid' and name = 'input' limit 3";
}

它必须更新所有连续的行

where it has to update all the successive rows

推荐答案

q =从输入中选择值,其中userid ='
q = "select value from inputs where userid = '


userid'和name ='input'顺序由id desc;
userid' and name = 'input' order by id desc";


result = mysqli_query(
result = mysqli_query(


这篇关于Php插入并更新mysql,限制为三行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆