我的SQL Update查询不会更新指定的记录

时间:2019-05-09 09:33:00

标签: php sql mysqli sql-update

我正在建立一个系统,您可以在其中分配客人到某些房间。要取消分配访客,必须在两个不同的表中执行两个UPDATE查询,但实际上只有其中一个正在执行。一个不起作用,不会发出错误消息,但似乎什么也没做。

我已经打印出我在查询中使用的所有变量以检查错误,但是我没有在错误的数据中找到任何数据。我还使用phpMyAdmin检查了查询中是否有错误,但在那里正确执行了。我在另一页上使用了相同类型的方法,对其他方法进行了更新,并且行得通。有人可以看看我的代码并告诉我我做错了什么吗?

谢谢!

if(isset($_GET['view'])) {
    $viewid = $_GET['view'];
    $sql = "SELECT * FROM renterinfo LEFT JOIN apartments ON apartments.renterid = renterinfo.renterid WHERE renterinfo.renterid = $viewid";
    $xresult = mysqli_query($conn, $sql);
    $xrow = mysqli_fetch_array($xresult);
}

$freeroomquery = "SELECT * FROM apartments WHERE status = 1 ORDER BY apartmentno ASC";
$freerooms = mysqli_query($conn, $freeroomquery);

$rentername = $xrow['name'];
$renteremail = $xrow['email'];
$roomno = $xrow['apartmentno'];
$enddate = $xrow['leaveroom'];

if(isset($_POST['submit'])){    

        //------------------------------------

        $name = $_POST['newNaam'];
        $email = $_POST['newEmail'];
        $room = $_POST['newKamer'];
        $leaveroom = $_POST['newEinde'];

        $insertSql = "UPDATE renterinfo SET name = '$name', email = '$email', leaveroom = '$leaveroom'";
        $updateRoom = "UPDATE apartments SET renterid = NULL WHERE apartmentno = '$roomno'";

第二个UPDATE查询不会更新renterid。第一个UPDATE查询确实起作用。





        if ($conn->query($insertSql) === TRUE) {
            $success = "Huurder <strong>&#39;" . $name . " - CobbenCampus&#39;</strong> is succesvol toegevoegd.";
        } else {
            $error = "Er is een fout opgetreden bij het toevoegen van appartement <strong>&#39;" . $apartmentno . " - CobbenCampus&#39;</strong>. Zijn alle velden ingevuld?<br><strong>Foutmelding:</strong> " . $sql . "<br>" . $conn->error;
        }

        if ($conn->query($updateRoom) === TRUE) {
            echo "Succeeded.";
        } else {
            echo "Query unsuccessfull.";
        }

        $conn->close();
}

HTML代码:


<form action="viewrenter.php" method="POST" enctype="multipart/form-data">

                <label id="first">Voor- en achternaam huurder:</label><br>
                <input type="text" name="newNaam" value="<?php echo $rentername ?>"><br><br>

                <hr class="line-black">

                <label id="first">E-mailadres van huurder:</label><br>
                <input type="text" name="newEmail" value="<?php echo $renteremail ?>"><br><br>

                <hr class="line-black">     

                <label id="first">Kamernummer:</label><br/>
                <p>Een kamer toewijzen aan een huurder, zal de kamer automatisch op bezet zetten.</p>
                <select name="newKamer">
                    <option value="<?php echo $roomno ?>"><?php echo $roomno ?></option>
                    <option value="0">Geen kamer</option>
                    <?php

                        while($rooms = mysqli_fetch_array($freerooms)) {
                            echo "<option value='" . $rooms['apartmentno'] . "'>" . $rooms['apartmentno'] . "</option>";
                        }

                    ?>

                </select><br><br>

                <hr class="line-black">

                <label id="first">Afloopdatum huurdersovereenkomst:</label><br>
                <input type="text" name="newEinde" placeholder="Als: 25-04-2019" value="<?php echo $enddate ?>"><br><br>

                <button class="spaced btn btn-primary btn-wide" type="submit" name="submit">Voeg huurder toe</button>
                <hr class="bigspacer">
            </form>

1 个答案:

答案 0 :(得分:-1)

UPDATE apartments SET `renterid` = NULL WHERE `apartmentno` = `$roomno`

这样写查询。我认为它将起作用。