How To Solve Laravel and MySql Error Called SQLSTATE[23000] - php

I want to create a Registration form. So, I have created a RegShortController.php file and after I have created view file called RegShort.blade.php. In RegShortController.php file , I have used a function called insert. But , When I run the Web Site , I got this error -
"SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name'
cannot be null (SQL: insert intoacademic(name,username,pw)
values (, , ))"
So , How to Fix this ??
Here is the RegShort.blade.php file
Unfortunately , I have removed name attributes in username and password when I edit this Thread. Now , I fixed it.
<form action="{{ route('RegShort') }}" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="form-group">
<label>Name : *</label>
<input type="text" class="form-control" name="name">
</div>
<div class="form-group">
<label>Username : *</label>
<input type="text" class="form-control" name="username" required>
</div>
<div class="form-group">
<label>Password : *</label>
<input type="password" class="form-control" name="password">
</div>
<button type="submit" class="btn btn-primary" name="submit">Submit</button>
</form>
Here is the RegShortController.php file
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
class RegShortController extends Controller
{
public function index()
{
return view('RegShort');
}
function insert(Request $req)
{
$name = $req->input('name');
$username = $req->input('username');
$password = $req->input('password');
$data = array("name"=>$name,'username'=>$username,"pw"=>$password);
DB::table('academic')->insert($data);
}
}
Here is the Route that I have created.
Route::any('/RegShort', 'RegShortController#insert')->name('RegShort');

name field must be non empty, so u should validate data before inserting it (server side validation):
function insert(Request $req)
{
$this->validate($req, [
'name' => 'required'
]);
$name = $req->input('name');
$username = $req->input('username');
$password = $req->input('password');
$data = array("name"=>$name,'username'=>$username,"pw"=>$password);
DB::table('academic')->insert($data);
}
BTW would be nice to validate data by javascript as well before sending data to server (client side validation).
Edit ur HTML, uve missed name attributes for ur inputs:
<form action="{{ route('RegShort') }}" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="form-group">
<label>Name : *</label>
<input type="text" class="form-control" name="name">
</div>
<div class="form-group">
<label>Username : *</label>
<input type="text" class="form-control" name="username" required>
</div>
<div class="form-group">
<label>Password : *</label>
<input type="password" name="password" class="form-control">
</div>
<button type="submit" class="btn btn-primary" name="submit">Submit</button>
</form>
Also change ur routes:
Route::get('/RegShort', 'RegShortController#index')->name('RegShort');
Route::post('/RegShort', 'RegShortController#insert');

It seems that you are unable to get data from the $req object. Be sure to check your data before inserting into the table.

you dont have name attribute for username and password
<div class="form-group">
<label>Name : *</label>
<input type="text" class="form-control" name="name">
</div>
<div class="form-group">
<label>Username : *</label>
<input type="text" name="username" class="form-control" required>
</div>
<div class="form-group">
<label>Password : *</label>
<input type="password" name="password" class="form-control">
</div>

Try to update html:
You are messing: name attribute here
<input type="text" name="username" class="form-control" required>
<input type="password" name="password" class="form-control">
Code:
<form action="{{ route('RegShort') }}" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="form-group">
<label>Name : *</label>
<input type="text" class="form-control" name="name">
</div>
<div class="form-group">
<label>Username : *</label>
<input type="text" name="username" class="form-control" required>
</div>
<div class="form-group">
<label>Password : *</label>
<input type="password" name="password" class="form-control">
</div>
<button type="submit" class="btn btn-primary" name="submit">Submit</button>
</form>

Related

After submitting a form i get an error "Sorry, the page you are looking for could not be found."

