douhuibo5635 2016-10-19 20:01
浏览 42
已采纳

通过ajax发送值,SQL中没有任何反应

I have built this ajax function where i want to update two fields in a database if the user decides to change them, i have this code where i'am printing hotelrooms (school assignment) with their current information, ID, etcetera:

 <?php
        include('php/connect.php');

        $chquery = "SELECT * FROM rooms";
        $chresult = mysqli_query($conn, $chquery);

        while($chrow = mysqli_fetch_array($chresult)){
            echo "<div class='edit_roomRow'>";
            echo "<h1> Rum " . $chrow['ID'] . "</h1>";
            echo "<form>";
            echo "Rumsnummer:<br> <input id='c-roomnumber' type='text' value = '" . $chrow['ID'] . "' placeholder='" . $chrow['ID'] . "'><br>";
            echo "Beskrivning:<br> <input id='c-description' type='text' value = '" . $chrow['Description'] . "'placeholder='" . $chrow['Description'] . "'>";
            echo "</form>";
            echo "<br>";
            echo "</div>";
            echo "<div class='btn btn-warning change-btn' data-value='". $chrow['ID'] . "'style='float: right; margin-top: 100px;'>Ändra</div>";
            echo "<hr/>";
        }

        mysqli_free_result($chresult);

        mysqli_close($conn);

    ?>

Here nothing is wrong and all values seem correct. Then here's the ajax where i send the info from this form:

<script type="text/javascript">
    $(".change-btn").click(function(){
        var id = $(this).attr("data-value");
        var description = $("#c-description").val();
        var roomnumber = $("#c-roomnumber").val();
        //Call to ajax
        $.ajax({
            method:"GET",
            url: "php/changepost.php",
            data:{ id: id, description: description, roomnumber: roomnumber },
            success: function(){
                $(this).val("");
                $("#c-description").val("");
                $("#c-roomnumber").val("");
                //Reload specific div with rooms, avoid full page reload
                $(".changerooms").load(location.href + " .changerooms");
            }
        })
    })
</script>

To make sure all the values are sent from the PHP (correct ID from the button, description & room number) i alerted them in the end of the ajax file, all values correct.

Then here i am trying to update the DB with the new values:

<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
include('connect.php');

if(isset($_GET['id'])){
    $changequery = "UPDATE rooms SET ID = $_GET['roomnumber'], Description = $_GET['description'] WHERE ID = $_GET['id']";
    //Perform $changequery
    mysqli_query($conn, $changequery);
}

mysqli_close($conn);

?>

I cant see anything that is off here, but i am kind of new with AJAX mostly, so if you see something that seems wrong, i would be glad with any help i could get.. :)

  • 写回答

1条回答 默认 最新

  • douxiangui5011 2016-10-19 20:19
    关注

    Your string syntax is wrong. If you don't use the curly syntax around variables, you mustn't put quotes around the array indexes. You also need quotes around string values in the SQL.

    $changequery = "UPDATE rooms SET ID = $_GET[roomnumber], Description = '$_GET[description]' WHERE ID = $_GET[id]";
    

    It would be better for you to learn to use prepared queries with mysqli_stmt_bind_param, to avoid SQL injection problems.

    $stmt = mysqli_prepare($conn, "UPDATE rooms SET ID = ?, Description = ? WHERE ID = ?");
    mysqli_stmt_bind_param($stmt, "isi", $_GET['roomnumber'], $_GET['description'], $_GET['id']);
    mysqli_stmt_execute($stmt);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)