简体   繁体   中英

SQLSTATE[42S02]: Base table or view not found: 1146 Table X doesn't exist

I am getting this error in Laravel 5:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'intern.users' doesn't exist (SQL: select * from users where username = admin limit 1)

config/database.php

        'mysql' => [
    'driver'    => 'mysql',
    'host'      => 'localhost',
    'database'  => 'intern',
    'username'  => 'root',
    'password'  => '',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
    'strict'    => false,
],

The function admin is called for admin page, and database table is mentioned in it as admin because there are many tables in it.

    public function admin(Request $request){

           if($request->isMethod('get')){
           return \View::make('student.admin');
        } else
    {


        $check=0;
        $check=\DB::table('admin')->get();
        $username = Input::get('username');
        $password = Input::get('password');



     $data=array(
     'username'=>$request->get('username'),
     'password'=>$request->get('password')
    );

    if(\Auth::attempt($data))
    {
        return redirect::intended('student/index');
    }
    else
    {
        return redirect('student/admin');
    }


        }   



       }    

Form is here:

  <div id="pageContent"><br />
<div align="left" style="margin-left:24px;">
  <h2>Please Log In To Manage</h2>
  {!! Form::open(array('url' => '/admin')) !!}
  <input type="hidden" name="_token" value="{{ csrf_token() }}">


    User Name:<br />
      <input name="username" type="text" id="username" size="40" />
    <br /><br />
    Password:<br />
   <input name="password" type="password" id="password" size="40" />
   <br />
   <br />
   <br />

     <input type="submit" name="button" id="button" value="Log In" />


 {!! Form::close() !!}

First you should hash and create the User details to make the coloumn ready for authentication.

Here i have given the steps to achieve it.

Step 1 : Get the Input

$UserData = Input::all();

Step 2 : Create the entry - Inserting into User Table

User::create($UserData);

Note :

You should have these following coloumns in your users table

  1. email,
  2. password
  3. created_at
  4. updated_at

Additional Setup :

Have this line in your User.php (Model)

protected $fillable = ['email', 'password'];

Here's my tiny login code for you which would simple enough for you

Have a try over this if you wish

$email = $this->request->input('email');
$password = $this->request->input('password');
if (Auth::attempt(['email' => $email, 'password' => $password])) #If the Credentials are Right
{
 return redirect::intended('student/index'); #Your Success Page
}
else
{
 return redirect('student/admin'); #Your Failure Page
}

Recommendation :

I would also recommend to validate the user input before creating

Additional Note :

If you see your table and if you password is something like encrypted and it means that you're done ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM