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