Home > Net >  MySQL Database not updated after registration
MySQL Database not updated after registration

Time:11-12

First of all I am new to Laravel framework, so please bear with me. I am creating an e-commerce website and created a registration page. The problem is whenever I fill the registration form and press register button the page will refresh but the data I filled will not be updated in MySQL database.

the registration file. registration.blade.php `

@extends('layouts.master')
@section('content')
@section('title', 'Register')

    <section > 
        <div >
            <div > Register</div>
            <div >
                <form action="{{route('register')}}" method="post">
                @csrf
                    <div >
                        <label for="name">name</label>
                        <input type="text" id="name" name="name" placeholder="john" >

                    </div>

                    <div >
                        <label for="email">email</label>
                        <input type="email" id="email" name="name" placeholder="[email protected]" >

                    </div>

                    <div >
                        <label for="password">password</label>
                        <input type="password" id="password" name="password" placeholder="******" >

                    </div>

                    <div >
                        <label for="passowrd_confirmation">confirm password</label>
                        <input type="password" id="password" name="passowrd_confirmation" placeholder="******" >

                    </div>

                    <div >
                        <button type= "submit" >Register</button>
                    </div>
                    

                </form>
            </div>
        </div>

    </section>
@endsection

` Also, here the route file in laravel

web.php

<?php

use App\Http\Controllers\AuthController;
use App\Http\Controllers\PagesController;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', [PagesController::class, 'home'])->name('home');
Route::get('/cart', [PagesController::class, 'cart'])->name('cart');

//Auth
Route::get('/login', [AuthController::class, 'showLogin'])->name('login');
Route::get('/register', [AuthController::class, 'showRegister'])->name('register');
Route::post('/register', [AuthController::class, 'postRegister'])->name('register');

And here is the file for registration functions AuthController.php `

<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;

class AuthController extends Controller
{
    //show login page
    public function showLogin()
    {
        return view('pages.login');
    }

    //show register page
    public function showRegister()
    {
        return view('pages.register');
    }

     //register user
    public function postRegister(Request $request)
     {
        //validation
        $request->validate([
            'name' => 'required|min:3|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|min:8|confirmed'
        ]);


        //registration
        $user = User::create([
            'name' => $request->name,
            'email' => $request->email,
            'password' => Hash::make($request->password),

        ]);


        //login user
        Auth::login($user);

        return back()->with('success','succesfully Logged in');
        
     }
    

    
}

` here is my Database in phpmyadmin after filling the form: users database I tried to use showRegister function in my AuthController.php in order to check if the data being filled can be seen in the webpage. However, I was not able to see anything.

CodePudding user response:

Display errors and old values

First of all, I suggest you to update your view to display errors when using validation, and display old values:

<div >
  <label for="name">name</label>
  <input type="text" id="name" name="name" value="{{ old('name') }}" placeholder="john" >
  @error('name')
    <div>{{$message}}</div>
  @enderror
</div>

If the validation has no success, no changes will be made. Try to display if the validation has passed:

public function postRegister(Request $request)
{
  //validation
  $request->validate([
    'name' => 'required|min:3|max:255',
    'email' => 'required|email|max:255|unique:users',
    'password' => 'required|min:8|confirmed'
  ]);

  dd("Validation passed!);
}

This will help you to find errors.

Take a look at the official Laravel validation documentation

CodePudding user response:

Try to check validation errors messages in registration.blade.php file

@if ($errors->any())
    <div >
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif
  • Related