Reputation: 641
I am trying to update a specific row column in a table once a button is clicked. In my table, all rows have a "scheduled" column and its unique key is "orderid"
Now in my display table (using a for each), I added an if condition in the buttons (yes if 1 no if 0).
<td>
<?php
require_once($routePath . "_config/db.php");
$dbConfig = new config_db();
$db = $dbConfig->init();
if (($rowOrder['sched']) == 1) {
?>
button type="submit" id="sched_button" name="sched_button" value="SCHED_BUTTON" class="btn btn-success">YES</button>
<?php
} else if (($rowOrder['sched']) == 0) {
?>
<button type="submit" id="sched_button" name="sched_button" value="SCHED_BUTTON" class="btn btn-danger">NO</button>
<?php
}
?>
</td>
Here is my code for the button. I am trying to change 0 to 1 or 1 to 0. I know there is something missing with my SQL Update. What could I add so that the scheduled column is updated? (depending on the specific row where I clicked the button)
PS I had the "scheduled" column turn into a shortcut "sched"
if (isset($_POST['sched_button']) && $_POST['sched_button'] == 'SCHED_BUTTON')
{
if (($rowOrder['sched']) == 1) {
$stmt = $db->prepare('UPDATE order SET scheduled = 0'));
$stmt->execute();
} else if (($rowOrder['sched']) == 0) {
$stmt = $db->prepare('UPDATE order SET scheduled = 1'));
$stmt->execute();
}
}
Upvotes: 1
Views: 1675
Reputation: 164
display.php
<td>
<?php
require_once($routePath . "_config/db.php");
$dbConfig = new config_db();
$db = $dbConfig->init();
if (($rowOrder['sched']) == 1) {
?>
<button class="btn btn-success" onclick="update(1,<?php echo $rowID; ?>)">YES</button>
<?php
} else if (($rowOrder['sched']) == 0) {
?>
<button class="btn btn-danger" onclick="update(0,<?php echo $rowID; ?>)">NO</button>
<?php
}
?>
</td>
<script>
function update(value,id) {
$.ajax({
url: "update.php",
type: "POST",
data: {"id": id, "value":value},
success: function () {
console.log('Updated');
},
error: function (err) {
console.log(err);
}
});
}
</script>
update.php
<?php
require_once($routePath . "_config/db.php");
$dbConfig = new config_db();
$db = $dbConfig->init();
if(isset($_POST['id'])&& isset($_POST['value'])){
$id = $_POST['id'];
$value = $_POST['value'];
$stmt = $db->prepare("UPDATE order SET scheduled = $value WHERE orderid = ?");
$stmt->bind_param('i', $id); // Bind "$id" to parameter.
$stmt->execute();
}
Upvotes: 0
Reputation: 4719
This is the quick method to update the scheduled
column :
the display
<td>
<?php
require_once($routePath . "_config/db.php");
$dbConfig = new config_db();
$db = $dbConfig->init();
if (($rowOrder['sched']) == 1) {
?>
<button type="submit" id="sched_button" name="sched_button" value="1" class="btn btn-success">YES</button>
<?php
} else if (($rowOrder['sched']) == 0) {
?>
<button type="submit" id="sched_button" name="sched_button" value="0" class="btn btn-danger">NO</button>
<?php
}
?>
</td>
the query
<?php
if (!empty($_POST['sched_button']))
{
$stmt = $db->prepare("UPDATE order SET scheduled = ?"));
$stmt->bind_param("i", $_POST['sched_button'];
$stmt->execute();
}
Upvotes: 1
Reputation: 4524
So your issue is, the form handler has no idea which orderid to process. You could adapt your form to something like this:
<td>
<?php
require_once($routePath . "_config/db.php");
$dbConfig = new config_db();
$db = $dbConfig->init();
?>
<button type="submit" id="sched_button" name="sched_button_<?= $rowOrder['sched'] ?>" value="<?= $rowOrder['orderid'] ?>" class="btn btn-success">YES</button>
</td>
This will either submit with sched_button_0
or sched_button_1
and the value is the orderid.
Your handler could look something like this:
if (isset($_POST['sched_button_0'])) {
$stmt = $db->prepare('UPDATE order SET scheduled = 1 WHERE orderid = ?');
$stmt->bind_param('i', $_POST['sched_button_0']);
$stmt->execute();
} else if (isset($_POST['sched_button_1'])) {
$stmt = $db->prepare('UPDATE order SET scheduled = 0 WHERE orderid = ?');
$stmt->bind_param('i', $_POST['sched_button_0']);
$stmt->execute();
}
I am assuming you're using mysqli to connect to the database.
Upvotes: 0
Reputation: 87
<button type="submit" id="sched_button" name="sched" value="<?php echo ($rowOrder['sched'] == 1 ? 0 : 1); ?>" class="btn btn-<?php echo ($rowOrder['sched'] == 1 ? "success" : "danger"); ?>"><?php echo ($rowOrder['sched'] == 1 ? "YES" : "NO"); ?></button>
if (isset($_POST['sched'])) {
$stmt = $db->prepare("UPDATE order SET packageid =$_POST['sched']");
$stmt->execute();
}
Upvotes: 0
Reputation: 371
Button values
to 1 or 0 , then on on submit it’ll do the job
Edit; as @Hasta Dhana said you’re missing the column packageid
Upvotes: 0