简体   繁体   English

尽管查询响应成功,但是无法从函数更新mysql表

[英]Unable to UPDATE mysql table from a function, although successful query response

I am using PHPMyAdmin for my database. 我正在为数据库使用PHPMyAdmin。 When a button on my html page is clicked, the following is executed: 单击我的html页面上的按钮时,将执行以下操作:

var stopID = 10; // Sample
var stopPOV = "129.29,158.58"; // Sample

$.ajax({
    url: "getFromDB.php",
    type: "post",
    dataType: 'json',
    data: {
        action: "setStopPOV",
        stopID : stopID,
        stopPOV : stopPOV
    },
    success: function(data) {
        //alert(data);
    }
});

My getFromDB.php is such: 我的getFromDB.php是这样的:

<?php
    require_once('connect.php');
    require_once('db_functions.php');

    if (isset($_POST["action"]) && !empty($_POST["action"])) {
        $action = $_POST["action"];

        switch ($action) {
            case "setStopPOV":
                $stopID = $_POST['stopID'];
                $stopPOV = $_POST['stopPOV'];

                setStopPOV($stopID, $stopPOV);
                break;
        }
    }
?>

Finally, setStopPOV(...) function inside db_functions.php is as below: 最后,db_functions.php中的setStopPOV(...)函数如下:

<?php
    function setStopPOV($stopID, $stopPOV) {
        global $dbc;        // Set in connect.php

        $query  = "UPDATE stop ";
        $query .= "SET STOP_POV = '{$stopPOV}' ";
        $query .= "WHERE STOP_ID = '{$stopID}'";

        $result = @mysqli_query($dbc, $query) or die("Error updating record: " . mysqli_error($dbc));

        if ($result) {
            file_put_contents('function_result.txt', "Record updated successfully" . PHP_EOL, FILE_APPEND);
        } else {
            file_put_contents('function_result.txt', "Error updating record" . PHP_EOL, FILE_APPEND);
        }
    }
?>

This outputs 'Record updated successfully' info the txt file, but nothing is updated in the database. 这将输出“记录成功更新”信息到txt文件,但是数据库中没有任何更新。

However, if getFromDB.php is like this: 但是,如果getFromDB.php是这样的:

<?php
    require_once('connect.php');
    require_once('db_functions.php');

    // NEW CODE ADDED START
    $id = 10;
    $pov = "129.29,158.58";

    $qry  = "UPDATE stop ";
    $qry .= "SET STOP_POV = '{$pov}' ";
    $qry .= "WHERE STOPID = '{$id}'";

    $result = @mysqli_query($dbc, $qry) or die("Error updating record: " . mysqli_error($dbc));

    if ($result) {
        file_put_contents('function_result.txt', "Record updated successfully" . PHP_EOL, FILE_APPEND);
    } else {
        file_put_contents('function_result.txt', "Error updating record" . PHP_EOL, FILE_APPEND);
    }
    // NEW CODE ADDED FINISH

    ...

    // The rest is previous code as shown above
    if (isset($_POST["action"]) && !empty($_POST["action"])) {
        $action = $_POST["action"];

        switch($action) {
            // Code
        }
    }
?>

And I go to the page directly (ie localhost/getFromDB.php), The txt file has 'Record updated successfully' AND the database is updated! 然后我直接转到页面(即localhost / getFromDB.php),该txt文件具有“记录已成功更新”并且数据库已更新!

It is important to note, that my db_functions.php contains other functions which retrieve info from the DB and they work as expected. 重要的是要注意,我的db_functions.php包含其他功能,这些功能可以从数据库中检索信息,并且可以按预期运行。 For example, this is one such function in the db_functions.php 例如,这是db_functions.php中的此类功能之一。

function getRouteList() {
    global $dbc;

    $query  = "SELECT RTE_NAME, TRAV_DIR ";
    $query .= "FROM route";

    $result = @mysqli_query($dbc, $query) or die("Error in Selecting " . mysqli_error($dbc));

    // Do whatever I want with the retrieved data
}

I have tried everything to get the update to work, but I haven't been able to. 我已经尽一切努力使更新生效,但是我却没有能力。 What am I missing? 我想念什么?

From my comment: 从我的评论:

If you add the line $affected_rows = mysqli_affected_rows($dbc); 如果添加行$affected_rows = mysqli_affected_rows($dbc); after the query and record $affected_rows in function_result.txt , how many rows does it say were affected? 查询后并在function_result.txt记录$affected_rows ,它说有多少行受到影响? Can you also record the value of $query in function_result.txt for both versions of getFromDB.php and compare the differences? 您是否还可以将两个版本的getFromDB.php的$query值记录在function_result.txt ,并比较差异?

The answer was found - in one instance a database variable was named STOPID in the other, it was STOP_ID . 找到了答案-在一个实例中,一个数据库变量名为STOPID ,另一个实例为STOP_ID By OP comparing the queries, they found the difference and resolved the issue. 通过OP比较查询,他们发现了差异并解决了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM