Hello I have Undefined property: stdClass::$email . Can you help me ?
A PHP Error was encountered
Severity: Notice
Message: Undefined property: stdClass::$email
Filename: controllers/verifylogin.php
Line Number: 48
Backtrace:
File: D:\wamp\www\codeigniter\application\controllers\verifylogin.php
Line: 48 Function: _error_handler
File: D:\wamp\www\codeigniter\index.php Line: 292 Function:
require_once
This is VerifyLogin Controller
class VerifyLogin extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('user_model','',TRUE);
}
function index()
{
//This method will have the credentials validation
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database');
if($this->form_validation->run() == FALSE)
{
//Field validation failed. User redirected to login page
$this->load->view('login');
}
else
{
//Go to private area
// redirect('home', 'refresh');
}
}
public function check_database($password)
{
//Field validation succeeded. Validate against database
$email = $this->input->post('email');
//query the database
$result = $this->user_model->login($email, $password);
if($result)
{
$sess_array = array();
foreach($result as $row)
{
$sess_array = array(
'user_id' => $row->user_id,
'email' => $row->email
);
// $session_data = $this->session->set_userdata('logged_in',$sess_array);
$this->session->set_userdata('logged_in', $sess_array);
}
return TRUE;
}
else
{
$this->form_validation->set_message('check_database', 'Invalid email or password');
return false;
}
}
}
Home Controller
<?php
session_start(); //we need to call PHP's session object to access it through CI
class Home extends CI_Controller {
function __construct()
{
parent::__construct();
}
function index()
{
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$data['email'] = $session_data['email'];
$this->load->view('home', $data);
}
else
{
//If no session, redirect to login page
redirect('login', 'refresh');
}
}
function logout()
{
$this->session->unset_userdata('logged_in');
session_destroy();
redirect('home', 'refresh');
}
}
User model
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class User_model extends CI_Model {
public function login($email, $password){
$this->db->select('user_id','email','password');
$this->db->from('users');
$this->db->where('email',$email);
$this->db->where('password',md5($password));
$this->db->limit(1);
$query = $this->db->get();
if($query->num_rows() == 1){
return $query->result();
}
else {
return false;
}
}
public function register($name, $surname, $email, $password)
{
$data = array(
'name' => $name,
'surname' => $surname,
'email' => $email,
'password' => md5($password)
);
if( ($name && $surname && $email && $password) != NULL){
$query = $this->db->insert('users', $data);
}
else{
return false;
}
}
}
Login form
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Logowanie</title>
</head>
<body>
<h1>Widok Logowania</h1>
<?php echo validation_errors(); ?>
<?php echo form_open('verifylogin'); ?>
<label for="email">Email:</label>
<input type="email" id="email" name="email">
<br>
<label for="password">Hasło: </label>
<input type="password" id="password" name="password">
<br>
<input type="submit" value="Zaloguj">
<?php echo form_close(); ?>
</body>
</html>
I think there is problem in your form_open() method
<?php echo form_open('verifylogin'); ?>
You get your variable in controller VerifyLogin of function check_database()
So you form_open would be
<?php echo form_open('verifylogin/check_database'); ?>
and you forget to load
$this->load->helper('form');
try to read manual CI form helper
The Solution:
user_model.php
This
$query = $this->db->select('user_id','email','password')
Change to
$query = $this->db->select('user_id,email','password')
MY GOD!
Related
I call the index() method of the collaborating controller, it automatically inserts 6 records into the database, and the action of logging into the database is not in the cadastrarquestao() method of the class, and not in the index method . I'm using a template library.
Controller Colaborador:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Colaborador extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->helper('url');
$this->load->helper('funcoes_helper');
$this->load->helper('file');
$this->load->helper('cookie');
$this->load->library('session');
$this->load->library('controle_acesso');
$this->load->model('resumoapp_model', 'resumo');
$this->load->model('homeapp_model', 'homeapp');
$this->load->model('simulado_model', 'simulado');
$this->controle_acesso->acesso();
$this->output->enable_profiler(TRUE);
}
public function index() {
$dados['teste'] = 1;
$this->template->load("template/template_app",'app/enviar-questao', $dados);
}
public function logout() {
session_unset();
redirect ('/entrar');
}
public function cadastrarquestao() {
$this->simulado->envioquestao(1);
$dados['mensagem'] = "dados cadastrados com sucesso!";
$this->template->load("template/template_app",'app/enviar-questao', $dados);
}
public function materia() {
}
}
part of the code
public function envioquestao($dados) {
$data = [
'Id_usuario' => $this->session->userdata('id_usuario'),
];
$this->db->insert('teste',$data);
}
librarie Template:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Template {
var $template_data = array();
function set($name, $value)
{
$this->template_data[$name] = $value;
}
function load($template = '', $view = '' , $view_data = array(), $return = FALSE)
{
$this->CI =& get_instance();
$this->set('contents', $this->CI->load->view($view, $view_data, TRUE));
return $this->CI->load->view($template, $this->template_data, $return);
}
}
View template:
<html lang="pt-br">
<?php
$this->load->view('app/header');
?>
<body>
<div class="wrapper">
<?php
$this->load->view('app/topo');
?>
<?php
$this->load->view('app/menu');
?>
<?php echo $contents ?>
<footer class="footer-container"><span>© </span></footer>
</div>
<?php
$this->load->view('app/footer');
?>
</body>
</html>
the correct one would be to insert it in the database after clicking on 'register' which is in the cadastrarquestao() method
i'm try to get news_id from database but when go to view say this error :
Trying to get property of non-object / Message: Message: Undefined variable: xls
model:
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Mdl_tagihan extends CI_Model { public function employeeList() {
$this->db->select(array('e.id', 'e.IDTAGIHAN', 'e.BANDWIDTH_BILLING', 'e.SITE_NAME', 'e.PERIODE_TAGIHAN',
'e.REGION', 'e.NOMINAL', 'e.USED_FOR','e.RECON_PERIOD','e.OA_DATE','e.REQUEST_ID','e.TRANSMISSION_ID','e.PROVIDER','e.PRODUCT',
'e.SOW', 'e.NE_ID', 'e.NE_NAME', 'e.FE_ID', 'e.FE_NAME', 'e.BANDWIDTH', 'e.SERVICE_2G', 'e.SERVICE_3G', 'e.SERVICE_4G', 'e.TOTAL_SERVICE'));
$this->db->from('import as e');
$query = $this->db->get();
return $query->result_array();
}
controller:
class Ctrl_tagihan extends CI_Controller { public function __construct()
{
parent::__construct();
if($this->session->userdata('group') != '1'){
$this->session->set_flashdata('error','Maaf, login first!');
redirect('CTRL_Login');
}
$this->load->model('Mdl_tagihan');
$this->load->helper(array('form', 'url'));
$this->load->library('upload');
}
public function index()
{
$data['tagihan'] = $this->Mdl_tagihan->get_tagihan();
$this->load->view('admin/dbtagihan/index_tagihan', $data);
}
public function export_excel(){
$data = array( 'IDTAGIHAN' => 'Laporan Excel',
'dbtagihan' => $this->Mdl_tagihan->listing());
$this->load->view('admin/dbtagihan/laporanexcel_tagihan',$data);
}
view:
<?php ("Content-type: application/octet-stream"); header("Content-Disposition: attachment; filename=$xls.xls.xls"); ("Pragma: no-cache"); ("Expires: 0") ?> <table border="1" width="100%"> <thead> <tr>
In order to use the $xls on your view, you have to supply a $xls variable on the controller :
public function export_excel(){
$data = array( 'IDTAGIHAN' => 'Laporan Excel',
'dbtagihan' => $this->Mdl_tagihan->listing());
$data['xls'] = 'Title';
$this->load->view('admin/dbtagihan/laporanexcel_tagihan',$data);
}
Here is my MessageController.php file
class MessageController extends Controller
{
protected $authUser;
public function __construct()
{
$this->middleware('auth');
Talk::setAuthUserId(Auth::user()->id);
View::composer('partials.peoplelist', function($view) {
$threads = Talk::threads();
$view->with(compact('threads'));
});
}
public function chatHistory($id)
{
$conversations = Talk::getMessagesByUserId($id);
$user = '';
$messages = [];
if(!$conversations) {
$user = User::find($id);
} else {
$user = $conversations->withUser;
$messages = $conversations->messages;
}
return view('messages.conversations', compact('messages', 'user'));
}
public function ajaxSendMessage(Request $request)
{
if ($request->ajax()) {
$rules = [
'message-data'=>'required',
'_id'=>'required'
];
$this->validate($request, $rules);
$body = $request->input('message-data');
$userId = $request->input('_id');
if ($message = Talk::sendMessageByUserId($userId, $body)) {
$html = view('ajax.newMessageHtml', compact('message'))->render();
return view('messages.conversations', compact('messages', 'user'));
}
}
}
public function ajaxDeleteMessage(Request $request, $id)
{
if ($request->ajax()) {
if(Talk::deleteMessage($id)) {
return response()->json(['status'=>'success'], 200);
}
return response()->json(['status'=>'errors', 'msg'=>'something went wrong'], 401);
}
}
i am trying to send a message from this form
<form action="{{url('/message_send')}}" method="post" id="talkSendMessage">
<textarea name="message-data" id="message-data" placeholder ="Type your message" rows="3"></textarea>
<input type="hidden" name="_id" value="{{#request()->route('id')}}">
<button type="submit">Send</button>
</form>
but it doesnt work, an error saying NotFoundHttpException in RouteCollection.php line 161: and here is my routes.php file
Route::get('message/{id}', 'MessageController#chatHistory')->name('message.read');
Route::group(['prefix'=>'ajax', 'as'=>'ajax::'], function() {
Route::post('message_send', 'MessageController#ajaxSendMessage')->name('message.new');
Route::delete('message/delete/{id}', 'MessageController#ajaxDeleteMessage')->name('message.delete');
});
I dont understand where the error is coming from??
You have missed the prefix you defined in your route group.
Route::group(['prefix'=>'ajax', 'as'=>'ajax::'], function() {
You have to add the prefix as well to the form's action,
<form action="{{url('/ajax/message_send')}}" method="post" id="talkSendMessage">
I am creating a login system and for some reason it redirects back to the login page even after i have put in the correct email and password. The called model function doesnt seem to work. Here is my code:
Controller:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login extends CI_Controller {
function validation()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email', 'required|trim');
$this->form_validation->set_rules('password', 'Password', 'required|md5');
if ($this->form_validation->run()){
redirect('login/valid_credentials');
}else{
$this->load->view('login_form');
//return false;
}
}
function valid_credentials(){
$this->load->model('login_model');
// oh i see
if ($this->login_model->match_login()){
//return true;
$data = array(
'id'=>$q['id'],
'email'=>$this->input->post('email'),
'is_logged_in'=> 1);
$this->session->set_userdata($data);
$this->load->view('dashboard_view', $data);
}else{
$this->load->view('login_form');
}
}
Model:
class Login_model extends CI_Model{
public function match_login(){
$this->db->where('email', $this->input->post('email'));
$this->db->where('password', md5($this->input->post('password')));
$q = $this->db->get('user');
if($q->num_rows()== 1){
return true;
}
}
}
View:
<?php
echo "<h1 class ='col-lg-10 col-lg-offset-5'>SIGN IN</h1>";
echo form_open('login/validation', $grid);
echo "<h2>Client</h2>";
"<h3>Please Login</h3>";
echo validation_errors();
echo "<p> Email: </br>";
echo form_input ('email');
echo "</p>";
echo "<p> Password: </br>";
echo form_password ('password');
echo "</p>";
echo "<p>";
echo form_submit ('submit', 'Signin');
echo "</p>";
echo form_close();
?>
For some reason, it is not working, Am not sure what I am missing.
No Need to validate form in one function and check login in another function. use one function for do all.
Changes
Controller code change and optimized
Model code changed
Some References
CodeIgniter Form Validation in FromGet.com (Personally Recommend this site )
Form Validation in Codeigniter.com
Try this
Controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login extends CI_Controller {
function validation()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email', 'required|trim');
$this->form_validation->set_rules('password', 'Password', 'required|md5');
if ($this->form_validation->run() == FALSE )
{
$this->load->view('login_form');
//return false;
}
else{
$this->load->model('login_model');
$email = $this->input->post('email');
$password = $this->input->post('password');
$result = $this->login_model->match_login($email, $password);
if ($result ==false) {
echo "Invalid Cardinals";
}
else
{
$session = array(
'id'=>$result[0]['id'],
'email'=>$this->input->post('email'),
'is_logged_in'=> 1
);
if (!$this->session->set_userdata($session)) {
$data['modelData'] = $result;
$data['sessionData'] = $session;
$this->load->view('dashboard_view', $data);
}
else {
echo "Error in session";.
}
}
}
}
Model
class Login_model extends CI_Model{
public function match_login($email, $password){
$this->db->where('email', $email);
$this->db->where('password', md5($password));
$query = $this->db->get('user');
$result = $query->result_array();
if (empty($result))
{
return false;
}
else
{
return $result;
}
}
}
I am trying to work through the CodeIgniter tutorial and my news pages won't output data from the foreach loop. I get the following messages:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: news
Filename: pages/index.php
Line Number: 1
and
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: pages/index.php
Line Number: 1
This is my model class:
<?php
class News_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function get_news($slug = FALSE)
{
if ($slug === FALSE)
{
$query = $this->db->get('news');
return $query->result_array();
}
$query = $this->db->get_where('news', array('slug' => $slug));
return $query->row_array();
}
public function index()
{
$data['news'] = $this->news_model->get_news();
$data['title'] = 'News archive';
$this->load->view('templates/header', $data);
$this->load->view('news/index', $data);
$this->load->view('templates/footer');
}
}
And my controller:
class News extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('news_model');
}
public function index()
{
$data['news'] = $this->news_model->get_news();
}
public function view($slug)
{
$data['news_item'] = $this->news_model->get_news($slug);
if (empty($data['news_item']))
{
show_404();
}
$data['title'] = $data['news_item']['title'];
$this->load->view('templates/header', $data);
$this->load->view('news/view', $data);
$this->load->view('templates/footer');
}
}
And the first view:
<h2><?php echo $news_item['title'] ?></h2>
<div id="main">
<?php echo $news_item['text'] ?>
</div>
<p>View article</p>
<?php endforeach ?>
and the second:
<?php
echo '<h2>'.$news_item['title'].'</h2>';
echo $news_item['text'];
I know there are other questions about the tutorial but none seemed to help me.
Thanks.
in model you have closed your class after constructor. Should be closed after all function.
Also view() is initialized twice.