問題描述
我有一個表格,可以從數據庫中選擇信息并顯示它,我想知道是否有人可以提供代碼讓我通過單擊按鈕來更新列?
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?
示例表:(Access=Boolean)
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>
我希望有這樣的事情,ONCLICK button1 Run $Allowed SQLONCLICK button2 運行 $NotAllowed SQL
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' ");
推薦答案
您可以使用帶有 post 方法的表單和帶有命名屬性的提交類型的按鈕,并在條件語句中使用這些按鈕.
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.
即:
旁注:我刪除了轉義引號,因為我不確定這些引號是否已經設置在 echo 中.
Sidenote: I removed the escaped quotes, as I was not sure if those were already set inside an echo.
HTML 表單:
<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.
使用準備好的語句,或 帶有準備好的語句的 PDO,它們更安全.
腳注:
運行 UPDATE 查詢時,最好使用
mysqli_affected_rows()
以獲得絕對真實性.When running an UPDATE query, it's best to use
mysqli_affected_rows()
for absolute truthness.- http://php.net/manual/en/mysqli.affected-行.php
否則,您可能會得到誤報.
Otherwise, you may get a false positive.
這篇關于單擊按鈕時更新信息表的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!