I'm new with laravel and now i making some small project. I have a form, after the submit button pressed i got this error message "Sorry, the page you are looking for could not be found."
Is there anything wrong about my code?
Please help me to fix this issue, so i can continuing the project.
Thanks in advice
view blade, i named it index.blade.php
<div class="col m7 s12">
<form method="submit" action="post">
{{ csrf_field() }}
<div class="card-panel">
<h5>Please Fill Out This Form</h5>
<div class="input-field">
<input type="text" name="name" id="name" required class="validate">
<label for="name">Name</label>
</div>
<div class="input-field">
<input type="email" name="email" id="email" class="validate">
<label for="email">Email</label>
</div>
<div class="input-field">
<input type="text" name="phone" id="phone">
<label for="phone">Phone</label>
</div>
<div class="input-field">
<textarea name="message" id="message" class="materialize-textarea"></textarea>
<label for="message">Message</label>
</div>
<button type="submit" class="btn" blue darken-1>Send</button>
</div>
</form>
controller, i named it LayoutController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
class LayoutController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
//
return view('layouts/index');
}
public function submit(Request $request)
{
$name = $req->input('name');
$email = $req->input('email');
$phone = $req->input('phone');
$message = $req->input('message');
$data = array('name'=>$name,"email"=>$email,"phone"=>$phone,"message"=>$message);
$data->save();
return Redirect::to('/layouts/index');
}
routes web.php
Route::get('/', 'LayoutController#index');
Route::post('/submit', 'LayoutController#submit');
Your form method should be POST and action should be /submit
<form method="POST" action="/submit">
{{ csrf_field() }}
<div class="card-panel">
<h5>Please Fill Out This Form</h5>
<div class="input-field">
<input type="text" name="name" id="name" required class="validate">
<label for="name">Name</label>
</div>
<div class="input-field">
<input type="email" name="email" id="email" class="validate">
<label for="email">Email</label>
</div>
<div class="input-field">
<input type="text" name="phone" id="phone">
<label for="phone">Phone</label>
</div>
<div class="input-field">
<textarea name="message" id="message" class="materialize-textarea"></textarea>
<label for="message">Message</label>
</div>
<button type="submit" class="btn" blue darken-1>Send</button>
</div>
</form>
Try this :
<form method="POST" action="{{ route('submit') }}">
The Error you're getting is because the wrong <form> tag attributes
action => 'The route or page or class method that'll process the form
information'
method => 'This the URI HTTP verb used to transport information, you
can either use POST(sending data as http payload) or GET(sending data
as query string)
changing the <form> tag like this will solve your issue
<form method="POST" action="{{ url('/submit') }}">
The form method should be POST and the action will be your route:
<form method="POST" action="{{ url('/submit') }}">
<div class="col m7 s12">
<form method="POST" action="{{url('/submit')}}">
{{ csrf_field() }}
<div class="card-panel">
<h5>Please Fill Out This Form</h5>
<div class="input-field">
<input type="text" name="name" id="name" required class="validate">
<label for="name">Name</label>
</div>
<div class="input-field">
<input type="email" name="email" id="email" class="validate">
<label for="email">Email</label>
</div>
<div class="input-field">
<input type="text" name="phone" id="phone">
<label for="phone">Phone</label>
</div>
<div class="input-field">
<textarea name="message" id="message" class="materialize-textarea"></textarea>
<label for="message">Message</label>
</div>
<button type="submit" class="btn" blue darken-1>Send</button>
</div>
</form>

Laravel MethodNotAllowedHttpException when i try to save fields?

Hi i try to save fields in db ot 2 steps but i have MethodNotAllowedHttpException
on step 1 in my controller i save the 2 fields in db
public function storeNumber(Request $request){
$number = new Number;
$number->user_id = $user = \Auth::user()->id;
$number->number = $this->getGeneratedNumber();
$number = new Number($request->all());
$number->save();
return redirect('numbers/{id}/details');
}
view
<form class="form-horizontal" method="POST" action="{{action('NumberController#storeNumber')}}">
{{ csrf_field() }}
<div class="form-group">
<div class="col-md-12">
<button type="submit" class="btn btn-primary btn-block">
Generate Numbers
</button>
</div>
</div>
model
class Number extends Model
{
/**
* #var array
*
*/
protected $fillable = [
'number'
];
}
on step 2 i want to save another field in same db with same controller this is my another store function for store another fields in same db . but when i try to save laravel say MethodNotAllowedHttpException.
public function store(Request $request, $id){
$number = Number::find($id);
$number = new Number($request->all());
$number->save();
return redirect('numbers');
}
this is my view
<form method="post" action="{{action('NumberController#store', $id)}}">
{{csrf_field()}}
<input name="_method" type="hidden" value="PATCH">
<div class="form-group">
<label for="number" class="control-label">Number</label>
<input type="text" class="form-control form-control-lg disabled" placeholder="Number" name="number" value="{{$number->number}}">
</div>
<div class="form-group">
<label for="comment" class="control-label">Comment</label>
<textarea name="comment" class="form-control form-control-lg" cols="30" rows="10">{{$number->comment}}</textarea>
</div>
<div class="form-group">
<label for="accept" class="control-label">Accept</label>
<input type="radio" name="accept" value="1">Yes<br>
<input type="radio" name="accept" value="0">No<br>
</div>
#if($number->accept == 1)
<div class="form-group">
<label for="name" class="control-label">Number</label>
<input type="text" class="form-control form-control-lg disabled" placeholder="Name" name="name" value="{{$number->name}}">
</div>
<div class="form-group">
<label for="city" class="control-label">City</label>
<input type="text" class="form-control form-control-lg" placeholder="City" name="city" value="{{$number->city}}">
</div>
<div class="form-group">
<label for="postcode" class="control-label">Postcode</label>
<input type="number" class="form-control form-control-lg" placeholder="Postcode" name="postcode" value="{{$number->postcode}}">
</div>
<div class="form-group">
<label for="address">Address</label>
<textarea name="address" class="form-control form-control-lg" cols="30" rows="10">{{$number->address}}</textarea>
</div>
#else
<p>TODO status for NO</p>
#endif
<div class="form-group">
<div class="col-md-12">
<button type="submit" class="btn btn-default">Finish</button>
</div>
</div>
</form>
You have a "_method" filed with "PATCH" value, so you have to change the route to "patch" instead of "post".
Please change blade content of update as follow
<form method="post" action="{{action('NumberController#store', $id)}}">
to
<form method="post" action="{{action('NumberController#update', $id)}}">
return redirect('numbers/{id}/details');
Here is incorrect, what is id?

