ERROR in register.php - php

Please help:
Fatal error: Call to a member function prepare() on a non-object in C:\wamp\www\ooploginreg\functions.php on line 14 Call Stack # Time Memory Function Location 1 0.0010 252552 {main}( ) ..\register.php:0 2 0.0020 265888 LoginRegistration->registerUser( ) ..\register.php:50
Codeline in functions.php:
<?php
require "config.php";
class LoginRegistration{
function _construct(){
$database = new DatabaseConnection();
}
public function registerUser($username, $password, $name, $email, $website){
global $pdo;
$query = $pdo->prepare("SELECT id FROM users WHERE username = ? AND email = ?");
$query->execute(array($username, $email));
$num = $query->rowCount;
if($num == 0){
$query = $pdo->prepare("INSERT INTO users (username, password, name, email, website)VALUES (?, ?, ?, ? ,?)");
$query->execute(array($username, $password, $name, $email, $website));
return true;
}else{
return print "<span style='color:=#e53d37'>Error...username/email already used.</span>";
}
}
}
?>

It looks like you are calling $pdo before it is initialized.

You called database connection in constructor. But your var "pdo" already have value. Your PDO make some data before this class runs.
Just Use
$this->database = new DatabaseConnection();

You create the variable $database but never used it:
$database = new DatabaseConnection();
Problems
You never used the $database variable
It is bad practice to use global
It is bad practice to have your class depend on database object
Connection should be passed to constructor
Solution
Your class should be like this:
class LoginRegistration
{
private $pdo;
function __construct($database)
{
$this->pdo = $database;
}
public function registerUser($username, $password, $name, $email, $website)
{
$query = $this->pdo->prepare("SELECT id FROM users WHERE username = ? AND email = ?");
$query->execute(array($username, $email));
$num = $query->rowCount;
if ($num == 0) {
$query = $this->pdo->prepare("INSERT INTO users (username, password, name, email, website)VALUES (?, ?, ?, ? ,?)");
$query->execute(array($username, $password, $name, $email, $website));
return true;
} else {
return print "<span style='color:=#e53d37'>Error...username/email already used.</span>";
}
}
}
usage:
$database = new DatabaseConnection();
$login = new LoginRegistration($database);

Related

How to use a created database class to query data from MySQL database

I have difficulties to select data, and even to put data in my database when I use classes. Here are two examples, one works (without classes) and the other does not (with classes).
Let's start with the one that works.
In the file name index1.php we have these code lines.
$con = new PDO("mysql:host=localhost;dbname=database","user","pass");
echo "Connection success ";
$sql = "SELECT email FROM users WHERE email=:email";
$email= "mail#gmail.com";
$stmt = $con->prepare($sql);
//var_dump($stmt);
$stmt->bindParam(":email", $email, PDO::PARAM_STR);
$stmt->execute();
$count = $stmt->rowCount();
echo $count;
This returns $count = 2, which is true. I have 2 users in the database with mail#gmail.com as email.
Now to the case that does not work.
This is the file Database.php
<?php
class Database{
protected $pdo;
protected static $instance;
protected function __construct(){
$this->pdo = new PDO("mysql:host".DB_HOST.";dbname=".DB_NAME, DB_USER, DB_PASS);
echo "success";
}
public static function instance(){
if(self::$instance===null){
self::$instance = new self;
}
return self::$instance;
}
public function __call($method, $args){
return call_user_func_array(array($this->pdo, $method), $args);
}
}
?>
And I use it in the file index2.php
define("DB_HOST", "localhost");
define("DB_USER", "user");
define("DB_PASS", "pass");
define("DB_NAME", "database");
include_once "Database.php";
$pdo = Database::instance();
$sql = "SELECT email FROM users WHERE email=:email";
$email = "mail#gmail.com";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(":email", $email, PDO::PARAM_STR);
$stmt->execute();
$count = $stmt->rowCount();
echo $count;
This $count returns 0.And I get no error. I used var_dump() and $stmt->debugDumpParams() but could not found where the problem is). Am I doing something wrong ?

Need help to get rid of in this PHP code that restricts me from having the same entry in a category in a database. (please help)

