I want to create a query that Inserts user data to a database. The connection works fine but for some reason the functions from the object don’t return their values inside the sql_query and instead the value () is stored inside the columns of the database. The code used is the following:
function addAccountToDB($conn, $user) {
//echo user->getUsername();
$sql_query = "INSERT INTO users(username, email, address, password, telephone) VALUES ('$user->getUsername()', '$user->getEmail()', '$user->getAddress()', '$user->getPassword()','$user->getTelephone()')";
if(mysqli_query($conn, $sql_query)) {
//logIn($conn, $user, false);
return true;
}
echo "query error " . mysqli_error($conn);
return false;
}
And I get the following error:
Warning: Undefined property: User::$getUsername in D:Vs CodeComposting-Projectphpfunctions.php on line 45
Warning: Undefined property: User::$getEmail in D:Vs CodeComposting-Projectphpfunctions.php on line 45
Warning: Undefined property: User::$getAddress in D:Vs CodeComposting-Projectphpfunctions.php on line 45
Warning: Undefined property: User::$getPassword in D:Vs CodeComposting-Projectphpfunctions.php on line 45
Warning: Undefined property: User::$getTelephone in D:Vs CodeComposting-Projectphpfunctions.php on line 45
The echo right before the query works fine and the line 45 that the error is refering to is the $sql_query = "INSERT INTO users(username, email, address, password, telephone) VALUES ('$user->getUsername()', '$user->getEmail()', '$user->getAddress()', '$user->getPassword()','$user->getTelephone()')"; line.
The following code is the functions.php and classes.php files that are used:
functions.php:
<?php
session_start();
include("../php/connect.php");
include("../php/classes.php");
function addAccountToDB($conn, $user) {
$sql_query = "INSERT INTO users(username, email, address, password, telephone) VALUES ('$user->getUsername()', '$user->getEmail()', '$user->getAddress()', '$user->getPassword()','$user->getTelephone()')";
if(mysqli_query($conn, $sql_query)) {
//logIn($conn, $user, false);
return true;
}
echo "query error " . mysqli_error($conn);
return false;
}
?>
classes.php;
<?php
class User {
private $username;
private $email;
private $address;
private $password;
private $telephone;
public function __construct($username, $email, $address, $password, $telephone) {
$this->username = $username;
$this->email = $email;
$this->address = $address;
$this->password = $password;
$this->telephone = $telephone;
}
public function getUsername() {
return $this->username;
}
public function getEmail() {
return $this->email;
}
public function getAddress() {
return $this->address;
}
public function getPassword() {
return $this->password;
}
public function getTelephone() {
return $this->telephone;
}
}
?>