Update a field in mysql table on click - php

I have a binary field in a mysql database that gets updated by a button on an html form.
So, I click the button for every row and it changes the mysql value for each of the rows…
The problem is that it has a delay, when I click on one button of a certain row it only gets updated when I click on the next. But if I click on that button and refresh the page it gets changed.
I've look into ways of updating the page, but I just can't get it to work...
<form action='index.php' method='POST'>
<input type=hidden name='keyword2' value='$keyword'>
<input type='submit' value = 'SELECT' >
</form>";
<?php
j($_POST['keyword2']);
function j($q){
$n = "SELECT `CÔR` FROM `keywords` WHERE `keyword`='$q'";
$b = mysql_query ($n);
$row = mysql_fetch_array($b);
echo $row['CÔR'];
$t = $row['CÔR'];
if ($t == 1) {
$m = "UPDATE `keywords` SET `CÔR`=0 WHERE `keyword`='$q'";
mysql_query ($m);
}
if ($t == 0) {
$l = "UPDATE `keywords` SET `CÔR`=1 WHERE `keyword`='$q'";
mysql_query ($l);
}

Related

Refreshing mysql query on checkbox check

I have a control panel in php and mysql that displays on page load. I have a checkbox on that page(default off, not stored in db or tied to anything other than being an option to show old items), that when i click it, i want the query to refresh the data with the new filter applied. Basically on load it show only active items, when they check the checkbox i want the page to reload showing outdated items.
My code is:
<form action="" method="POST">
<input type="checkbox" name="filter" value="true">
</form>
<?PHP
$showold = (isset($_REQUEST['filter']));
if ($showold === 'checked' )
{
$showold = "WHERE `MyTable` = '0'";
}
else
{
$showold = "WHERE `MyTable` = '1'";
}
$query_NowPouring = "SELECT * FROM MyTable $showold";
$result = mysqli_query($mysqli, $query_NowPouring);
if($result){
while($row = mysqli_fetch_array($result)){
#set variables here and display the info in table here.
}
}
?>
Your code (isset($_REQUEST['filter'])); is returning boolean like 0 or 1 not string and you are matching it with string at if ($showold === 'checked' ) .
Problem is you can not compare boolean with string.

Using data in <select> options in MySQL query

I would like to delete a row from an MySQL database. The row that I'd like to delete is displayed in a box, with each being obtained via a loop and SELECT statement.
I've already got the rows in the database being displayed accordingly; however, I'd like a button that once pressed, would delete the selected option from the database.
Here is my current code:
<form action="" method="post">
<label>Patient Name:</label>
<br><br>
<select name="patient" id="patient">
<?php
$conn = new mysqli("localhost", "root", "", "as2");
$result = $conn->query("SELECT patientID, patientName, address FROM patient ORDER BY patientName ASC");
while ($row = $result->fetch_assoc()){
$patientName = $row['patientName'];
$address = $row['address'];
echo "<option value=\"patient\">" .$patientName. ", ".$address."</option>";
}
?>
</select>
<input type="submit" name="delete" value="Delete Record">
</form>
How would I go about making the "Delete Record" button delete the selected option from the database?
first record (patientID) in a string
$patientID = $row['patientID'];
then Add $patientIDto (option value) so it become :
echo "<option value=".$patientID.">" .$patientName. ", ".$address."</option>";
then add this code After everything (ofc outside the "while" loop) :
<?php
$selected_patient = $_POST['patient'];
if( $_SERVER['REQUEST_METHOD'] === 'POST'
&& isset($_POST['delete']) && isset($_POST['patient']) ) {
if( !empty($_POST['patient']) ){
$patient_ID = mysql_real_escape_string($selected_patient);
if ( $conn->query("DELETE FROM patient WHERE patientID={$patient_ID}") )
echo "user has been deleted successfully";
else
echo "Error deleting";
}
}
?>
now you'r good to go , after click delete button refresh your page and Boom! , the user will Disappears
and if you want a real time Action u can use (Ajax)
Try this
while ($row = $result->fetch_assoc()){
$patientName = $row['patientName'];
$address = $row['address'];
$patientID = $row['patientID']; //get patient id
echo "<option value=\"$patientID\">" .$patientName. ", ".$address."</option>"; // change in option value
}
Now on form submit, you will get patient id in $_POST['patient'] , can write your delete query.
Hope this will hope.

php and mysql anomaly

I have a php page which is showing data in a HTML table taken from a MySQL database. Part of the table has a submit button for people to click to like a row. When clicked it runs a form which reloads the page and attempts to process the new inputs. In the example below all the inputs "submit", "timestamp" and "timestamp2" are set after the button is clicked. In the example the query $jpgh1 works and immediately reloads the row with the updated ticks count. If I use $jpgh2 the page reloads but is not updated. I have tried encasing $rt1 and $rt2 in quotes but that doesn't work. The reason I have the $admin bit is because this is being tested on a live system and I don't want the other users to see the testing. Currently the two values for timestamp and timestamp2 are being shown in the fields on the selected row and they match the values which work in $jpgh1.
if(isset($_POST['submit'])) {
If((isset($_POST['timestamp'])) and (isset($_POST['timestamp2']))) {
$rt1 = $_POST['timestamp'];
$rt2 = $_POST['timestamp2'];
$jpgh1 = mysql_query('update topics set ticks=1 where timestamp=1486723327 and timestamp2=1487081058');
$jpgh2 = mysql_query('update topics set ticks=3 where timestamp=$rt1 and timestamp2=$rt2');
}
}
<?php if($_SESSION['username'] == $admin) {
$t1 = $dnn2['timestamp'];
$t2 = $dnn2['timestamp2'];
?>
<form action='' method='POST'>
<input type='text' id='timestamp' name='timestamp' value='<?php echo $t1; ?>'/><br/>
<input type='text' id='timestamp2' name='timestamp2' value='<?php echo $t2; ?>'/><br/>
<input type='submit' name='submit' value='Tick'/></form>
<?php
}
?>
Needless to say after I posted I found the answer.
if(isset($_POST['submit'])){
If((isset($_POST['timestamp'])) and (isset($_POST['timestamp2'])))
{
$rt1 = $_POST['timestamp'];
$rt2 = $_POST['timestamp2'];
$rt3 = "update topics set ticks=1 where timestamp=$rt1 and timestamp2=$rt2";
$jpgh3 = mysql_query($rt3);
}
}

Creating Link For User To Delete Row

I'd like to ask two things for this particular user ability. The first is how to delete a row upon user clicking a button. The second is....would this be a good idea? How can I create a safe environment for someone to do this. This is what I've got so far :
<?php
include_once "db_conx.php";
if($_POST['wall'] == "post") {
//open if($_POST['wall'] == "post")
$id = mysqli_real_escape_string($db_conx, trim($_POST['id1']));
if($id == " ")
{
exit();
}
else
$sql = "SELECT FROM courseprogress WHERE userid='$id' LIMIT 1";
$results = mysqli_query($db_conx, $sql);
$sidebar = mysqli_num_rows($results);
if($sidebar > 0) {
//close if($sidebar > 0)
while($row = mysqli_fetch_assoc($results))
{
$sql = mysqli_query("DELETE FROM courseprogress WHERE userid='$id'");
$results = mysqli_query($db_conx, $sql);
}
//close if($sidebar > 0)
}
else
{
echo 'Already Complete!';
echo "<pre>";
var_dump($sql);
echo "</pre><br>";
}
//close if($_POST['wall'] == "post")
}
?>
Right now I'm in the process of dumping out variables, but can't seem to get my id variable right.
The idea is to "start over" in a sense. The table holds the user progression and settings. Once they've decided they need to start over they will be allowed to do so by simply deleting the row. When the begin again the row will be created again.
A little more information:
The small form script I was trying to use is:
<div class="userInfoContain"><div class="positionRight"><div id="form"> <form><div class="submit"><input type="hidden" id="id" value="'.$id.'" /><input type="submit" name="button" id="button" value="Start Over" onclick="return false" onmousedown="javascript:wall();"/><img src="images/loading.gif" alt="" width="15" height="15" id="loadingstart" /></div></form></div></div></div>
<script type="text/javascript">
$(document).ready(function(){
$('#loadingstart').hide();
});
function wall(){
$('#loadingstart').show();
var id = $('#id').val();
var URL = "./includes/start-over-user.php"; /////post.php will be equal the variable "comment"
$.post(URL,{wall:"post",id1:id},function(data){//parameter wall will be equal "post", name1 will be equal to the var "name" and comment1 will be equal to the var "comment"
$("#result").prepend(data).show();// the result will be placed above the the #result div
$('#loadingstart').hide();
});
}
</script>
1) Safely allow user to delete: The safest way is to not allow delete permissions on the MySQL user that is being used by the website. A method called soft delete is much safer for deleting rows in MySQL tables. This involves adding a column named "is_deleted" to the table where you are making this update. When is_deleted is set to 0, allow the row to act normally. When a user sets is_deleted to 1, it should act as if it doesn't exist. In this example, I am assuming you have set $is_deleted to the column is_deleted in your table:
if($is_deleted == '1')
{
// Don't display
}
else
{
// Display
}
2) How to implement: The best way to do this is with:
UPDATE tablename SET is_deleted = '1' WHERE id = '".$id."'
which should be executed through AJAX command or a link that will execute the MySQL query.
to delete I advice you to use ajax , it's perfect and you can have more control on this action
the other Problem i need more explanation i didn't understand you good

