通过单击按钮更新信息表 [英] Update a table of information on a button click
问题描述
我有一个表格,它将从数据库中选择信息并显示它,我想知道是否有人可以为我提供一个代码来更新点击一个按钮的列?
示例表:(Access = Boolean)
ID - 名称 - 访问
--- ------------------------
1 - John - 1
------------- --------------
2 - Ben - 1
----------------------- ----
3 - 特里 - 0
---------------------------
我现有的按钮是基于bootstrap的,
< button type = \button \id = \passed\class = \btn btn-success btn-flat \>< i class = \ fa fa-check \>< / i>< / button>
我希望得到像这样的东西,ONCLICK button1运行$允许SQL
ONCLICK button2运行$ NotAllowed SQL
$ allowed = mysqli_query($ conn,UPDATE users SET Access =1WHERE id ='27' );
$ notallowed = mysqli_query($ conn,UPDATE users SET Access =0WHERE id ='453');
您可以使用post方法和提交具有命名属性的按钮类型并在条件语句中使用这些按钮。
即:
旁注:I删除了转义引号,因为我不确定这些引用是否已经设置在echo中。
HTML表单:
< form method =postaction =handler.php>
< button type =submitname =insufficientid =insufficientclass =btn btn-danger btn-flat>< i class =fa fa-times>< / I>< /按钮>
< / form>
PHP:
< $ c $<?php
// db连接
if(isset($ _ POST ['passed'])){
$ allowed = mysqli_query($ conn,UPDATE users SET Access =1WHERE id ='27');
$ b if(isset($ _ POST ['insufficient'])){
$ notallowed = mysqli_query($ conn,UPDATE users SET Access =0WHERE id ='453');
-
小心如果这在任何给定的点上都有用户交互的话。
使用 准备好的声明 或 PDO包含准备好的语句 ,它们更安全。 > - < a href =http://php.net/manual/en/mysqli.affected-rows.php =nofollow> http://php.net/manual/zh/mysqli.affected-rows.php
脚注:
$ b 运行UPDATE查询时,最好使用 mysqli_affected_rows()
来表示绝对真实性。
否则,您可能会收到误报。
I have a table which will select information from a database and display it, I'm wondering if anyone can provide a code for me to update a column on the click of a button?
Example Table: (Access=Boolean)
ID - Name - Access
---------------------------
1 - John - 1
---------------------------
2 - Ben - 1
---------------------------
3 - Terry - 0
---------------------------
My exisiting button is based on bootstrap,
<button type=\"button\" id=\"passed\" class=\"btn btn-success btn-flat\"><i class=\"fa fa-check\"></i></button>
<button type=\"button\" id=\"insufficient\" class=\"btn btn-danger btn-flat\"><i class=\"fa fa-times\"></i></button>
I was hoping for something like this, ONCLICK button1 Run $Allowed SQL ONCLICK button2 Run $NotAllowed SQL
$allowed = mysqli_query($conn," UPDATE users SET Access = "1" WHERE id = '27' ");
$notallowed = mysqli_query($conn," UPDATE users SET Access = "0" WHERE id = '453' ");
You can use a form with a post method and a submit type of buttons with named attributes and use those in a conditional statement.
I.e.:
Sidenote: I removed the escaped quotes, as I was not sure if those were already set inside an echo.
HTML form:
<form method="post" action="handler.php">
<button type="submit" name="passed" id="passed" class="btn btn-success btn-flat"><i class="fa fa-check"></i></button>
<button type="submit" name="insufficient" id="insufficient" class="btn btn-danger btn-flat"><i class="fa fa-times"></i></button>
</form>
PHP:
<?php
// db connection
if(isset($_POST['passed'])){
$allowed = mysqli_query($conn," UPDATE users SET Access = "1" WHERE id = '27' ");
}
if(isset($_POST['insufficient'])){
$notallowed = mysqli_query($conn," UPDATE users SET Access = "0" WHERE id = '453' ");
}
Be careful if this has any user interaction at any given point.
Use a prepared statement, or PDO with prepared statements, they're much safer.
Footnotes:
When running an UPDATE query, it's best to use mysqli_affected_rows()
for absolute truthness.
Otherwise, you may get a false positive.
这篇关于通过单击按钮更新信息表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!