所以,总结一下我的问题,当用户输入表单时,我有一个小复选框,当选中该复选框时,它应该将+1添加到数据库中的当前值,但我只是不了解什么使用逻辑
表-用户
字段
idUsers-PK(int)
uidUsers(用户名)
emailUsers
pwdUsers
user_type
shifts_done-表单提交期间每次选中此复选框都需要增加的字段
<div class="form-group">
<input type="checkbox" id="shifts_done" name="shifts_done"/>
</div>
<!-- Submit form -->
<br>
<button class="btn btn-lg btn-primary btn-block" name="create-report" type="submit">Create</button>
if(isset($_POST['shifts_Done'])) {
$username=mysqli_real_escape_string($conn,$_POST['uid']);
$shifts_done=mysqli_real_escape_string($conn,$_POST['shifts_done']);
$increment = " UPDATE users SET shifts_done = '".$shifts_done."' WHERE uidUsers = '".$username."'";
}
<?php
require 'dbh.inc.php';
if(isset($_POST['shifts_done'])) {
$username = mysqli_real_escape_string($conn, $_POST['uid']);
$shifts_done = mysqli_real_escape_string($conn, $_POST['shifts_done']);
$increment = "UPDATE users SET shifts_done = shifts_done + 1 WHERE uidUsers = '" . $username . "'";
mysqli_query($conn, $increment);
}
if(isset($_POST['create-submit'])) {
//Declaração de variáveis
$username = $_POST['uid'];
$origin = $_POST['origin'];
$destination = $_POST['destination'];
$transport = $_POST['transport'];
$status = $_POST['status'];
$date=strtotime($_POST['date']);
$date=date("Y-m-d",$date);
$time=strtotime($_POST['time']);
$time=date('H:i:s',$time);
//inserts new shift
$sql = "INSERT INTO shifts (name, origin, destination, transport_number, status, date, time) VALUES (?,?,?,?,?,?,?)";
$stmt = mysqli_stmt_init($conn);
if(!mysqli_stmt_prepare($stmt, $sql)) {
header("Location: ../shifts.php?error=sqlerror");
exit();
}
else {
mysqli_stmt_bind_param($stmt, "sssssss", $username,$origin, $destination, $transport, $status, $date, $time);
mysqli_stmt_execute($stmt);
header("Location: ../shifts.php?newrecord=success");
exit();
}
}
现在我真的不知道更新查询是否正确,因为用户名被用作更新其他字段的变量,但是我也不真正知道如何执行我打算做的增量部分。
>在这两个方面的任何帮助将不胜感激,谢谢。
答案 0 :(得分:1)
解决方案是将查询重写为:
$increment = "UPDATE users SET shifts_done = shifts_done + 1 WHERE uidUsers = '".$username."'";