im trying to make function that take every row from column inv by user id multiply it by 0,015 and then insert this value to zhod with same user id.
i have this but im lost already. if someone can help or simplify it ill be glad.
public function updateZhod($id) {
$sql = "UPDATE users
SET zhod = inv * 0.015
WHERE id = :id
AND verified = 1";
$stmt = $this->conn->prepare($sql);
$stmt->bindParam(':id', $id);
$stmt->execute();
}
if (isset($_POST['action']) && $_POST['action'] == 'fetchAllUsersInv') {
$output = '';
$data = $admin->fetchAllUsersInv(0);
if ($data) {
$output .= '<table class="table table-striped table-bordered text-center">
<thead>
<tr>
<th>ID</th>
<th>Inv</th>
<th>Zhod</th>
</tr>
</thead>
<tbody>';
foreach ($data as $row) {
$zhodValue = $row['inv'] * 0.015;
$output .= '<tr>
<td>' . $row['id'] . '</td>
<td>' . $row['inv'] . '</td>
<td>' . $zhodValue . '</td>
</tr>';
}
$output .= '</tbody>
</table>';
echo $output;
} else {
echo '<h3 class="text-center text-secondary">:( No inv</h3>';
}
}
if (isset($_POST['action']) && $_POST['action'] == 'updateZhod') {
$admin->updateZhod();
echo 'Zhod values updated successfully!';
}
<script>
$(document).ready(function() {
$("#zhodLed").click(function() {
$.ajax({
url: 'assets/php/admin-action.php',
type: 'POST',
data: {
action: 'updateZhod'
},
dataType: 'json',
success: function(response) {
if (response.success) {
$("#invValue").text(response.newValue.toFixed(2) + " ...");
} else {
alert("Error: Failed to update zhod value.");
}
},
error: function(jqXHR, textStatus, errorThrown) {
console.error("AJAX Error:", textStatus, errorThrown);
alert("Error: Unable to make the AJAX request.");
}
});
});
});
</script>
this should be simple func that take one value from inv multiply it by 0,015 and insert it to zhod but its probably behind my skills
2
Answers
Move your admin block in the loop by userId, with it’s id parameter:
Btw: you need a flag to multiply this value one time, in order to prevent more than once multiplication with every action.
If I read your intents from the question
im trying to make function that take every row from column inv by user id multiply it by 0.015 and then insert this value to zhod with same user id.
And then I read your Javascript, which calls the PHP function with no parameter other than an action, I mean you dont pass an
id
parameter or anything else.Then I think you are saying you want to do this all in one go, so no need to read all the data before running the query. So the query could simply be
Your code could then just select all the changed data and send it back to the javascript as HTML which you could simply place in a location of the page using the javascript.