Call to a member function query() - php

I have problem in php i'm using WAMP(php 5.3.13 and mysql 5.5.24). And my problem is when I am testing the register form:
include "config.php";
if(isset($_GET['confirm_registration'])){
User::ConfirmRegistration($_GET['confirm_registration']);
}
if(isset($_POST['confirm_registration'])){
$username= $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];
if(!Validator::Email($email)){
echo "Invalid email address";
} else {
User::BeginRegistration($username,$email,$password);
}
}
?>
<div id="contentWrapper">
<div id="form">
<form id="signup" method="POST" action="">
<div class="signup_form"><strong>Sign up</strong></div>
<div class="signup_form">
<label>Username</label>
<input type="text" id="username" name="username" size="30">
</div>
<div class="signup_form">
<label>E-Mail</label>
<input type='text' id='email' name='email' size="30">
</div>
<div class="signup_form">
<label>Password</label>
<input type='password' id='password' name='password' size="30">
</div>
<div class="signup_form">
<label></label>
<input type='submit' name="confirm_registration" value='Submit'>
</div>
</form>
</div>
</html>
This is the error :Call to a member function query() on a non-object in C:\wamp\www\zavrsni_rad\classes\DataBase.class.php on line 11
and here is the DataBase class:
class DataBase
{
public static $connection;
public static function Connect(){
self::$connection = new mysqli("localhost","root","","zavrsni_rad");
self::$connection->set_charset("utf8");
}
public static function Execute($query){
self::$connection->query($query);
}
public static function GetString($query){
$r = self::$connection->query($query);
$res_arr = $r->fetch_row();
return $res_arr[0];
}
public static function GetRow($query){
$r = self::$connection->query($query);
return $r->fetch_row();
}
public static function GetTable($query){
$r = self::$connection->query($query);
$res = array();
while($rw = $r->fetch_row()){
$res[] = $rw;
}
return $res;
}
public static function Disconnect(){
self::$connection->close();
}
}

Clearly self::$connection is not yet instantiated when you call Execute() - you should run Connect() before.

Related

Undefined property: App\Controllers\Admin::$user

I want to Login whenever I click the submit button, I get all information on username and password to log in and can get in into the dashboard. can somebody help me?
but I got an error like this
Undefined property: App\Controllers\Admin::$user
this is my code:
public function __construct()
{
$this->user = new UserModel();
$this->sponsor = new SponsorModel();
$this->auditorium = new auditoriumModel();
}
public function index()
{
$user = $this->user->findAll();
$audVid = $this->auditorium->findAll();
$sponsorData = $this->sponsor->findAll();
$data = [
'user' => $user,
'sponsorData' => $sponsorData,
'audvid' => $audVid
];
// dd($sponsorData);
echo view('templates/header');
echo view('login', $data);
echo view('templates/footer');
}
public function doLogin()
{
$email
= $this->request->getVar('email');
$password =
$this->request->getVar('password');
$userData = $this->user->where('email', $email)
->where('password', $password)
->findAll();
if ($userData == null) {
return redirect()->to('/admin');
} else {
$_SESSION['logonUser'] = 'aktif';
return redirect()->to('/adminDashboard', $userData);
}
}
}
this is my login view
<form id="login-form" class="form" action="/admin/doLogin" method="post">
<h3 class="text-center text-info">Login Admin</h3>
<div class="form-group">
<label for="username" class="text-info">Username:</label><br>
<input type="text" name="username" id="username" class="form-control">
</div>
<div class="form-group">
<label for="password" class="text-info">Password:</label><br>
<input type="text" name="password" id="password" class="form-control">
</div>
<div class="form-group">
<labezl for="remember-me" class="text-info"><span>Remember me</span> <span><input id="remember-me" name="remember-me" type="checkbox"></span></labezl><br>
<input type="submit" class="btn btn-info btn-md" value="submit" href="/adminDashboard">
</div>
<div id="register-link" class="text-right">
Register here
</div>
</form>
can somebody help me to solve my code?
thanks
$userData = $this->user->where('email', $email)
->where('password', $password)
->findAll();
findAll() will get you an array of rows that applies to your criteria. In your case will try to find all users where email and password are correct, but it will still return you an array of them, not just one. You need to be using find() instead.

Why program show error with : array_key_exists()?

I want to validate form register and I created class UserValidator and I try to check if the username field exists. In my opinion everything is correct, but show me 2 errors
Warning: array_key_exists() expects parameter 2 to be array, null given in C:\xampp\htdocs\class\index.php on line 22
Notice: username is not present in data in C:\xampp\htdocs\class\index.php on line 24.
<?php
if(isset($_POST['submit']))
{
$validation = new UserValidator($_POST);
$errors =$validation->validateForm();
}
<?php
class UserValidator
{
private $data;
private $errors =[];
private static $fields =['username', 'email'];
public function _construct($post_data)
{
$this->data = $post_data;
}
public function validateForm()
{
foreach(self::$fields as $field)
{
if(!array_key_exists($field, $this->data))
{
trigger_error("$field is not present in data");
return;
}
}
$this->validateUsername();
return $this->errors;
}
private function validateUsername()
{
$val = trim($this->data['username']);
if(empty($val))
$this->addError('username', 'username cannot be empty');
else
{
if(!preg_match('/^[a-zA-Z-0-9]{6,12}$/', $val))
{
$this->addError('username', 'username must me 6-12 characters');
}
}
}
private function addError($key, $val)
{
$this->errors[$key] = $val;
}
}
?>
<div id="register">
<h1>REJESTRACJA</h1>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<div class="row">
<div class="col-25">
<label for="loginRe">Login</label>
</div>
<div class="col-75">
<input type="text" name="username" placeholder="Login...">
<div class="error">
<?php echo $errors['username'] ?? '' ?>
</div>
</div>
</div>
<div class="row">
<div class="col-25">
<label for="email">Email</label>
</div>
<div class="col-75">
<input type="text" name="email" placeholder="Email...">
</div>
</div>
<div class="button">
<input type="submit" name="submit" value="Utwórz konto">
</div>
<div class="displayCenter">
<div class="display">
</div>
</div>
</form>
</div>
$this->data is NULL, it is not filled by constructor, the constructor declaration has a typo, missing one _
public function __construct($post_data)
{
$this->data = $post_data;
}
Constructor declaration has a typo, please rectify

how to fetch results from select query with class

I am trying to learn select query with class.
Below is index.php, which have a form to display results and I in PHP I am calling class operations, which is in operations.php
<?php
session_start();
include('operations.php');
$userprofileobj = new operations();
$userprofileobj->showuserprofile($_SESSION['user_email']);
$row = $userprofileobj->fetch_assoc();
?>
<form class="form-horizontal" method="post" action="#" name="number_validate" id="number_validate" >
<div class="control-group">
<label class="control-label">Admin Name</label>
<div class="controls">
<input type="text" name="min" id="min" value="<?php echo $row['user_name']; ?>" required />
</div>
</div>
<div class="control-group">
<label class="control-label">User Email</label>
<div class="controls">
<input type="text" name="max" id="max" value="<?php echo $row['user_email']; ?>" required />
</div>
<div class="form-actions">
<input type="submit" value="Update" class="btn btn-success">
</div>
</form>
operations.php is:
class operations{
public $user_email;
public $error;
public $con;
public function __construct()
{
$this->con = new mysqli("localhost","root","","admin_with_oops");
}
public function showuserprofile($user_email)
{
$result = $this->con->query("select * from user_table where user_email='$user_email'");
if($result->num_rows > 0)
{
return $result;
}
else
{
$this->error = "User not exist";
}
}
}
Am I on right way? If yes teh what is the problem on above code?
Error is: Fatal error: Call to undefined method operations::fetch_assoc() in F:\xampp\htdocs\admin_with_oops\HTML\index.php on line 16
$userprofileobj is an instance of the operations class. It doesn't have fetch_assoc() method. You should replace
$userprofileobj->showuserprofile($_SESSION['user_email']);
$row = $userprofileobj->fetch_assoc();
with
$row = $userprofileobj->showuserprofile($_SESSION['user_email'])->fetch_assoc();
Or you could assign the result to a variable and then call the fetch_assoc() function on it:
$result = $userprofileobj->showuserprofile($_SESSION['user_email']);
$row = $result->fetch_assoc();

How to send $this->uname & $this->email to controller (controller/ctrl_crud.php)

