gpt4 book ai didi

php - SQL 更新行 while row[val]<$_POST['$val];

转载 作者:行者123 更新时间:2023-11-29 07:30:20 25 4
gpt4 key购买 nike

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

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

最佳答案

你要么没有发布完整的代码,要么你的 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;
}
....

关于php - SQL 更新行 while row[val]<$_POST['$val];,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51588447/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com