Agustin Scalisi
Agustin Scalisi

Reputation: 551

Android Update MySQL table through PHP

I am working on an Android App. I need to update a register, I didn't have issues with entries. But when I try to update the table, Doesn't work

this my PHP

<?php
require_once 'include/userupdate.php';
$username = "";
$name = "";
$movil="";
$email = "";
$password = "";
$fnac = "";
$calle_numero_piso ="";
$nom_urba = "";
$cod_postal ="";
$localidad="";
$observaciones="";
/////////////////
if(isset($_POST['username'])){
$username = $_POST['username'];
}
if(isset($_POST['name'])){
$name = $_POST['name'];
}
if(isset($_POST['movil'])){
$movil = $_POST['movil'];
}
if(isset($_POST['email'])){
$email = $_POST['email'];
}
if(isset($_POST['password'])){
$password = $_POST['password'];
 } 
if(isset($_POST['fnac'])){
$fnac = $_POST['fnac'];
}
if(isset($_POST['calle_numero_piso'])){
$calle_numero_piso = $_POST['calle_numero_piso'];
}
if(isset($_POST['nom_urba'])){
$nom_urba = $_POST['nom_urba'];
}
if(isset($_POST['cod_postal'])){
$cod_postal = $_POST['cod_postal'];
}
if(isset($_POST['localidad'])){
$localidad = $_POST['localidad'];
}
if(isset($_POST['observaciones'])){
$observaciones = $_POST['observaciones'];
}
// Instance of a User class
$userObject = new User();
// update user
$json_registration = $userObject->updateRegisterUser($username, $name, $movil, $email, $password, $fnac, $calle_numero_piso, $nom_urba, $cod_postal, $localidad, observaciones );
  echo json_encode($json_registration);
 }
 ?>

And this my update.php

            <?php
        include_once 'db.php';
        class User{
         private $db;
         private $db_table = "users";
         public function __construct(){
        $this->db = new DbConnect();
        }
        mysqli_close($this->db->getDb());
        return false;
        }
        public function updateRegisterUser($username, $name, $movil, $email, $password, $fnac, $calle_numero_piso, $nom_urba, $cod_postal, $localidad, $observaciones ){
        $query = "UPDATE users SET name = '$name', movil = '$movil', email = '$email', password = '$password', fnac = '$fnac', calle_numero_piso = '$calle_numero_piso', nom_urba ='$nom_urba', cod_postal = '$cod_postal', localidad = '$localidad', observaciones = '$observaciones' WHERE username = $username;";
        $updated = mysqli_query($this->db->getDb(), $query);
        if($updated == 1){
        $json['success'] = 1;
        }else{
        $json['success'] = 0;
        }
        mysqli_close($this->db->getDb());
        return $json;
        }
        public function loginUsers($username, $password){
        $json = array();
        $canUserLogin = $this->isLoginExist($username, $password);
        if($canUserLogin){
        $json['success'] = 1;
        }else{
        $json['success'] = 0;
        }
        return $json;
        }
        }
        ?>

I believe I there is something wrong with both php, but I am not sure where.

Upvotes: 0

Views: 88

Answers (1)

Pavneet_Singh
Pavneet_Singh

Reputation: 37404

try this , as obvious your username is a string so it should be kept inside single quotes

$query = "UPDATE users SET name = '$name', movil = '$movil', email = '$email', password = '$password', fnac = '$fnac', calle_numero_piso = '$calle_numero_piso', nom_urba ='$nom_urba', cod_postal = '$cod_postal', localidad = '$localidad', observaciones = '$observaciones' WHERE username = '$username'";

One advise for best practice is, always make some primary key recommended as int autoincrement which will help to enforce uniqueness because in this case you can have two users with same name which will be a problem when your database grows.

Upvotes: 1

Related Questions