MySql值更新失败

时间:2017-09-28 06:02:54

标签: php sql mysqli

我正在尝试创建可以更改为用户的图库,用户只需为某些图像选择等级,并将等级存储在SQL数据库中。但是当我尝试更新数据库中的排名时,它会失败,但我没有收到任何错误,只有我的if (mysqli_query($conn, $sql)失败。

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "photodb";

$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT * FROM `photos` ORDER BY `rank` ASC";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    while ($imageRow = mysqli_fetch_assoc($result)) {
        if (!empty($_POST["imageRank$imageRow[rank]"])) {
            $valueRankNew = $_POST["imageRank$imageRow[rank]"];
            $valueRankOld = $imageRow['rank'];

            echo "New value is $valueRankNew <br>";
            echo "Old value is $valueRankOld <br>";
            if ($valueRankNew > $valueRankOld) {
                echo "Hight <br>";

                $sql = "SELECT * FROM `photos` ORDER BY `rank` ASC";
                $changeResult = mysqli_query($conn, $sql);
                while ($changeRow = mysqli_fetch_assoc($changeResult)) {
                    $actualRow = $changeRow['rank'];
                    $actualRowPositon = $changeRow['id'];
                    if ($actualRow <= $valueRankNew && $actualRow > $valueRankOld) {
                        echo "Old Row was - " . $actualRow . "<br>";
                        --$actualRow;
                        echo "New Row is - " . $actualRow . "<br>";

                        $sql = "UPDATE 'photos' SET 'rank' = '$actualRow' WHERE 'id' = '$actualRowPositon'";
                        if (mysqli_query($conn, $sql)) {
                            echo "Record set <br>";
                        } else {
                            echo "Error record <br>";
                        }
                    }
                }
            } else if ($valueRankNew < $valueRankOld) {
                echo "Low <br>";

                $sql = "SELECT * FROM `photos` ORDER BY `rank` ASC";
                $changeResult = mysqli_query($conn, $sql);
                while ($changeRow = mysqli_fetch_assoc($changeResult)) {
                    $actualRow = $changeRow['rank'];
                    $actualRowPositon = $changeRow['id'];
                    if ($actualRow >= $valueRankNew && $actualRow < $valueRankOld) {
                        echo "Old Row was - " . $actualRow . "<br>";
                        ++$actualRow;
                        echo "New Row is - " . $actualRow . "<br>";


                        $sql = "UPDATE 'photos' SET 'rank' = '$actualRow' WHERE 'id' = '$actualRowPositon'";
                        if (mysqli_query($conn, $sql)) {
                            echo "Record set <br>";
                        } else {
                            echo "Error record <br>";
                        }
                    }
                }
            } else {
                echo "The same <br>";
            }
        } else {
            echo "Empty <br>";
        }
    }
} else {
    "Error - zero rows 2";
}

mysqli_close($conn);

}
?>

0 个答案:

没有答案