Monday, September 24, 2018

php - Error in updating database table and fetching appropriate row using mysqli_fetch





I am trying to send login information to this php script and update certain fields in the database if the username and password exists. But I'm getting an error with mysqli_fetch_array. There are a lot of similar questions here already and I know that the problem is with "$result"(when I print out the value of result it is 1), probably because of the query failure ! But when I execute just the query in mysql it updates the fields without any problem so I'm really clueless. Would really appreciate any help !
Thank you.



This is the php code :




if(isset($_POST) == true && empty($_POST) == false){
$latitude = $_POST['Latitude'];
$longitude = $_POST['Longitude'];
$timestamp = $_POST['Timestamp'];
$Password=$_POST['Password'];
$name=$_POST['userName'];


$dbname = "android_db";

$servername = "localhost";
$username = "root";
$password = "1234";

//connect to the database
$conn = mysqli_connect($servername, $username, $password, $dbname);
if(!$conn)
{
die("Connection Failed" . mysqli_error($conn));
}

$sql = "Update LocationTable SET Latitude='$latitude', Longitude='$longitude' WHERE name='$name' AND Password='$Password';";

$result = mysqli_query($conn, $sql);
echo"$result";

$response = array();

while($row = mysqli_fetch_array($result)){
$response = array("id"=>$row[0],"name"=>$row[1],"password"=>$row[2],"email"=>$row[3]);
}


echo json_encode(array("user_data"=>$response));
}
?>


This is the output:



1


Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in
/opt/lampp/htdocs/friendFinder/login.php on line
28

{"user_data":[]}


These are the fields in the database table
enter image description here


Answer




if(isset($_POST) == true && empty($_POST) == false){
$latitude = $_POST['Latitude'];
$longitude = $_POST['Longitude'];
$timestamp = $_POST['Timestamp'];
$Password=$_POST['Password'];
$name=$_POST['userName'];


$dbname = "android_db";

$servername = "localhost";
$username = "root";
$password = "1234";

//connect to the database
$conn = mysqli_connect($servername, $username, $password, $dbname);
if(!$conn)
{
die("Connection Failed" . mysqli_error($conn));
}

$sql = "Update LocationTable SET Latitude='$latitude', Longitude='$longitude' WHERE name='$name' AND Password='$Password';";

$result1 = mysqli_query($conn, $sql); // update action

$result = mysqli_query($conn, "select *from LocationTable"); // fetch action


$response = array();

while($row = mysqli_fetch_array($result)){

$response = array("id"=>$row[0],"name"=>$row[1],"password"=>$row[2],"email"=>$row[3]);
}

echo json_encode(array("user_data"=>$response));
}
?>

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...