php - Unable to UPDATE mysql table from a function, although successful query response -


i using phpmyadmin database. when button on html page clicked, following executed:

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 such:

<?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 below:

<?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 txt file, nothing updated in database.

however, if getfromdb.php this:

<?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      ...      // rest previous code shown above     if (isset($_post["action"]) && !empty($_post["action"])) {         $action = $_post["action"];          switch($action) {             // code         }     } ?> 

and go page directly (i.e. localhost/getfromdb.php), txt file has 'record updated successfully' , database updated!

it important note, db_functions.php contains other functions retrieve info db , work expected. example, 1 such function in 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));      // whatever want retrieved data } 

i have tried update work, haven't been able to. missing?

from comment:

if add line $affected_rows = mysqli_affected_rows($dbc); after query , record $affected_rows in function_result.txt, how many rows affected? can record value of $query in function_result.txt both versions of getfromdb.php , compare differences?

the answer found - in 1 instance database variable named stopid in other, stop_id. op comparing queries, found difference , resolved issue.


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -