Undefined variable Laravel using ajax function - php

I am using ajax for submitting data in the database but when I hit submit button in the return response "undefined " value I got, and the problem lies in the controller. I am not sure why I got this error. I am getting this error on fname variable and obv other variables too as they all are similar.
Here is the Controller
public function save(Request $request)
{
$validator = \Validator::make($request->all(), [
'cnic' => 'required|unique:patients',
]);
if ($validator->fails()) {
return response()->json(['success' => false, 'errors' => $validator->errors()->all()]);
}
$temppatientId = IdGenerator::generate(['table' => 'patients', 'length' => 5, 'prefix' => '22']);
$patientid = $temppatientId + 1;
$query = new patient;
$query->patientid = $patientid;
$query->fname =$fname;
$query->lname = $lname;
$query->cnic = $cnic;
$query->contactno = $contactno;
$query->gender = $gender;
$query->age = $age;
$query->dob = $dob;
$query->city = $city;
$query->address = $address;
$query->husbandname = $husbandname;
$query->fathername = $fathername;
$query->bloodgroup = $bloodgroup;
$query->maritalstatus = $maritalstatus;
$query->save();
return response()->json(['success' => true, 'patients' => $query]);
}
Here is the Ajax function
$(document).ready(function() {
$("#save1").on('click', function(e) {
var cnic = $("#cnic").val();
if (cnic == '') {
alert("Kindly Enter CNIC");
return false;
}
var gender = $("#gender").val();
if (gender == '') {
alert("Kindly Enter Gender");
return false;
}
var contactno = $("#contactno").val();
if (contactno == '') {
alert("Kindly Enter Contact No");
return false;
}
var fname = $("#fname").val();
if (fname == '') {
alert("Kindly Enter Name");
return false;
}
else{
$.ajax({
url: "/save",
type: "post",
data: $('#registrationform').serialize(),
dataType: 'json',
success: function(data) {
if (data.success === false) {
alert('CNIC already Exists !');
} else {
$("#patientid").val(data.patient.patientid);
// console.log(data.patient);
}
}
})
}
});
});
this is my blade file
<form class="form" data-parsley-validate id="registrationform" autocomplete="off">
#csrf
<div class="row">
<div class="col-md-4 col-4">
<div class="form-group mandatory">
<label for="first-name-column" class="form-label">First Name</label>
<input type="text" autofocus tabIndex="1" name="fname" id="fname" class="form-control" placeholder="First Name" name="fname" data-parsley-required="true">
</div>
</div>
<div class="col-md-4 col-4">
<div class="form-group">
<label for="last-name-column" class="form-label">Last Name</label>
<input type="text" id="lname" tabIndex="2" class="form-control" placeholder="Last Name" name="lname">
</div>
</div>
<div class="col-md-4 col-4">
<div class="form-group">
<label for="city-column" class="form-label">Age</label>
<input type="number" id="age" tabIndex="3" class="form-control" placeholder="Age" name="age" >
</div>
</div>
<div class="col-md-4 col-4">
<div class="form-group">
<label for="country-floating" class="form-label">DOB</label>
<input type="date" id="dob" tabIndex="4" class="form-control" name="dob" placeholder="Date of Birth">
</div>
</div>
<div class="col-md-4 col-4">
<div class="form-group choices" mandatory>
<label for="company-column" class="form-label choices">Gender:</label>
<div class="form-group">
<select class="choices form-select" tabIndex="5" id="gender" name="gender">
<option value="">SELECT</option>
<option value="MALE">MALE</option>
<option value="FEMALE">FEMALE</option>
<option value="NEUTER">NEUTER</option>
</select>
</div>
</div>
</div>
<div class="col-md-4 col-4">
<div class="form-group mandatory">
<label for="email-id-column" class="form-label">Contact No</label>
<input type="number" id="contactno" tabIndex="6" class="form-control" name="contactno" placeholder="Contact No" data-parsley-required="true">
</div>
</div>
<div class="col-md-4 col-4">
<div class="form-group ">
<label for="email-id-column" class="form-label">Father Name</label>
<input type="text" id="fathername" tabIndex="7" class="form-control" name="fathername" placeholder="Father Name" data-parsley-required="true">
</div>
</div>
<div class="col-md-4 col-4">
<div class="form-group ">
<label for="email-id-column" class="form-label">Husband Name</label>
<input type="text" id="husbandname" tabIndex="8" class="form-control" name="husbandname" placeholder="Husband Name" data-parsley-required="true">
</div>
</div>
<div class="col-md-4 col-4">
<div class="form-group mandatory">
<label for="email-id-column" class="form-label">CNIC</label>
<input type="number" id="cnic" class="form-control" tabIndex="9" name="cnic" placeholder="CNIC" data-parsley-required="true">
</div>
</div>
<div class="col-md-4 col-4">
<div class="form-group ">
<label for="email-id-column" class="form-label">City</label>
<input type="text" id="city" tabIndex="10" class="form-control" name="city" placeholder="City" data-parsley-required="true">
</div>
</div>
<div class="col-md-4 col-4">
<div class="form-group choices">
<label for="company-column" class="form-label choices">Marital Status</label>
<div class="form-group">
<select class="choices form-select" tabIndex="11" id="maritalstatus" name="maritalstatus">
<option value="">SELECT</option>
<option value="NOT SPECIFIED">NOT SPECIFIED</option>
<option value="SINGLE">SINGLE</option>
<option value="MARRIED">MARRIED</option>
<option value="WIDOW">WIDOW</option>
<option value="WIDOWER">WIDOWER</option>
<option value="DIVORCED">DIVORCED</option>
<option value="SEPARATED">SEPARATED</option>
<option value="UNKNOWN">UNKNOWN</option>
</select>
</div>
</div>
</div>
<div class="col-md-4 col-4">
<div class="form-group choices">
<label for="company-column" class="form-label choices">Blood Group</label>
<div class="form-group">
<select class="choices form-select" tabIndex="12" id="bloodgroup" name="bloodgroup">
<option value="">SELECT</option>
<option value="NOT SPECIFIED">NOT SPECIFIED</option>
<option value="A POSITIVE">A POSITIVE</option>
<option value="A NEGATIVE">A NEGATIVE</option>
<option value="B POSITIVE">B POSITIVE</option>
<option value="B NEGATIVE">B NEGATIVE</option>
<option value="O POSITIVE">O POSITIVE</option>
<option value="O NEGATIVE">O NEGATIVE</option>
<option value="AB POSITIVE">AB POSITIVE</option>
<option value="AB NEGATIVE">AB NEGATIVE</option>
</select>
</div>
</div>
</div>
<div class="col-md-4 col-4">
<div class="form-group mandatory">
<label for="email-id-column" class="form-label">Address</label>
<input type="text" id="address" class="form-control" tabIndex="13" name="address" placeholder="Address" data-parsley-required="true">
</div>
</div>
<div class="col-6 col-lg-3 col-md-6">
<div class="card-body px-2 py-3-4">
<div class="row">
<div class="col-md-4 col-lg-4 col-xl-8 col-xxl-5 d-flex justify-content-start ">
<div class="stats-icon purple mb-2">
<i class="iconly-boldShow"></i>
</div>
</div>
<div class="col-md-8 col-lg-12 col-xl-12 col-xxl-7">
<h6 class="text-muted font-semibold">Patient ID</h6>
<input class="font-extrabold mb-0" style="align-content: center; color:red; " type="number" name="patientid" id="patientid" disabled>
</div>
</div>
</div>
</div>
{{-- <label>Search Patient:</label>
<div class="col-lg-2">
<label>Patient ID:</label>
<input placeholder="Enter Patient ID" type="number" name="patientid"
id="selectpatientid" class="form-control form-control-sm d2">
</div>
<div class="col-lg-2">
<label>Patient CNIC:</label>
<input placeholder="Enter Patient CNIC" type="number" name="cnic"
id="selectpatientcnic" class="form-control form-control-sm d2">
</div>
</div> --}}
<div class="row">
<div class="col-12 d-flex justify-content-end">
<a id="save1" tabIndex="14" class="btn btn-primary me-1 mb-1">Register</a>
{{-- <button type="submit" class="btn btn-light-secondary me-1 mb-1">Register</button> --}}
<button type="reset" class="btn btn-light-secondary me-1 mb-1">Reset</button>
</div>
</div>
</form>

Try opening the F12 tools and heading over to the networking tab, under the networking tab you can double check the CSR under is getting pass along to the app under the 'headers' or 'request' once you've clicked on your jQuery request
You should also be able to see that larval responded with if you click 'response'
It might also be worth checking out the laravel.log under storage provider any more insight to the problem, this normally contains some help hints

Related

Undefined variable: UserRegistration (View: C:\xampp\htdocs\laravel\Form_submit\resources\views\registration.blade.php)

I am new in laravel 8 & I am trying to edit & show the value of that field in a form and i am able to show it in that field.
But when i am trying to add another data it is showing Undefined variable: UserRegistration.
My registration.blade.php code:
<form action="{{$url}}", method="POST">
#csrf
<div class="row">
<div class="col-md-6 mb-4">
<div class="form-outline">
<input type="text" id="form3Example1m" name="Fname"
class="form-control form-control-lg" value="{{$UserRegistration->Fname}}" />
<label class="form-label" for="form3Example1m">First name</label>
</div>
</div>
<div class="col-md-6 mb-4">
<div class="form-outline">
<input type="text" id="form3Example1n" name="Lname"
class="form-control form-control-lg" value="{{$UserRegistration->Lname}}" />
<label class="form-label" for="form3Example1n">Last name</label>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6 mb-4">
<div class="form-outline">
<input type="text" id="form3Example1m1" name="mother"
class="form-control form-control-lg" value="{{ $UserRegistration->mother}}" />
<label class="form-label" for="form3Example1m1">Mother's
name</label>
</div>
</div>
<div class="col-md-6 mb-4">
<div class="form-outline">
<input type="text" id="form3Example1n1" name="father"
class="form-control form-control-lg" value="{{$UserRegistration->father}}" />
<label class="form-label" for="form3Example1n1">Father's
name</label>
</div>
</div>
</div>
<div class="form-outline mb-4">
<input type="text" id="form3Example8" name="address" value="{{$UserRegistration->address}}" class="form-control form-control-lg" />
<label class="form-label" for="form3Example8">Address</label>
</div>
<div class="d-md-flex justify-content-start align-items-center mb-4 py-2">
<h6 class="mb-0 me-4">Gender: </h6>
<div class="form-check form-check-inline mb-0 me-4">
<input class="form-check-input" type="radio" name="gender"
id="femaleGender" value="M"{{$UserRegistration->gender=='M'?'checked':''}} />
<label class="form-check-label" for="femaleGender">Male</label>
</div>
<div class="form-check form-check-inline mb-0 me-4">
<input class="form-check-input" type="radio" name="gender"
id="maleGender" value="F" {{$UserRegistration->gender=='F'?'checked':''}} />
<label class="form-check-label" for="maleGender">Female</label>
</div>
<div class="form-check form-check-inline mb-0">
<input class="form-check-input" type="radio" name="gender"
id="otherGender" value="O" {{$UserRegistration->gender=='O'?'checked':''}} />
<label class="form-check-label" for="otherGender">Other</label>
</div>
</div>
<div class="row">
<h6 class="mb-0 me-4">State: </h6>
<div class="col-md-6 mb-4">
<select class="select" name="state">
<option value="">State</option>
<option value="Khulna" {{$UserRegistration->state=='Khulna' ? 'selected':''}}>Khulna</option>
<option value="Dhaka" {{$UserRegistration->state=='Dhaka' ? 'selected':''}}>Dhaka</option>
<option value="Dinajpur" {{$UserRegistration->state=='Dinajpur' ? 'selected':''}}>Dinajpur</option>
</select>
</div>
</div>
<div class="row">
<h6 class="mb-0 me-4">City: </h6>
<div class="col-md-6 mb-4">
{{-- <h6 class="mb-0 me-4">City: </h6> --}}
<select class="select" name="city">
<option value="">City</option>
<option value="Khulna" {{$UserRegistration->state=='Khulna' ? 'selected':''}}>Khulna</option>
<option value="Dhaka" {{$UserRegistration->state=='Dhaka' ? 'selected':''}}>Dhaka</option>
<option value="Dinajpur" {{$UserRegistration->state=='Dinajpur' ? 'selected':''}}>Dinajpur</option>
</select>
</div>
</div>
<div class="d-flex justify-content-end pt-3">
<button type="button" class="btn btn-light btn-lg mr-3">Reset all</button>
<button type="submit" name="submit" class="btn btn-warning btn-lg ms-2">{{$btntitle}}</button>
</div>
</form>
My controller UserSignup.php file:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\UserRegistration;
class UserSignup extends Controller
{
//
public function homePage(){
return view('home');
}
public function signup(){
$url=url('/usersignup');
$title="Student registration form";
$btntitle="Submit";
$data=compact('url','title','btntitle');
return view('registration')->with($data);
}
public function storeData(Request $request){
//insert into table
$UserRegistration=new UserRegistration;
$UserRegistration->Fname=$request['Fname'];
$UserRegistration->Lname=$request['Lname'];
$UserRegistration->mother=$request['mother'];
$UserRegistration->father=$request['father'];
$UserRegistration->address=$request['address'];
$UserRegistration->gender=$request['gender'];
$UserRegistration->state=$request['state'];
$UserRegistration->pincode=$request['pincode'];
$UserRegistration->course=$request['course'];
$UserRegistration->city=$request['city'];
$UserRegistration->email=$request['email'];
$UserRegistration->dob=$request['dob'];
$UserRegistration->save();
return redirect('/signup/view');
}
public function viewData(){
// show data
$UserRegistration=UserRegistration::all();
$data=compact('UserRegistration');
return view("customer-view")->with($data);
}
public function edit($id){
$UserRegistration=UserRegistration::find($id);
if (is_null($UserRegistration)) {
# not found
return redirect('/signup/view');
}else{
$url=url('/signup/update').'/'.$id;
$title="Update Student registration form";
$btntitle="Update";
$data=compact('UserRegistration','url','title','btntitle');
return view('registration')->with($data);
}
}
public function update($id,Request $request)
{
$UserRegistration=UserRegistration::find($id);
$UserRegistration->Fname=$request['Fname'];
$UserRegistration->Lname=$request['Lname'];
$UserRegistration->mother=$request['mother'];
$UserRegistration->father=$request['father'];
$UserRegistration->address=$request['address'];
$UserRegistration->gender=$request['gender'];
$UserRegistration->state=$request['state'];
$UserRegistration->pincode=$request['pincode'];
$UserRegistration->course=$request['course'];
$UserRegistration->city=$request['city'];
$UserRegistration->email=$request['email'];
$UserRegistration->dob=$request['dob'];
$UserRegistration->save();
return redirect('/signup/view');
}
}
web.php:
Route::get('/home',[UserSignup::class,'homePage']);
Route::get('/signup',[UserSignup::class,'signup']);
Route::post('/usersignup',[UserSignup::class,'storeData']);
Route::get('/signup/view',[UserSignup::class,'viewData']);
Route::get('/signup/delete/{id}',[UserSignup::class,'delete'])->name('customer.delete');
Route::get('/signup/edit/{id}',[UserSignup::class,'edit'])->name('customer.edit');
Route::post('/signup/update/{id}',[UserSignup::class,'update'])->name('customer.update');
Try This in your registration.blade.php because you are using same file for add and update both.
{{isset($UserRegistration) ? $UserRegistration->Fname : ''}}