Autocomplete search in Laravel

I was wondering if there is any way I can display more than one fields in the autocomplete search box. It already search for fist and last name but when the user types the last name it shows only the first name. But I wonder if there is a way that I can also display the last name or address on the search result. I am only getting the id of the guardian because it's the only thing I need to sent to the database. Here is a look at my code:
This is my function for the autocomplete:
public function autocomplete(Request $request)
{
$term=$request->term;
$data = Guardian::orWhere('first_name','LIKE','%'.$term.'%')
->orWhere('last_name','LIKE','%'.$term.'%')
->take(10)->get();
$results=array();
foreach($data as $key => $v)
{
$results[]=['id'=>$v->idguardian, 'value'=>$v->first_name];
}
return response()->json($results);
}
My view:
<div class="row">
<div class="col-md-6">
<h3>New Kid</h3>
<p> Please search the guardian name for the kid. If the guardian isn't registered please add it</p>
<form action="{{route('createkid')}}" method="post">
<div class="form-group">
<input type="text" name="searchname" class="form-control" id="searchname" placeholder="Search">
<tr>
<td>ID</td>
<td><input type="text" name="idguardian" id="idguardian" class="form-control"></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
</div>
<a href="{{URL::to('addguardian/list/')}}" class="btn btn-success btn-xs" >Add Guardian</a>
<div class ="form-group">
<label for="first_name">First Name</label>
<input type="text" name="first_name" class="form-control" placeholder="FirstName">
</div>
<div class ="form-group">
<label for="middle_name">Middle Name</label>
<input type="text" name="middle_name" class="form-control" placeholder="MiddleName">
</div>
<div class ="form-group">
<label for="last_name">Last Name</label>
<input type="text" name="last_name" class="form-control" placeholder="LastName">
</div>
<div class ="form-group">
<label for="dateofbirth">Date Of Birth</label>
<input type="text" name="dateofbirth" class="form-control" placeholder="DateOfBirth">
</div>
<div class ="form-group">
<label for="sex">Sex</label>
<input type="text" name="sex" class="form-control" placeholder="Sex">
</div>
<div class ="form-group">
<label for="age">Age</label>
<input type="text" name="age" class="form-control" placeholder="Age">
</div>
<div class ="form-group">
<label for="schoolname">School Name</label>
<input type="text" name="schoolname" class="form-control" placeholder="SchoolName">
</div>
<div class ="form-group">
<label for="schoolgrade">School Grade</label>
<input type="text" name="schoolgrade" class="form-control" placeholder="SchoolGrade">
</div>
<div class ="form-group">
<label for="schoolgroup">School Group</label>
<input type="text" name="schoolgroup" class="form-control" placeholder="SchoolGroup">
</div>
<div class ="form-group">
<label for="bloodtype">Blood Type</label>
<input type="text" name="bloodtype" class="form-control" placeholder="BloodType">
</div>
<div class ="form-group">
<label for="allergies">Allergies</label>
<input type="text" name="allergies" class="form-control" placeholder="Allergies">
</div>
<div class ="form-group">
<label for="enroll_date">Enroll Date</label>
<input type="text" name="enroll_date" class="form-control" placeholder="EnrollDate">
</div>
<div class="form-group">
{!! Form::label('iduser','Select User') !!}
{!! Form::select('iduser', $kids, ['class' => 'form-control']) !!}
</div>
<button type="submit" class="btn btn-primary">Submit</button>
<input type="hidden" name="_token" value="{{ Session::token()}}">
</form>
</div>
</div>
<script type="text/javascript">
$("#searchname").autocomplete({
source : '{!!URL::route('autocomplete')!!}',
minLenght:1,
autoFocus:true,
select:function(e, ui){
$('#idguardian').val(ui.item.id);
}
});
</script>
My Route:
Route::get('/autocomplete',array('as'=>'autocomplete','uses'=>'KidsController#autocomplete'));
I really hope you can help me. Thank you!
Why not just append the last name onto the first name?
public function autocomplete(Request $request)
{
$term=$request->term;
$data = Guardian::orWhere('first_name','LIKE','%'.$term.'%')
->orWhere('last_name','LIKE','%'.$term.'%')
->take(10)->get();
$results=array();
foreach($data as $key => $v)
{
$results[]=['id'=>$v->idguardian, 'value'=>$v->first_name . ' ' . $v->last_name];
}
return response()->json($results);
}

Passing data from form to controller Laravel 5

I'm trying register users in a laravel 5 application using the restful controller.
The problem is that when I dump the data in my store function, I only get the csrf token, but not the values.
Here's what I tried so far:
Input::all();
Request::get();
Here's the code I'm executing:
Form
<form class="form-horizontal" method="post" action="/users">
<div class="form-group">
<label for="name" class="col-lg-2 control-label">
Name
</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="name" value="a">
</div>
</div>
<div class="form-group">
<label for="email" class="col-lg-2 control-label">
Email
</label>
<div class="col-lg-10">
<input type="email" class="form-control" id="email" value="a#a.Fr">
</div>
</div>
<div class="form-group">
<label for="password" class="col-lg-2 control-label">
Pw
</label>
<div class="col-lg-10">
<input type="password" class="form-control" id="password">
</div>
</div>
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
<button type="reset" class="btn btn-default">Cancel</button>
<button type="submit" class="btn btn-primary">Create</button>
</div>
</div>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
Controller
public function store()
{
$input = Input::only('name','email','password');
$user = new User;
$user->name = $input['name'];
$user->email = $input['email'];
$user->password = Hash::make($input['password']);
$user->save();
}
And Route is just
Route::resource('users','UsersController');
Your input fields are all missing a name attribute! For example
<input type="text" class="form-control" id="name" name="name" value="a">
<!-- ^^^^^^^^^^^ -->
You should put name attributes on the input fields, because html forms communicate with php with name attribute.
Example:
<input........... name="etc">
and in controller use the name you have given an input as a key :
$user->name = $input['etc'];
<input type="text" class="form-control" id="name" name="name" value="a">

add data using ajax and codeigniter