I need to get rid of the part that restricts me from adding the same value in a field from previous entries. I need to get rid of the part that gives me an error message if the entry matches a value from the database. Can someone please help me?
<?php
class DbOperation
{
private $conn;
//Constructor
function __construct()
{
require_once dirname(__FILE__) . '/Constants.php';
require_once dirname(__FILE__) . '/DbConnect.php';
// opening db connection
$db = new DbConnect();
$this->conn = $db->connect();
}
//Function to create a new user
public function createUser($RC, $Date, $Value)
{
if (!$this->isUserExist($RC, $Date, $Value)) {
$password = md5($pass);
$stmt = $this->conn->prepare("INSERT INTO MyInventory (username, password, email, name, phone) VALUES (?, ?, ?, ?, ?)");
$stmt->bind_param("sssss", $username, $password, $email, $name, $phone);
if ($stmt->execute()) {
return ENTRY_CREATED;
} else {
return ENTRY_ALREADY_EXIST;
}
} else {
return ENTRY_ERROR;
}
}
private function isUserExist($username, $email, $phone)
{
$stmt = $this->conn->prepare("SELECT id FROM users WHERE username = ? OR email = ? OR phone = ?");
$stmt->bind_param("sss", $username, $email, $phone);
$stmt->execute();
$stmt->store_result();
return $stmt->num_rows > 0;
}
as you can see in the photo below, every single entry in the database is different. I need to get rid of this and make it so that it is possible for 2 "RC" values to be the same.
When createUser is called, it first checks if the user already exists (if a record exists in the database with the same RC) by calling isUserExist. If you want to allow duplicate RC values, simply remove the if/else statement and only keep the code inside of the if block.

how to fix easily this error Fatal error: Call to a member function execute() on boolean in /Applications/XAMPP/xamppfiles/htdocs

I developed php simple page to register users and check if the user exists or not but it is not working and displays the fallowing error :
Fatal error: Call to a member function execute() on boolean in /Applications/XAMPP/xamppfiles/htdocs/one/include/DbOperation.php on line 31
and php code here please help us for this issue
<?php
class DbOperation
{
private $conn;
enter code here
//Constructor
function __construct()
{
require_once('Constants.php');
require_once('DbConnect.php');
// opening db connection
$db = new DbConnect();
$this->conn = $db->connect();
}
//Function to create a new user
public function createUser($username, $pass, $email, $name, $phone)
{
if (!$this->isUserExist($username, $email, $phone)) {
$password = md5($pass);
$stmt = $this->conn->prepare("INSERT INTO users (username, password, email, name, phone) VALUES (?, ?, ?, ?, ?)");
$stmt->bind_param("sssss", $username, $password, $email, $name, $phone);
if ($stmt->execute()) {
return USER_CREATED;
} else {
return USER_NOT_CREATED;
}
} else {
return USER_ALREADY_EXIST;
}
}
private function isUserExist($username, $email, $phone)
{
$stmt = $this->conn->prepare("SELECT id FROM users WHERE username = ? OR email = ? OR phone = ?");
//if($query = $this->db->conn->prepare($sql)){
$stmt->bind_param(array("sss", $username, $email, $phone));
$stmt->execute();
$stmt->store_result();
$stmt->fetch();
$stmt->close();
return $stmt->num_rows > 0;
}
}
?>
In your isUserExist() function it looks like your bind_param has an array which shouldn't be there:
$stmt->bind_param(array("sss", $username, $email, $phone));
should be:
$stmt->bind_param("sss", $username, $email, $phone);
This is most likely why mysqli->bind_param is returning FALSE
change your isUserExist as below:
private function isUserExist($username, $email, $phone)
{
$stmt = $this->conn->prepare("SELECT id FROM users WHERE username = ? OR email = ? OR phone = ?");
//if($query = $this->db->conn->prepare($sql)){
$stmt->bind_param("sss", $username, $email, $phone); // change here remove array
$stmt->execute();
$stmt->store_result();
$stmt->fetch();
//$stmt->close(); // change this comment or remove this
return $stmt->num_rows > 0;
}
use this in isUserExist() function
$stmt->bind_param("sss", $username, $email, $phone);

Call to member function query() on Null [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 7 years ago.
So just trying to make a simple register/login form which i have done before, but not for hell can i figure out why this is happening. my code looks like the below.
Ive commented out the error above the line that it says its occurring.
<?php
class connection{
private $hostname = "localhost";
private $username = "root";
private $password = "";
private $database = "test";
private $conn;
public function __construct(){
$conn = new mysqli($this->hostname, $this->username, $this->password, $this->database)or die("MySQL Connection Error");
}
public function getConn(){
return $this->conn;
}
}
class queries{
private $conn;
public function __construct($conn){
$this->conn = $conn;
}
public function checkUser($username, $email){
$query = "SELECT * FROM users WHERE username = '$username' OR email = '$email'";
//Call to a member function query() on null in C:\xampp\htdocs\i\functions.php on line 28
$result = $this->conn->query("$query");
return $result;
}
public function insertUser($activated, $activation_code, $firstname, $lastname, $username, $password, $email){
$query = "INSERT INTO users (activated, activation_code, firstname, lastname, username, password, email)
VALUES ('$activated', '$activation_code', '$firstname', '$lastname', '$username', '$password', '$email')";
$result = $this->conn->query("$query");
}
}
Seems Simple enough right.. now heres my php code on the page thats being used (register.php).
if (isset($_POST['register'])) {
include 'functions.php';
$connection = new connection();
$query = new queries($connection->getConn());
$firstname = mysql_real_escape_string($_POST['firstname']);
$lastname = mysql_real_escape_string($_POST['lastname']);
$username = mysql_real_escape_string($_POST['username']);
$email = mysql_real_escape_string($_POST['email']);
$password = sha1($_POST['password']);
$user = $query->checkUser($username, $email);
if ($user->num_rows > 0) {
echo "User Exists";
}else{
echo "User Does not Exist";
}
}
Your code seems correct, except in the constructor of your connection class, you've forgot to use the $this, change it to something like the following:
public function __construct() {
$this->conn = new mysqli($this->hostname, $this->username, $this->password, $this->database)or die("MySQL Connection Error");
}

Registration with PDO/PHP doesn't work

I've got a problem through build a registration system. I don't know where is the problem, because I'm a beginner in PHP OOP.
Here's my registration class:
<?php
/*
*=======================
* REGISTRATION CLASS !!!
* ======================
*/
include_once '/../core/registration.php';
include_once '/../core/db_connect.php';
class registration {
private $db;
function __construct() { /* Connecting with MySQL */
$this->db = new db_connect();
$this->db = $this->db->connect();
} /* End of connection */
function registration($username, $password1, $password2, $email, $date) {
if(!empty($username) && !empty($password1) && !empty($password2) && !empty($email)) {
$q = $this->db->prepare("INSERT INTO users(username, password, email, date, logged, admin) VALUES (?, ?, ?, ?, ?, ?)");
$q->bindParam(1, $username);
$q->bindParam(2, $password1);
$q->bindParam(3, $email);
$q->bindParam(4, $date);
$q->bindParam(5, '0');
$q->bindParam(6, '0');
$q->execute();
}
}
}
?>
Here is a file, that is running, when user clicks on 'register' button:
<?php
include_once '/../classes/registration.php';
if(isset($_POST['regUser'])) {
$username = $_POST['regUsername'];
$pass1 = $_POST['regPassword1'];
$pass2 = $_POST['regPassword2'];
$email = $_POST['regEmail'];
$date = date("Y-m-d H:i:s");
$obj = new registration();
$obj->registration($username, $password1, $password2, $email, $date);
}
?>
For safety, I'll put also a connection file:
<?php
class db_connect {
function connect() {
return new PDO("mysql:host=127.0.0.1;dbname=oop", "root", "");
}
}
?>
It's one of the most common "error" with PDO.
With bindParam, you can only pass variables, not values.
In your registration() method, replace:
$q->bindParam(5, '0');
$q->bindParam(6, '0');
By:
$q->bindValue(5, '0');
$q->bindValue(6, '0');
With bindValue you can pass values and variables.
You can read the PHP manual for more info (here and here).

Categories