How to send $this->uname & $this->email to controller data to controller/ctrl_crud.php
create.php
<?php
include 'FullScreenLayout.php';
class index {
public $uname, $email;
public function get_data() {
if ( isset($_POST) ) {
$this->uname = $_POST['form_uname'];
$this->email = $_POST['form_email'];
}
}
public function createForm(){
$createHTML = <<<EOT
<form method="POST">
<div class="main_form">
<div class="md-form">
<input type="text" id="form_uname" name="form_uname" class="form-control" placeholder="User Name">
</div>
<div class="md-form">
<input type="text" id="form_email" name="form_email" class="form-control" placeholder="Email Address">
</div>
<button type="submit" class="btn btn-submit">Submit</button>
</div>
</form>
EOT;
return $createHTML;
}
}
$ObjFullScreenLayout = new FullScreenLayout();
$ObjIndex = new index();
$ObjIndex->get_data();
echo $ObjFullScreenLayout->header();
echo $ObjIndex->createForm();
echo $ObjFullScreenLayout->footer();
?>
controller/ctrl_crud.php
<?php
class ctrl_crud {
}
?>
Send those data to the constructor
include 'controller/ctrl_crud.php';
public function get_data() {
if ( isset($_POST) ) {
$this->uname = $_POST['form_uname'];
$this->email = $_POST['form_email'];
new ctrl_crud($this->uname, $this->email);
}
}
controller/ctrl_crud.php
class ctrl_crud {
function __construct($uname, $email){
// use uname and email here
}
}
Just add an action to your form that points to the page you want to send the data like so:
<form action="controller/ctrl_crud.php" method="post">

Undefined property error message in PHP OOP

I'm working on a custom CMS using PHP OOP and this is actually my first project ever which is made with object oriented programming so I don't have that much of experience with it. Basically I have a class called Site.class.php which retrieves data from one of the tables in MySQL database and goes like this:
<?php
class Site
{
public $id,$site_name,$site_title,$site_url,$site_tags,$site_desc;
public function __construct()
{
$this->db = new Connection();
$this->db = $this->db->dbConnect();
}
public function getSite($name)
{
if(!empty($name))
{
$site = $this->db->prepare("select * from admins where site_name = ?");
$site->bindParam(1,$name);
$site->execute();
while($row = $site->fetch())
{
$this->id = $row['id'];
$this->site_name = $row['site_name'];
$this->site_title = $row['site_title'];
$this->site_url = $row['site_url'];
$this->site_tags = $row['site_tags'];
$this->site_desc = $row['site_desc'];
}
}
else
{
header("Location: maint/php/includes/errors/005.php");
exit();
}
}
public function getID()
{
return $this->id;
}
public function getSiteName()
{
return $this->site_name;
}
public function getSiteTitle()
{
return $this->site_title;
}
public function getSiteUrl()
{
return $this->site_url;
}
public function getSiteTags()
{
return $this->site_tags;
}
public function getSiteDesc()
{
return $this->site_desc;
}
}
?>
I have included this file at another file which is called settings.php and called it in this way:
$siteSet = new Site();
$siteSet->getSite("Daygostar");
Then I tried echoing out the variables like this:
<div class="box-body">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="usr">Site Name:</label>
<input type="text" class="form-control" id="usr" disabled='disabled' value="<?php echo $siteSet->getSiteName; ?>">
</div>
<div class="form-group">
<label for="usr">User URL:</label>
<input type="text" class="form-control" id="usr" disabled='disabled' value="<?php echo $siteSet->getSiteUrl; ?>">
</div>
</div>
</div>
</div>
But the problem is that whenever I call this file ,I receive this error message:
Undefined property: Site::$getSiteName
Undefined property: Site::$getSiteUrl
I don't know what's really going wrong because I have coded everything correctly! So if you know how to solve this question please let me know, I really appreciate that.. Thanks in advance.
Those are both methods. You need to add the () to the end of them to invoke the method.
<div class="box-body">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="usr">Site Name:</label>
<input type="text" class="form-control" id="usr" disabled='disabled' value="<?php echo $siteSet->getSiteName(); ?>">
</div>
<div class="form-group">
<label for="usr">User URL:</label>
<input type="text" class="form-control" id="usr" disabled='disabled' value="<?php echo $siteSet->getSiteUrl(); ?>">
</div>
</div>
</div>
</div>

Categories