even after submit of form my fields are not inserted into database using php

I am new to php. When form is submitted the data has to store on database. When I started to code it worked fine but later on data is not inserted into database I was trying to insert form details to database but unable to insert on regular basis. Sometimes,data is stored on database but most of the times, it doesn't store. so please help me....
php version:7
and MySQL database
form
<form role="form" class="register-form" method="post" action="aluminiregdb.php">
<center><h2>Alumni Registration</h2></center>
<hr class="colorgraph">
<div class="row">
<div class="col-xs-12 col-sm-6 col-md-6">
<div class="form-group">
<input type="text" name="first_name" id="first_name" class="form-control input-lg" placeholder="First Name" tabindex="1">
</div>
</div>
<div class="col-xs-12 col-sm-6 col-md-6">
<div class="form-group">
<input type="text" name="last_name" id="last_name" class="form-control input-lg" placeholder="Last Name" tabindex="2">
</div>
</div>
</div>
<div class="form-group">
<input type="number" name="roll_number" id="roll_number" class="form-control input-lg" placeholder="Roll Number" tabindex="3">
</div>
<div class="form-group">
<input type="email" name="email" id="email" class="form-control input-lg" placeholder="Email Address" tabindex="4">
</div>
<div class="row">
<div class="col-xs-12 col-lg-12 col-sm-6 col-md-6 ">
<select class="form-group form-control input-lg" name="SelectInstitution">
<option value="select">Select Institution</option>
<option value="sdes">Sree Dattha institute of Engineering and science (SDES)</option>
<option value="sdgi">Sree Dattha group of istitutions Integrated Campus (SDGI)</option>
<option value="sdip">Sree Dattha institute of Pharmacy (SDIP)</option>
<option value="sdbn">Sree Dattha Brindavan institute of Teacher Education (SDBN)</option>
<option value="sdbd">Sree Dattha Brindavan institute of Diploma (SDBD)</option>
<option value="bits">Brindavan institute of Teacher Education (BITS)</option>
</select>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-lg-12 col-sm-6 col-md-6 ">
<select class="form-group form-control input-lg" name="SelectInstitutiondegree">
<option value="select">Select Degree Obtained</option>
<option value="B.Tech">B.Tech</option>
<option value="M.Tech">M.Tech</option>
<option value="MBA">MBA</option>
<option value="Polytechnic">Polytechnic</option>
<option value="B.Pharm">B.Pharm</option>
<option value="M.Pharm">M.Pharm</option>
<option value="Pharm.D">Pharm.D</option>
<option value="B.Ed">B.Ed</option>
</select>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-6 col-md-12">
<div class="form-group">
<input type="text" name="program" id="program" class="form-control input-lg" placeholder="Program : CSE or ECE or B.Pharm or M.Pharm or B.Ed etc..." tabindex="5">
</div>
</div>
</div>
<div class="form-group">
<input type="number" name="year" id="year" min="2000" class="form-control input-lg" placeholder="Completion Year Of Graduation" tabindex="6">
</div>
<div class="form-group">
<input type="date" name="dob" id="dob" class="form-control input-lg" placeholder=" Date of Birth : 1994-07-23" tabindex="7">
</div>
<div class="form-group">
<input type="number" name="mobile" id="mobile" class="form-control input-lg" placeholder="Mobile Number" tabindex="8">
</div>
<div class="form-group">
<textarea class="form-control form-control input-lg" name="adress" rows="6" data-rule="required" data-msg="Please write something for us" placeholder="Address"></textarea>
<div class="validation"></div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-6 col-md-12">
<div class="form-group input-lg">
<label for="Status">Current Status :&nbsp&nbsp</label>
<label class="radio-inline">
<input type="radio" name="optradio" value="Working" onclick="doClick(this)">Working
</label>
<label class="radio-inline">
<input type="radio" name="optradio" value="Self-Employed" onclick="doClick(this)">Self-Employed
</label>
<label class="radio-inline">
<input type="radio" name="optradio" value="Studying" onclick="doClick(this)">Studying
</label>
<label class="radio-inline">
<input type="radio" name="optradio" value="Home Maker" onclick="doClick(this)">Home Maker
</label>
</div>
</div>
</div>
<div class="row" id="textbox" style="display:none">
<div class="col-xs-12 col-sm-6 col-md-12">
<div class="form-group">
<input type="text" name="org_name" id="org_name" class="form-control input-lg" placeholder="Organization Name" tabindex="9">
</div>
</div>
<div class="col-xs-12 col-sm-6 col-md-12">
<div class="form-group">
<input type="text" name="designatin" id="designation" class="form-control input-lg" placeholder="Designation in your Company" tabindex="10">
</div>
</div>
</div>
<hr class="colorgraph">
<div class="row">
<div class="col-xs-12 col-md-12"><input align="center" name="alumniregs" type="submit" value="Submit" class="btn btn-theme btn-block btn-lg" tabindex="11"></div>
</div>
</form>
dbconnection
if(isset($_POST['alumniregs'])){
if(isset($_POST['first_name'])){
$fname=$_POST['first_name'];
}
if(isset($_POST['last_name'])){
$lname=$_POST['last_name'];
}
if(isset($_POST['roll_number'])){
$roll=$_POST['roll_number'];
}
if(isset($_POST['email'])){
$email=$_POST['email'];
}
if(isset($_POST['SelectInstitution'])){
$institu=$_POST['SelectInstitution'];
}
if(isset($_POST['SelectInstitutiondegree'])){
$insttidegree=$_POST['SelectInstitutiondegree'];
}
if(isset($_POST['program'])){
$program=$_POST['program'];
}
if(isset($_POST['year'])){
$year=$_POST['year'];
}
if(isset($_POST['dob'])){
$dob=$_POST['dob'];
}
if(isset($_POST['mobile'])){
$mobile=$_POST['mobile'];
}
if(isset($_POST['adress'])){
$adress=$_POST['adress'];
}
if(isset($_POST['optradio'])){
$optradio=$_POST['optradio'];
}
if(isset($_POST['org_name'])){
$orgname=$_POST['org_name'];
}
if(isset($_POST['designatin'])){
$designatin=$_POST['designatin'];
}
$date = date('Y-m-d H:i:s');
$smt=$db->prepare("INSERT INTO `naik`.`aluminireg` (`fname`, `lname`, `rollno`, `email`, `Institution`, `degree`, `branch`, `year`, `dob`, `mobile`, `adress`, `presentstatus`, `orgname`, `designatin`,`date`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,'$date')");
$smt->bind_param('ssssssssssssss',$fname,$lname,$roll,$email,$institu,$insttidegree,$program,$year,$dob,$mobile,$adress,$optradio,$orgname,$designatin);
$smt->execute();
$smt->close();
if ($smt) {
echo "<script>alert('Item Entered Successful')</script>";
}
}
?>
config
<?php
$dbconnection=array(
'server'=>'localhost',
'user'=>'root',
'password'=>'java',
'dbname'=>'naik'
);
$db=new mysqli(
$dbconnection['server'],
$dbconnection['user'],
$dbconnection['password'],
$dbconnection['dbname']
);
//if($db!="null"){
// echo "<script>alert('connection Successful')</script>";
//}
if($db->connect_errno>0){
echo "database connection error".$db->connect_error;
exit;
}
//echo $db->host_info;
#echo $db->connect_errno;
?>

Unable to insert data into mysql but no error message shown

Was trying to make a student register page but the data won't insert into database and there was no error message shown so i'm not sure where the problem is. Any help is appreciated!
Here's the code:
<form onsubmit="return Add_Validate()" class="form-horizontal" action="AddStudent.php" method="POST">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title"><i class="fa fa-plus"></i> Add Student</h4>
</div>
<div class="modal-body">
<div class="form-group">
<label class="col-sm-3 control-label">Student Name </label>
<label class="col-sm-1 control-label">: </label>
<div class="col-sm-8">
<input type="text" class="form-control" id="Add_Student_Name" placeholder="Student Name" name="Student_Name1" autocomplete="off" required>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Gender </label>
<label class="col-sm-1 control-label">: </label>
<div class="col-sm-8">
<select class="form-control" id="Add_Student_Gender" name="Student_Gender1" required>
<option value="">~~SELECT~~</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
</div></div>
<div class="form-group">
<label class="col-sm-3 control-label">Address </label>
<label class="col-sm-1 control-label">: </label>
<div class="col-sm-8">
<input type="text" class="form-control" id="Add_Student_Address" placeholder="Address" name="Student_Address1" autocomplete="off" required>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Contact Number </label>
<label class="col-sm-1 control-label">: </label>
<div class="col-sm-8">
<input type="text" class="form-control" id="Add_Contact_Number" placeholder="Contact Number" name="Contact_Number1" autocomplete="off" required>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Email </label>
<label class="col-sm-1 control-label">: </label>
<div class="col-sm-8">
<input type="text" class="form-control" id="Add_Student_Email" placeholder="Email" name="Student_Email1" autocomplete="off" required>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Faculty </label>
<label class="col-sm-1 control-label">: </label>
<div class="col-sm-8">
<select class="form-control" name="Add_Faculty1" id="FacultyName" required>
<option value="">~~SELECT~~</option>
<?php $query_faculty="SELECT FacultyName FROM `faculty`";
$result_faculty = mysqli_query($connect, $query_faculty) or die(mysqli_error($connect));
while($row_faculty=mysqli_fetch_array($result_faculty)) { ?>
<option value="<?php echo $row_faculty['FacultyName'] ?>"><?php echo $row_faculty['FacultyName'] ?></option>
<?php } ?></select></div></div>
<div class="form-group">
<label class="col-sm-3 control-label">Username </label>
<label class="col-sm-1 control-label">: </label>
<div class="col-sm-8">
<input type="text" class="form-control" id="Add_Student_Username" placeholder="Username" name="Student_Username1" autocomplete="off" required>
</div></div>
<div class="form-group">
<label class="col-sm-3 control-label">Password </label>
<label class="col-sm-1 control-label">: </label>
<div class="col-sm-8">
<input type="password" class="form-control" id="Add_Password" placeholder="Password" name="Student_Password1" autocomplete="off" required>
</div>
</div>
And SQL command:
<?php require 'php_action/db_connect.php';
if($_POST) {
$Student_Name= $_POST['Student_Name1'];
$Student_Gender= $_POST['Student_Gender1'];
$Student_Address= $_POST['Student_Address1'];
$Contact_Number= $_POST['Contact_Number1'];
$Student_Email= $_POST['Student_Email1'];
$FacultyName= $_POST['Add_Faculty1'];
$Student_Username= $_POST['Student_Username1'];
$Password= $_POST['Student_Password1'];
$sql = "INSERT INTO student (Student_Name,Student_Gender,Student_Address,Contact_Number,Student_Email,FacultyName,Student_Username,Password) VALUES ('$Student_Name', '$Student_Gender','$Student_Address','$Contact_Number','$Student_Email','$FacultyName','$Student_Username','$Password')";
if($connect->query($sql) === TRUE) {
echo "<SCRIPT>alert('Student successfully added!');document.location='Student_Register.php'</SCRIPT>";
}
else {
echo "<SCRIPT>alert('Student add unsuccessful!');document.location='Student_Register.php'</SCRIPT>";
}
}
$connect->close();
?>
Found the error, seems to be wrong column name, sorry the all the trouble.....
Admin please close my question.
You're not getting an error message because you never get the error message from MySQL and show it in your alert. $connect->error will contain the error message, and you can add that to your alert like this:
else {
echo "<SCRIPT>alert('Student add unsuccessful! Reason: ' + " . json_encode($connect->error) . ");document.location='Student_Register.php'</SCRIPT>";
}

Update Database tables with values input by user in CodeIgniter using PHP

I'm trying to update one of my user tables in the Database with values taken from the the user. But for some reason it's not updating anything.
HTML Form
<form class="form-horizontal" method = "post">
<fieldset>
<!-- Form Name -->
<legend>User Details</legend>
<div>
<?php echo $this->session->flashdata('msg'); ?>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="name">Full Name</label>
<div class="col-md-8">
<input id="name" name="name" type="text" placeholder="something" class="form-control input-md">
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="dob">Date of Birth</label>
<div class="col-md-4">
<input id="dob" name="dob" type="text" placeholder="" class="form-control input-md">
</div>
</div>
<!-- Multiple Radios -->
<div class="form-group">
<label class="col-md-4 control-label" for="gender">Gender</label>
<div class="col-md-2">
<select id="gender" name="gender" class="form-control">
<option value="1">Male</option>
<option value="2">Female</option>
</select>
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="degree">Degree</label>
<div class="col-md-8">
<input id="degree" name="degree" type="text" placeholder="degree" class="form-control input-md">
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="specialization">Specialization</label>
<div class="col-md-8">
<input id="specialization" name="specialization" type="text" placeholder="specialization" class="form-control input-md">
</div>
</div>
<!-- Select Basic -->
<div class="form-group">
<label class="col-md-4 control-label" for="year">Degree Year</label>
<div class="col-md-2">
<select id="year" name="year" class="form-control">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</div>
</div>
<!-- Select Basic -->
<div class="form-group">
<label class="col-md-4 control-label" for="semester">Semester</label>
<div class="col-md-2">
<select id="semester" name="semester" class="form-control">
<option value="1">1</option>
<option value="2">2</option>
</select>
</div>
</div>
<!-- File Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="filebutton">Upload Profile Picture</label>
<div class="col-md-4">
<input id="filebutton" name="filebutton" class="input-file" type="file">
</div>
</div>
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="submit"></label>
<div class="col-md-4">
<a href="<?php echo base_url("/index.php/studentDashboardController/saveUserDetails"); ?>" >
<button id="submit" name="submit" class="btn btn-primary">Save Changes</button>
</a>
</div>
</div>
</fieldset>
studentDashboardController
public function saveUserDetails()
{
$this->load->model('userModel');
if (isset($this->session->userdata['logged_in']))
{
$username = ($this->session->userdata['logged_in']['username']);
}
$user = $this-> userModel-> getUserUid($username); //Gets the uid of the current user
$this->userModel->saveUserDetails($user);
$this->session->set_flashdata('msg', '<div class="alert alert-success text-center">Successfully Inserted Data</div>');
$this->load->view('studentDashboard/common',$data);
redirect(base_url('index.php/studentDashboard/editProfile',$data1));
}
userModel
public function saveUserDetails($uid)
{
$data = array(
'name' => $this->input->post('name')
);
$this->db->where("uid",$uid);
$this->db->update("sysuser",$data);
}
The sysuser has the uid and name fields.I' not sure what I'm doing wrong here. Any help in this regard will be appreciated
You dont send your post data to controller. Change $this->userModel->saveUserDetails($user); as
$this->userModel->saveUserDetails($user,$this->input->post('name'));
Now Model should be like
public function saveUserDetails($uid,$name)
{
$data = array(
'name' => $name
);
$this->db->where("uid",$uid);
$this->db->update("sysuser",$data);
}
and make sure you have set correctly form action like
<form class="form-horizontal" method="post" action="<?php echo site_url('studentDashboardController/saveUserDetails');?>">