two submissions for a form

I have PHP code which creates a list with radio buttons for each list item.
$result = mysql_query("SELECT * FROM attitudes WHERE x_axis = '$famID'",$db);
$rowcheck = mysql_num_rows($result);
while ($row_user = mysql_fetch_assoc($result))
foreach ($row_user as $col=>$val) {
if ($col != $famID && $col != 'x_axis') {
list($famname) = mysql_fetch_row(mysql_query("SELECT familyname FROM families WHERE families_ID='$col'",$db));
echo "col $col famname $famname is val $val.";
echo "<input type = \"radio\" name = \"whichfam\" value = \"$col\" />";
echo "</br>";
}
}
Then I have a submit button at the bottom (and form tags for the whole thing)
I want to have two possible submissions. This code is intended to let the player raise or lower a value. They click on one of the radio buttons, and then select "Raise", or "Lower". It should then post to a backend and execute code to either raise or lower that value. I know how to do this in jquery, but I don't know how to have two SUBMIT buttons in PHP.
How can I do this?
(Specifically, how do I make two submission buttons work, the backend code should be relatively simple, $_POST or whatever)
Is this what you're looking for?
<button type="submit" name="submit1" value="submit1">submit1</button>
<button type="submit" name="submit2" value="submit2">submit2</button>
then
if(isset($_POST["submit1"])) {
} else if(isset($_POST["submit2"])) {
}

Categories