SQL更新row而row [val] <$ _ POST ['$ val];

时间:2018-07-30 06:53:19

标签: php mysql

所以我只想在满足少数条件的情况下更新行。

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。如果保存的值小于POST值

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

1 个答案:

答案 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;
}
....