jeudi 25 janvier 2018

PHP update() MySQL database not working

I am working on a quiz that asks the user for demographic information at the end, but I am having some issue correctly populating the table with the information that I gather.

The following code is a part of my databaseAdapator.php file. The first, initUser(), sets up a user entry in my database and initializes everything to 0 - this is happening/I can see these values in MySQL.

udpateUser() is where I am having issues. The page that calls it (addUser.php) has all of the information that the function needs, but when I call it there is no change in the database.

Essentially, I want to execute the following command: update users set age=11, gender="Male", height_cm=2 where user_id=95;

        public function initUser($age, $gender, $height, $weight, $ip){
                //create new dummy user
                $stmt = $this->DB->prepare("Insert into users values(null, :age, :gender, :height, :weight, :ip)");
                $stmt->bindParam('age', $age);
                $stmt->bindParam('gender', $gender);
                $stmt->bindParam('height', $height);
                $stmt->bindParam('weight', $weight);
                $stmt->bindParam('ip', $ip);
                $stmt->execute();

                //select last insert ID
                $stmt = $this->DB->prepare("SELECT LAST_INSERT_ID() from users");
                $stmt->execute();
                $userId = $stmt->fetch();
                return $userId[0];
        }
        
        
public function updateUser($user, $age, $gender, $height, $weight){
                //edit the information for a user after demographics page
                $stmt = $this->DB->prepare("UPDATE users SET age=:age, gender=:gender, height_cm=:height, weight_kg=:weight WHERE user_id=:user");
                $stmt->bindParam('age', $age);
                $stmt->bindParam('gender', $gender);
                $stmt->bindParam('height', $height);
                $stmt->bindParam('weight', $weight);
                $stmt->bindParam('user', $user);
                $stmt->execute();

                return $user;
        }

Here is addUser.php, the page calling the function. I know this is horrendous practice (sorry!), I'm trying to get the database adaptor working correctly for the time being, and I have checked all the variables manually - they are all valid and non-null.

<?php
require_once("./databaseAdaptor.php");
$user = $_GET["user"];

$age = $_POST["Age"];
$gender = $_POST["Gender"];
$height_in = $_POST['in'];
$height_ft = $_POST['ft'];
$height = 2.54 * (($height_ft * 12) + $height_in);
$weight = $_POST['lbs'];


$userId = $myDatabaseFunctions->updateUser($user, $age, $gender, $height, $weight);
?>



Aucun commentaire:

Enregistrer un commentaire