sql更新行while行[val]< $\u post['$val];

cyej8jka  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(349)

所以我只想在满足少数条件时更新行。

1. 如果我已经有一个特定行的值

$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){

所以如果我已经有一个我不需要插入新值,只想更新值。

2. 如果保存的值小于后置值

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);
}

当我想插入新数据时,我的代码工作正常。但当我想更新时,每次更新时,即使$\u 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();
63lcw9qa

63lcw9qa1#

您可能没有发布完整的代码,或者您的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;
}
....

相关问题