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:
PHP:
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.
The buttons are running on the front-end (javascript) but the SQL to update your database is running on the back-end (php) so we need to connect these two together.
And then each button would look like this:
Your query would then go in
update.php
and the javascript data you're passing in ind
would be available in$_POST
.Now, with all of this being said, a few things: