In this example, we are going to show you how to insert data in the database using laravel framework PHP.
For insert data in MySQL using laravel first, we have to create a table in the data base.
The INSERT INTO statement is used to insert new data to a MySQL table:
INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)
To learn more about SQL, please visit our
For creating table the SQL query is:
SQL Query
CREATE TABLE student_details
(
id int NOT NULL AUTO_INCREMENT,
first_name varchar(50),
last_name varchar(50),
city_name varchar(50),
email varchar(50),
PRIMARY KEY (id)
);
Create 3 files for insert data in Laravel :
- StudInsertController.php (app/Http/Controllers/StudInsertController.php)
- stud_create.php (resources/views/stud_create.php)
- web.php (routes/web.php)
stud_create.php
<!DOCTYPE html>
<html>
<head>
<title>Student Management | Add</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<br>
<h2 class="text-center">Form Insert Data Student</h2>
<form action = "/create" method = "post">
<input type = "hidden" name = "_token" value = "<?php echo csrf_token(); ?>">
<div class="form-group">
<div class="row">
<div class="col-6">
<label for="usr">First Name:</label>
<input type='text'class="form-control" name='first_name' required >
</div>
<div class="col-6">
<label for="usr">Last Name:</label>
<input type="text" class="form-control" name='last_name' required>
</div>
</div>
</div>
<div class="form-group">
<label for="pwd">City Name:</label>
<select class="form-control" name="city_name" required>
<option >Please select city</option>
<option value="Phnom Penh">Phnom Penh</option>
<option value="USA">USA</option>
<option value="English">English</option>
<option value="Canada">Canada</option>
<option value="Frence">Frence</option>
</select>
</div>
<div class="form-group">
<label for="pwd">City Name:</label>
<input type="text" class="form-control" name='email' required>
</div>
<input type ='submit' class=" btn btn-success text-center" value = "Insert">
</div>
</form>
</body>
</html>
StudInsertController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class StudInsertController extends Controller {
public function insertform(){
return view('stud_create');
}
public function insert(Request $request){
$first_name = $request->input('first_name');
$last_name = $request->input('last_name');
$city_name = $request->input('city_name');
$email = $request->input('email');
$data=array('first_name'=>$first_name,"last_name"=>$last_name,"city_name"=>$city_name,"email"=>$email);
DB::table('student')->insert($data);
echo "Record inserted successfully.<br/>";
echo '<a href = "/insert">Click Here</a> to go back.';
}
}
web.php
<?php
/*
|--------------------------------------------------------------------------
| 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!
|
*/
//insert dataRoute::get('insert','StudInsertController@insertform');
Route::post('create','StudInsertController@insert');
0 Comments
CAN FEEDBACK
Emoji