how to insert multi select drop down data into the database in laravel 5.2

In my form i created multiselect drop down list. i need to store the data which i selected from my multi select drop down to database, table name devicelist along with some value that is taken from my form.
my view page is
#extends('app')
#section('content')
<br><br><br><br><br>
<div class="templatemo-content-wrapper">
<div class="container">
<ol class="breadcrumb">
<li><font color="green">Home</font></li>
<li class="active">View/Edit Vehicle</li>
</ol>
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-success">
<div class="panel-heading">View/Edit Vehicle Information</div>
<div class="panel-body">
#if (count($errors) > 0)
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.<br><br>
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<form class="form-horizontal" role="form" method="POST" action="{{ url('vehicle/update/') }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
#foreach($devices as $device)
<div class="form-group">
<label class="col-md-4 control-label">Vehicle ID</label>
<div class="col-md-6">
<input type="text" class="form-control" name="deviceID" value="{{ ($device->deviceID)}}">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Creation date</label>
<div class="col-md-6">
<input type="text" class="form-control" name="creationTime" value="{{ date('Y/m/d H:i:s',($device->creationTime))}}">
</div>
</div>
<!--<div class="form-group">
<label class="col-md-4 control-label">Server ID</label>
<div class="col-md-6">
<input type="text" class="form-control" name="userID" value="" placeholder="Enter User ID">
</div>
</div> -->
<div class="form-group">
<label class="col-md-4 control-label">Unique ID</label>
<div class="col-md-6">
<input type="text" class="form-control" name="uniqueID" value="{{ ($device->uniqueID)}}" placeholder="Enter Unique ID">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Active</label>
<div class="col-md-6">
<select class="form-control" value="{{ ($device->isActive) }}" name="isActive" >
<option value="1">Yes</option>
<option value="0">No</option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Vehicle Description</label>
<div class="col-md-6">
<input type="text" class="form-control" name="description" value="{{ ($device->description) }}" placeholder="Enter the description">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Short Name</label>
<div class="col-md-6">
<input type="text" class="form-control" name="displayName" value="{{ ($device->displayName) }}" placeholder="Enter Display Name">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Vehicle ID</label>
<div class="col-md-6">
<input type="text" class="form-control" name="vehicleID" value="{{ ($device->vehicleID) }}" placeholder="Enter Vehicle ID">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">License Plate</label>
<div class="col-md-6">
<input type="text" class="form-control" name="licensePlate" value="{{ ($device->licensePlate) }}" placeholder="Enter license Plate">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">License Expiration</label>
<div class="col-md-6">
<input type="date" class="form-control" name="licenseExpire" value="{{ ($device->licenseExpire) }}" placeholder="Enter license Expire Date">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Equipment Type</label>
<div class="col-md-6">
<input type="email" class="form-control" name="equipmentType" value="{{ ($device->equipmentType) }}" placeholder="Enter E-Mail Address">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Equipment Status</label>
<div class="col-md-6">
<select class="form-control" value="{{ ($device->equipmentStatus) }}" name="equipmentStatus" >
<option>Unspecified</option>
<option value="inservice">In Service</option>
<option value="rented">Rented</option>
<option value="pending">Pending</option>
<option value="completed">Completed</option>
<option value="available">Available</option>
<option value="unavailable">Unavailable</option>
<option value="repair">Repair</option>
<option value="retired">Retired</option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">IMEI/EDN Number</label>
<div class="col-md-6">
<input type="email" class="form-control" name="imeiNumber" value="{{ ($device->imeiNumber) }}" placeholder="Enter IMEI/EDN Number">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Serial Number</label>
<div class="col-md-6">
<input type="email" class="form-control" name="serialNumber" value="{{ ($device->serialNumber) }}" placeholder="Enter Serial Number">
</div>
</div>
<!-- <div class="form-group">
<label class="col-md-4 control-label">Data Key</label>
<div class="col-md-6">
<input type="email" class="form-control" name="notifyEmail" value="" placeholder="Enter E-Mail Address">
</div>
</div> -->
<div class="form-group">
<label class="col-md-4 control-label">SIM Phone</label>
<div class="col-md-6">
<input type="email" class="form-control" name="simPhoneNumber" value="{{ ($device->simPhoneNumber) }}" placeholder="Enter SIM Phone Number">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">SMS Email Address</label>
<div class="col-md-6">
<input type="email" class="form-control" name="smsEmail" value="{{ ($device->smsEmail) }}" placeholder="Enter SMS E-Mail Address">
</div>
</div>
<!-- <div class="form-group">
<label class="col-md-4 control-label">Group Pushpin ID</label>
<div class="col-md-6">
<input type="email" class="form-control" name="notifyEmail" value="" placeholder="Enter E-Mail Address">
</div>
</div> -->
<div class="form-group">
<label class="col-md-4 control-label">Map Route Color</label>
<div class="col-md-6">
<select class="form-control" value="{{ ($device->displayColor) }}" name="displayColor" >
<option>Default</option>
<option value="#000000">Black</option>
<option value="#a52a2a">Brown</option>
<option value="#dd0000">Red</option>
<option value="#b37400">Orange</option>
<option value="#008f00">Green</option>
<option value="#0000ee">Blue</option>
<option value="#9400d3">Purple</option>
<option value="#505050">Grey</option>
<option value="#00b3b3">Cyan</option>
<option value="#ff1493">Pink</option>
<option value="none">None</option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Fuel Capacity</label>
<div class="col-md-6">
<input type="text" class="form-control" name="fuelCapacity" value="{{ ($device->fuelCapacity) }}" >
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Driver ID</label>
<div class="col-md-6">
<input type="email" class="form-control" name="driverID" value="{{ ($device->driverID) }}">
</div>
</div>
<!-- <div class="form-group">
<label class="col-md-4 control-label">Reported Odometer</label>
<div class="col-md-6">
<input type="email" class="form-control" name="notifyEmail" value="" placeholder="Enter E-Mail Address">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Reported Engine Hours</label>
<div class="col-md-6">
<input type="email" class="form-control" name="notifyEmail" value="" placeholder="Enter E-Mail Address">
</div>
</div> -->
<div class="form-group">
<label class="col-md-4 control-label">Group Membership</label>
<div class="col-md-6">
{{--<select class="form-control" value="{{ old('groupID') }}" name="groupID" multiple >--}}
{{--#foreach( $grouplist as $group)--}}
{{--#if ($group->groupID == old('description'))--}}
{{--<option value="{{ $group->groupID }}" >{{ $group->groupID." ".'['.$group->description.']' }}</option>--}}
{{--#else--}}
{{--<option value="{{ $group->groupID }}" >{{ $group->groupID." ".'['.$group->description.']' }}</option>--}}
{{--#endif--}}
{{--#endforeach--}}
{{--</select>--}}
<script type="text/javascript">
$(".js-example-basic-multiple").select2();
</script>
<select class="js-example-basic-multiple js-states form-control" id="id_label_multiple" value="{{ old('groupID[]') }}" name="groupID" multiple="multiple" >
#foreach( $grouplist as $group)
#if ($group->groupID == old('description'))
<option value="{{ $group->groupID }}" >{{ $group->groupID." ".'['.$group->description.']' }}</option>
#else
<option value="{{ $group->groupID }}" >{{ $group->groupID." ".'['.$group->description.']' }}</option>
#endif
#endforeach
</select>
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-warning">
Update
</button>
</div>
</div>
#endforeach
</form>
</div>
</div>
</div>
</div>
</div>
</div>
#endsection
and my controller page is
public function update(Request $request)
{
try {
$postUser = Input::all();
//insert data into mysql table
$account = Account::select('accountID')->where('accountID', '=', 'gts')->get();
foreach ($account as $acc) {
$abc = $acc->accountID;
}
$data=DB::table('device')
->where("deviceID",$request['deviceID'])
->update(array(
"uniqueID"=>$request['uniqueID'],
"isActive"=>$request['isActive'],
"description"=>$request['description'],
"displayName"=>$request['displayName'],
"vehicleID"=>$request['vehicleID'],
"licensePlate"=>$request['licensePlate'],
"licenseExpire"=>$request['licenseExpire'],
"equipmentType"=>$request['equipmentType'],
"equipmentStatus"=>$request['equipmentStatus'],
"imeiNumber"=>$request['imeiNumber'],
"serialNumber"=>$request['serialNumber'],
"simPhoneNumber"=>$request['simPhoneNumber'],
"smsEmail"=>$request['smsEmail'],
"displayColor"=>$request['displayColor'],
"fuelCapacity"=>$request['fuelCapacity'],
"driverID"=>$request['driverID'],
"lastUpdateTime"=>time()));
$data = array("accountID" => $abc,
"deviceID"=> $request['deviceID'],
"groupID"=>$request['groupID[]'],
"lastUpdateTime"=>time(),
"creationTime"=>time());
$ck = 0;
$ck = DB::table('devicelist')->Insert($data);
$devices = DB::table('device')->simplePaginate(10);
return view('vehicle.vehicleAdmin')->with('devices', $devices);
} catch (ModelNotFoundException $err) {
//Show error page
}
}
}
how can i write the function to store the data from multiselect drop down to my table each value in each row along with other value. Can anyone tell me what should i do? and how to write "string to array" conversion and "for loop" for the insertion
If you are getting array from multi select and want to insert it in one row. You should implode array into comma separated string then insert it in table
Implode function
$val = implode(",",$request['groupID[]']);
Then put $val into your $data array like
$data = array("accountID" => $abc,
"deviceID"=> $request['deviceID'],
"groupID"=>$val,
"lastUpdateTime"=>time(),
"creationTime"=>time());
If you are getting string then explode it
Explode function
$val = explode(",",$request['groupID[]']);
$count = count($val);
for($a=0; $a<$count; $a++){
$data = array("accountID" => $abc,
"deviceID"=> $request['deviceID'],
"groupID"=>$val[$a],
"lastUpdateTime"=>time(),
"creationTime"=>time());
}

Categories