This post will help step by step to understand , how to save your HTML form data into Mysql Database.
Database Table structure
Form
Step 1
Make HTML Form , code is given below
<form method="post" action="index">
@csrf
Name : <input type="text" name="name" placeholder="Your Name" required class="form-control"><br>
Phone : <input type="text" name="phone" required placeholder="Phone Number" class="form-control"><br>
Message :
<textarea rows="5" name="message" required placeholder="Message" class="form-control"></textarea><br>
<input type="submit" value="Save" name="submit">
</form>
Step 2
Create Model by given command
php artisan make:model stumodel
Model Location : app\Models
Model Code
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class stumodel extends Model
{
protected $table = 'kashiquery';
use HasFactory;
public $timestamps=false;
protected $fillable = [
'name', 'phone','message','createddate'
];
}
Step 3 : Create Controller
Create controller by given command below
Command : PHP artisan make:controller common
Controller location : app\Http\Controllers
Code
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\stumodel;
class common extends Controller
{
public function AddData(Request $req)
{
$data=new stumodel;
//$r = $req->input();
$data->name=$req["name"];
$data->phone=$req["phone"];
$data->message=$req["message"];
$data->createddate=date("Y-m-d");
$data->save();
return redirect('index');
}
}
Step 4 : Configure Route
Route File Location : \routes\web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\common;
Route::get('/', function () {
return view('index');
});
Route::view('index','index');
Route::post('index',[common::class,'AddData']);
No comments:
Post a Comment