所以我只想在满足少数条件的情况下更新行。
$val = mysqli_real_escape_string($conn,$_POST['$result']);
$sql = "SELECT * FROM user_video WHERE video_id = '$id' AND user_uid = '".$_SESSION['u_uid'] ."'";
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);
if($resultCheck>0){
因此,如果我已经拥有一个,则不需要插入新值,只需更新值即可。
if ($row['done']< $val) {
$sql = "UPDATE user_video SET obejrzane='$val' WHERE video_id = '$id' AND user_uid='".$_SESSION['u_uid'] ."'";
mysqli_query($conn, $sql);
}
当我想插入新数据时,我的代码工作正常。但是,当我想更新时,即使$ _POST ['$ val']小于连续的一个,每次也要更新...对您有帮助吗?!
<?php
session_start();
include 'db.php';
$val = mysqli_real_escape_string($conn,$_POST['$result']);
$time = mysqli_real_escape_string($conn,date("Y-m-d H:i:s"));
$id = mysqli_real_escape_string($conn,$_POST['$title']);
$sql = "SELECT * FROM user_video WHERE video_id = '$id' AND user_uid = '".$_SESSION['u_uid'] ."'";
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);
if($resultCheck>0){
if ($row['done']< $val) {
$sql = "UPDATE user_video SET obejrzane='$val' WHERE video_id = '$id' AND user_uid='".$_SESSION['u_uid'] ."'";
mysqli_query($conn, $sql);
}
echo $resultCheck;
}
else{
if (!empty($val)) {
$sql = "INSERT INTO user_video (user_uid, video_id, done, created) VALUES ('".$_SESSION['u_uid'] ."', '$id', '$val', '$time');";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
}
else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
}
$conn->close();
答案 0 :(得分:1)
您要么没有发布完整的代码,要么您的php正在浏览错误消息undefined index 'done'
,因为您从未实际访问过SELECT
查询中的值。
代码应如下所示:
<?php
....
if($resultCheck>0){
while($row = mysqli_fetch_array($result)){ //Fetch data from the SELECT query
if ($row['done']< $val) {
$sql = "UPDATE user_video SET obejrzane='$val' WHERE video_id = '$id' AND user_uid='".$_SESSION['u_uid'] ."'";
mysqli_query($conn, $sql);
}
}
echo $resultCheck;
}
....