I'm new in codeigniter
I try to add data in table users using ajax when I send data I have now problem and all data send good but no data stored in data base
and this is my code
controller
public function addusersajax()
{
if($this->input->post("action") =="addusersintable")
{
$this->load->helper('date');
$data=array(
"fullname"=> $this->input->post("fullname"),
"username"=> $this->input->post("username"),
"password" => md5($this->input->post("password")),
"email"=>$this->input->post("email"),
"groubid"=>$this->input->post("groubid"),
"date"=>mdate('%Y-%m-%d ', now()),
"time"=>date(" H:i:s")
);
$this->load->model("usersmodel");
if($this->usersmodel->adduserbyajax($data)){
echo "done";
}else{
echo 'failed';
}
}
}
this is function for view form
public function view()
{
$data['pagetitle']="xx";
$this->load->view("template/admin/header",$data);
$this->load->view("users/ss",$data);
$this->load->view("template/admin/footer");
}
this is my view
<div class="container">
<div id="container">
<div class="col-lg-8">
<form id="insercodegn" method="post" action="" enctype="multipart/form-data">
<div class="form-group">
<label for="recipient-name" class="control-label">Full Name :</label>
<input type="text" class="form-control" name="fullname" id="fullname" placeholder="please insert fullname" autocomplete="off" required="required">
</div>
<div class="form-group">
<label for="recipient-name" class="control-label">UserName :</label>
<input type="text" class="form-control" name="username" id="username" placeholder="please insert user name" autocomplete="off" required="required" >
</div>
<div class="form-group">
<label for="message-text" class="control-label">Password:</label>
<input type="password" class="form-control" name="password" id="password" placeholder="please insert yout password" autocomplete="new-password" required="required" >
<i class=" showpass fa fa-eye fa-3" aria-hidden="true"></i>
</div>
<div class="form-group">
<label for="recipient-name" class="control-label">Email :</label>
<input type="text" class="form-control" name="email" id="email" placeholder="please insert email" autocomplete="off" required="required" >
</div>
<div class="form-group">
<label for="recipient-name" class="control-label">User Type :</label>
<select class="form-control" name="groubid" id="groubid">
<option value="1">Administartor</option>
<option value="0">User</option>
<option value="2">Maker</option>
<option value="3">cheker</option>
</select>
</div>
<div class="form-group">
<label for="exampleInputFile">Image :</label>
</div>
<input type="submit" name="action" value="adduserssss">
<button type="submit" name="action" value="addusersintable" class="btn btn-primary">addcc</button>
</form>
</div>
</div>
</div>
****this is my script inside ss viewpage****
<script>
$(document).ready(function () {
$(function () {
$("#insercodegn").on('submit', function (e) {
e.preventDefault();
$.ajax({
url:'<?php echo base_url()?>Users/addusersajax',
//url:"<?php echo base_url() .'Users/addusersajax'?>",
method:'post',
data: new FormData(this),
contentType: false,
cache: false,
processData: false,
success:function(data) {
alert(data);
}
})
})
})
})
</script>
my form is
<div class="container">
<div id="container">
<div class="col-lg-8">
<form id="insercodegn" method="post" action="" enctype="multipart/form-data">
<div class="form-group">
<label for="recipient-name" class="control-label">Full Name :</label>
<input type="text" class="form-control" name="fullname" id="fullname" placeholder="please insert fullname" autocomplete="off" required="required">
</div>
<div class="form-group">
<label for="recipient-name" class="control-label">UserName :</label>
<input type="text" class="form-control" name="username" id="username" placeholder="please insert user name" autocomplete="off" required="required" >
</div>
<div class="form-group">
<label for="message-text" class="control-label">Password:</label>
<input type="password" class="form-control" name="password" id="password" placeholder="please insert yout password" autocomplete="new-password" required="required" >
<i class=" showpass fa fa-eye fa-3" aria-hidden="true"></i>
</div>
<div class="form-group">
<label for="recipient-name" class="control-label">Email :</label>
<input type="text" class="form-control" name="email" id="email" placeholder="please insert email" autocomplete="off" required="required" >
</div>
<div class="form-group">
<label for="recipient-name" class="control-label">User Type :</label>
<select class="form-control" name="groubid" id="groubid">
<option value="1">Administartor</option>
<option value="0">User</option>
<option value="2">Maker</option>
<option value="3">cheker</option>
</select>
</div>
<div class="form-group">
<label for="exampleInputFile">Image :</label>
</div>
<button type="submit" name="action" value="adduserssss" class="btn btn-primary">add</button>
</form>
</div>
</div>
</div>
Change this:
<input type="submit" name="action" value="adduserssss">
<button type="submit" name="action" value="addusersintable" class="btn btn-primary">addcc</button>
To this:
<input type="hidden" name="action" value="addusersintable">
<button type="submit" class="btn btn-primary">addcc</button>
Buttons don't have $_POST data thus: if($this->input->post("action") =="addusersintable") is always evaluating to false and never hitting your model. A hidden input is what you are looking for.
1) Use lower case url in your javascript file, try "users/addusersajax" instead of "Users/addusersajax".
2) Use var_dump($_POST) or print_r($_POST) in your controller. in network tab of Chrome or Firefox check to see result of your request. you must see what is posted. if it is all good you must check your model
3) In your model it is best to use transactions. see transactions documentations in Codeigniter user guide. also you can echo $this->db->last_query() to see what query is executed. copy the executed query and run it from phpMyAdmin or any other MySQL client you use and see if you get any error.

Categories