When I submit my form I can see in the error that all my values are passed except 'user_id':
('insert into `products` (`title`, `body`, `address`, `condition`, `quantity`, `price`, `bid_price`, `img_1`, `updated_at`, `created_at`)
But there's missing 'user_id', why ? This is my Product.php
protected $fillable = ['slug', 'title', 'body', 'user_id', 'price', 'bid_price', 'address', 'condition', 'quantity', 'img_1', 'img_2', 'img_3', 'img_4', 'views'];
The function in controller:
public function storeAuction(AuctionSubmitRequest $request)
{
$product = Product::create($request->all());
Auth::user()->product()->save($product);
$request->file('img_1')->move(public_path('images'), $request->file('img_1')->getClientOriginalName());
$data = $request->except(['img_1']);
$data['img_1'] = public_path('images') . '/' . $request->file('img_1')->getClientOriginalName();
$product->create($data);
return redirect('/');
}
My form:
<form id="tryitForm" class="form-horizontal" method="POST" action="/paskelbti-nauja-aukciona" enctype="multipart/form-data">
{!! csrf_field() !!}
<div class="form-group">
<label class="col-md-3 control-label">Prekės pavadinimas</label>
<div class="col-md-4">
<input type="text" class="form-control" name="title" />
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Prekės aprašymas</label>
<div class="col-md-4">
<textarea name="body" class="form-control"></textarea>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Adresas</label>
<div class="col-md-6">
<input type="text" class="form-control" name="address" />
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Pristatymas</label>
<div class="col-md-6">
<div class="radio">
<label><input type="radio" name="dispatch" value="kurjeris" /> Kurjeriu</label>
</div>
<div class="radio">
<label><input type="radio" name="dispatch" value="pastas" /> Paštu</label>
</div>
<div class="radio">
<label><input type="radio" name="dispatch" value="kita" /> Kita (parašysiu "prekės aprašyme")</label>
</div>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Prekės būklė</label>
<div class="col-md-6">
<div class="radio">
<label><input type="radio" name="condition" /> Nauja</label>
</div>
<div class="radio">
<label><input type="radio" name="condition" /> Labai gera</label>
</div>
<div class="radio">
<label><input type="radio" name="condition" /> Gera</label>
</div>
<div class="radio">
<label><input type="radio" name="condition" /> Panaudota</label>
</div>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Barkodas (jei turite)</label>
<div class="col-md-4">
<input class="form-control" name="barcode" type="number" min="0" />
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Prekių kiekis</label>
<div class="col-md-2">
<input class="form-control" name="quantity" type="number" min="1" />
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Prekės nuotraukos</label>
<div class="col-md-6">
<input type="file" class="form-control" name="img_1" /><br>
<input type="file" class="form-control" name="img_2" /><br>
<input type="file" class="form-control" name="img_3" /><br>
<input type="file" class="form-control" name="img_4" />
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Prekės kaina</label>
<div class="col-md-2">
<input class="form-control" name="price" type="number" step="0.01" min="0.01" />
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Minimali siūlymo suma</label>
<div class="col-md-2">
<input class="form-control" name="bid_price" type="number" step="0.01" min="0.01" />
</div>
</div>
<div class="form-group">
<div class="col-md-offset-3 col-md-8">
<button type="submit" class="btn btn-primary">Pradėti aukcioną</button>
</div>
</div>
</form>
You may try something like the following.
public function storeAuction(AuctionSubmitRequest $request)
{
$request->file('img_1')->move(public_path('images'), $request->file('img_1')->getClientOriginalName());
// first create the related model, do not save or create
// because you will get a 'Cannot add or update a child row'
$product = new Product();
$product->property1 = $request->get('property1');
$product->property2 = $request->get('property2');
// ...
$product->img_1 = public_path('images') . '/' . $request->file('img_1')->getClientOriginalName();
// here you save a product associated to the user.
Auth::user()->product()->save($product);
return redirect('/');
}
Related
How i can checked radio button inside echo method ?
<?php
if(isset($_GET['sid'])){
echo'
<div class="right_col" role="main">';
$a=$_GET['sid'];
$update = $obj->selectdata($a);
foreach($update as $upd)
{
$upd['sname'];
echo'
<h1 class="text-center">update student</h1>
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-8">
<form class="form-horizontal" action="dashboard.php?sid='.$upd['id'].'" method="POST" enctype="multipart/form-data">
<div class="form-group">
<label>Name</label>
<input type="text" value="'.$upd['sname'].'" name="sname" required class="form-control"/>
</div>
<div class="form-group">
<label>Father Name</label>
<input type="text" value="'.$upd['sfname'].'" name="sfname" required class="form-control"/>
</div>
<div class="form-group">
<label>Email</label>
<input type="email" value="'.$upd['email'].'" name="semail" required class="form-control"/>
</div>
<div class="form-group">
<label>Password</label>
<input type="password" name="spass" placeholder="Change Password" required class="form-control"/>
</div>
<div class="form-group">
<label>Date Of Birth</label>
<input type="date" value="'.$upd['dob'].'" name="sdob" required class="form-control"/>
</div>
<div class="form-group">
<label>Gender</label>
<div class="radio-inline">
<label><input type="radio" name="sgender" if($s_gender == Male){echo checked=checked} value="Male" />Male</label>
</div>
<div class="radio-inline">
<label><input type="radio" name="sgender" if($s_gender == Female){echo checked=checked} value ="Female" />Female</label>
</div></div>
<div class="form-group">
<label>Contact</label>
<input type="text" name="scontact" value="'.$upd['contact'].'" required class="form-control"/>
</div>
<div class="form-group">
<label>Address</label>
<input type="text" name="saddress" value="'.$upd['address'].'" required class="form-control"/>
</div>
<div class="form-group">
<label>Photo</label>
<input type="file" name="spic" required class="form-control"/>
</div>
<button type="submit" name="upt_form" class="btn btn-default">Submit</button>
</form>
</div>
</div>';
}
}
else {
echo'
<div class="right_col" role="main">
<h1 class="text-center">add student</h1>
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-8">
<form class="form-horizontal" action="dashboard.php" method="POST" enctype="multipart/form-data">
<div class="form-group">
<label>Name</label>
<input type="text" name="sname" required class="form-control"/>
</div>
<div class="form-group">
<label>Father Name</label>
<input type="text" name="sfname" required class="form-control"/>
</div>
<div class="form-group">
<label>Email</label>
<input type="email" name="semail" required class="form-control"/>
</div>
<div class="form-group">
<label>Password</label>
<input type="password" name="spass" required class="form-control"/>
</div>
<div class="form-group">
<label>Date Of Birth</label>
<input type="date" name="sdob" required class="form-control"/>
</div>
<div class="form-group">
<label>Gender</label>
<div class="radio-inline">
<label><input type="radio" name="sgender" value="Male" required/>Male</label>
</div>
<div class="radio-inline">
<label><input type="radio" name="sgender" value ="Female"required />Female</label>
</div></div>
<div class="form-group">
<label>Contact</label>
<input type="text" name="scontact" required class="form-control"/>
</div>
<div class="form-group">
<label>Address</label>
<input type="text" name="saddress" required class="form-control"/>
</div>
<div class="form-group">
<label>Photo</label>
<input type="file" name="spic" required class="form-control"/>
</div>
<button type="submit" name="stu_form" class="btn btn-default">Submit</button>
</form>
</div>
</div>';
}
?>
Work like this:
<input type="radio" '.($s_gender == Female ? 'checked="checked"' : "").' name="sgender" value ="Female" />
<?php
if(isset($_GET['sid'])){
echo'
<div class="right_col" role="main">';
$a=$_GET['sid'];
$update = $obj->selectdata($a);
foreach($update as $upd)
{
$upd['sname'];
echo'
<h1 class="text-center">update student</h1>
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-8">
<form class="form-horizontal" action="dashboard.php?sid='.$upd['id'].'" method="POST" enctype="multipart/form-data">
<div class="form-group">
<label>Name</label>
<input type="text" value="'.$upd['sname'].'" name="sname" required class="form-control"/>
</div>
<div class="form-group">
<label>Father Name</label>
<input type="text" value="'.$upd['sfname'].'" name="sfname" required class="form-control"/>
</div>
<div class="form-group">
<label>Email</label>
<input type="email" value="'.$upd['email'].'" name="semail" required class="form-control"/>
</div>
<div class="form-group">
<label>Password</label>
<input type="password" name="spass" placeholder="Change Password" required class="form-control"/>
</div>
<div class="form-group">
<label>Date Of Birth</label>
<input type="date" value="'.$upd['dob'].'" name="sdob" required class="form-control"/>
</div>
<div class="form-group">
<label>Gender</label>
<div class="radio-inline">
if($upd['gender']== "Male") {
echo '
<label><input cheacked= "cheacked" type="radio" name="sgender" value="Male" />Male</label>
</div>';
}
else {
echo' <div class="radio-inline">
<label><input type="radio" cheacked= "cheacked" name="sgender" value ="Female" />Female</label>
</div></div>';
}
<div class="form-group">
<label>Contact</label>
<input type="text" name="scontact" value="'.$upd['contact'].'" required class="form-control"/>
</div>
<div class="form-group">
<label>Address</label>
<input type="text" name="saddress" value="'.$upd['address'].'" required class="form-control"/>
</div>
<div class="form-group">
<label>Photo</label>
<input type="file" name="spic" required class="form-control"/>
</div>
<button type="submit" name="upt_form" class="btn btn-default">Submit</button>
</form>
</div>
</div>';
}
}
else {
echo'
<div class="right_col" role="main">
<h1 class="text-center">add student</h1>
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-8">
<form class="form-horizontal" action="dashboard.php" method="POST" enctype="multipart/form-data">
<div class="form-group">
<label>Name</label>
<input type="text" name="sname" required class="form-control"/>
</div>
<div class="form-group">
<label>Father Name</label>
<input type="text" name="sfname" required class="form-control"/>
</div>
<div class="form-group">
<label>Email</label>
<input type="email" name="semail" required class="form-control"/>
</div>
<div class="form-group">
<label>Password</label>
<input type="password" name="spass" required class="form-control"/>
</div>
<div class="form-group">
<label>Date Of Birth</label>
<input type="date" name="sdob" required class="form-control"/>
</div>
<div class="form-group">
<label>Gender</label>
<div class="radio-inline">
<label><input type="radio" name="sgender" value="Male" required/>Male</label>
</div>
<div class="radio-inline">
<label><input type="radio" name="sgender" value ="Female"required />Female</label>
</div></div>
<div class="form-group">
<label>Contact</label>
<input type="text" name="scontact" required class="form-control"/>
</div>
<div class="form-group">
<label>Address</label>
<input type="text" name="saddress" required class="form-control"/>
</div>
<div class="form-group">
<label>Photo</label>
<input type="file" name="spic" required class="form-control"/>
</div>
<button type="submit" name="stu_form" class="btn btn-default">Submit</button>
</form>
</div>
</div>';
}
?>
use this code then let me know if it resolved your issue, you had to put if and else with your problem in proper way you used wrong variable for get gender value.
I have this error in controller
A PHP Error was encountered
Severity: Notice
Message: Undefined property: dashboardController::$upload
Filename: controllers/dashboardController.php
Line Number: 70
Call to a member function data() on a non-object in
C:\xampp\htdocs\High_tack\application\controllers\dashboardController.php
on line 71
just do as i done it in my project using wamp and codeigniter. I uploaded my pictures directly into the folder named uploads in the project folder.
here is my controller named addproduct,
<?php class Addproduct extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('form', 'url'));
}
public function index(){
$this->load->view('addproduct');
}
function save() {
$name_file = $_FILES['pro_filename']['name'];
//$user=$this->session->userdata['logged_in'];
$data=array (
'pro_name'=>$this->input->post('pro_name'),
'pro_code'=>$this->input->post('pro_code'),
'hsn_code'=>$this->input->post('hsn_code'),
'pro_price'=>$this->input->post('pro_price'),
'pro_tax1'=>$this->input->post('pro_tax1'),
'pro_tax2'=>$this->input->post('pro_tax2'),
'pro_tax3'=>$this->input->post('pro_tax3'),
'pro_tax4'=>$this->input->post('pro_tax4'),
'pro_description'=>$this->input->post('pro_description'),
'pro_brand'=>$this->input->post('pro_brand'),
'pro_category'=>$this->input->post('pro_category'),
'pro_scategory'=>$this->input->post('pro_scategory'),
'pro_sscategory'=>$this->input->post('pro_sscategory'),
'pro_qauntity'=>$this->input->post('pro_qauntity'),
'pro_service'=>$this->input->post('pro_service'),
'pro_certificate'=>$this->input->post('pro_certificate'),
'pro_filename'=>$name_file,
'pro_date'=>$this->input->post('pro_date')
);
$this->load->model('base_model');
$this->base_model->save('addproduct',$data);
$this->do_upload();
$this->load->view('addproduct');
}
public function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|gif|jpeg';
$config['max_size'] = 500000;
$config['max_width'] = 200000;
$config['max_height'] = 200000;
$this->load->library('upload', $config);
$this->upload->do_upload('pro_filename');
}
}
here is my view page named addproduct
<div class="panel-body">
<div class="form">
<div class="col-md-12 sign-up text-left">
<div class="form-group ">
<label for="pro_name" class="control-label col-lg-3">Product Name</label>
<div class="col-lg-6">
<input class="form-control " id="pro_name" name="pro_name" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_code" class="control-label col-lg-3">Product Code</label>
<div class="col-lg-6">
<input class="form-control " id="pro_code" name="pro_code" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="hsn_code" class="control-label col-lg-3">HSN Code</label>
<div class="col-lg-6">
<input class="form-control " id="hsn_code" name="hsn_code" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_price" class="control-label col-lg-3">Prize</label>
<div class="col-lg-6">
<input class="form-control " id="pro_price" name="pro_price" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_tax1" class="control-label col-lg-3">Tax 1</label>
<div class="col-lg-6">
<input class="form-control " id="pro_tax1" name="pro_tax1" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_tax2" class="control-label col-lg-3">Tax 2</label>
<div class="col-lg-6">
<input class="form-control " id="pro_tax2" name="pro_tax2" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_tax3" class="control-label col-lg-3">Tax 3</label>
<div class="col-lg-6">
<input class="form-control " id="pro_tax3" name="pro_tax3" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_tax4" class="control-label col-lg-3">Tax 4</label>
<div class="col-lg-6">
<input class="form-control " id="pro_tax4" name="pro_tax4" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_description" class="control-label col-lg-3">Description</label>
<div class="col-lg-6">
<input class="form-control " id="pro_description" name="pro_description" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_brand" class="control-label col-lg-3">Brand</label>
<div class="col-lg-6">
<input class="form-control " id="pro_brand" name="pro_brand" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_category" class="control-label col-lg-3">Category</label>
<div class="col-lg-6">
<input class="form-control " id="pro_category" name="pro_category" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_scategory" class="control-label col-lg-3">Sub Category</label>
<div class="col-lg-6">
<input class="form-control " id="pro_scategory" name="pro_scategory" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_sscategory" class="control-label col-lg-3">S-Sub Category</label>
<div class="col-lg-6">
<input class="form-control " id="pro_sscategory" name="pro_sscategory" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_quantity" class="control-label col-lg-3">Qauntity</label>
<div class="col-lg-6">
<input class="form-control " id="pro_quantity" name="pro_quantity" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_service" class="control-label col-lg-3">Service Alert</label>
<div class="col-lg-6">
<input class="form-control " id="pro_service" name="pro_service" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_certificate" class="control-label col-lg-3">Certificate Alert</label>
<div class="col-lg-6">
<input class="form-control " id="pro_certificate" name="pro_certificate" type="text" />
</div>
</div><br><br>
<div class="form-group ">
<label for="pro_filename" class="control-label col-lg-3">Photo:</label>
<div class="col-lg-6">
<input class="form-control" type="file" name="pro_filename" required/>
</div>
</div><br><br>
<div class="clearfix"></div>
<?php
$today = date("Y-m-d");
?>
<input type="hidden" name="pro_date" value="<?php echo $today ?>">
<div class="form-group">
<div class="col-lg-offset-3 col-lg-6">
<button class="btn btn-primary" type="submit">Save</button>
<button class="btn btn-default" >Cancel</button>
</div>
</div>
<br>
</div>
<br>
</div>
</div>
this is my code in model named base_model
public function save($table, $data) {
return $this->db->insert($table, $data);
}
delete the portion you dont want, if you dont need to upload the image to folder simply change the controller field where the upload occurs
'pro_filename'=>$this->input->post('pro_filename'),
I am working on codeigniter project in which viwe page having textboxes, radio buttons and button.I fetched textbox values from database and manually enter from date and to date then by clicking on save button insert query gets empty values because in controller i called all functions serially in index function.What is the best solution to call controller methods on each event of view page?
leave_appl_view.php
<form action="leave_apply_control/index" method="post" id="myForm">
<div class="row">
<div class="col-md-3">
<div class="form-group label-floating">
<label class="control-label">Emp ID</label>
<?php if(!empty($emp_leave))
{
foreach ($emp_leave as $liv)
{ ?>
<input type="text" class="form-control" name="id" value="<?php echo $liv->employee_id ?>" >
</div>
</div>
<div class="col-md-4">
<div class="form-group label-floating">
<label class="control-label">Employee Name</label>
<input type="text" class="form-control" name="empname" id="empname" value="<?php echo $liv->employee_name ?>" >
</div>
</div>
<div class="col-md-4">
<div class="form-group label-floating">
<label class="control-label">Email address</label>
<input type="email" class="form-control" name="empmail" id="empmail" value="<?php echo $liv->email ?>" >
</div>
</div>
</div>
<div class="row">
<div class="col-md-4">
<div class="form-group label-floating">
<label class="control-label">Leave type: </label>
<input type="radio" id="half" name="type" onclick="Checkradiobutton()" value="half"> Half Day
<input type="radio" id="full" name="type" onclick="Checkbutton()" value="full" Checked> Full Day
</div>
</div>
<div class="col-md-3">
<div class="form-group label-floating" id="frm_dt">
<label class="control-label">From Date: </label>
<input type="text" class="form-control" id="datepicker1" name="from" ></p>
</div>
</div>
<div class="col-md-3">
<div class="form-group label-floating" id="to_dt">
<label class="control-label">To Date: </label>
<input type="text" class="form-control" id="datepicker2" name="to" ></p>
</div>
</div>
</div>
<div class="row">
<div class="col-md-3">
<div class="form-group label-floating">
<label class="control-label">Balance Leaves</label>
<input type="text" class="form-control" name="bal_leave" value="<?php echo $liv->balanced_leaves ?>" >
</div>
<?php }
}
?>
</div>
<div class="col-md-3">
<div class="form-group label-floating">
<label class="control-label">Applied Leaves</label>
<input type="text" class="form-control" name="totaldays" id="totaldays" readonly>
</div>
</div>
<div class="col-md-3">
<div class="form-group label-floating">
<label class="control-label">Applied Date</label>
<input type="text" class="form-control" name="todayDate" id="todayDate" value="<?php echo date("d/m/Y"); ?>" readonly>
</div>
</div>
</div>
<div class="row">
<div class="col-md-7">
<div class="form-group label-floating">
<label class="control-label">Reason </label>
<TEXTAREA class="form-control" id="reason" name="reason"> </TEXTAREA>
</div>
</div>
</div>
<button type="submit" class="btn btn-primary" id="submit" name="submit" onclick="<?php // echo base_url() ?>leave_apply_control/save_data">Apply</button>
<button type="reset" class="btn btn-primary" onclick="<?php echo base_url();?>leave_apply_control" >Reset</button>
</form>
leave_Apply_control.php
<?php
class leave_apply_control extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper(array('form','url'));
$this->load->library(array('session', 'form_validation'));
$this->load->library('calendar');
$this->load->database();
$this->load->model('user_model');
$this->load->helper('date');
}//contruct close
function index()
{
$id=$this->session->userdata('id');
$data['foren_id'] = $this -> user_model -> get_emp_id($id);
foreach($data['foren_id'] as $fk)
{
//echo "////control page fk is ".$fk->emp_id;
}
$data2['emp_leave'] = $this->user_model->get_leave_count($fk->emp_id);
$this->load->view('leave_application',$data2);
$this->save_data();
}
public function save_data()
{
$fd = date('Y-m-d',strtotime($this->input->post('from')));
$td = date('Y-m-d',strtotime($this->input->post('to')));
$data = array(
'employee_id' => $this->input->post('id'),
'emp_name' => $this->input->post('empname'),
'leave_from' => $fd,
'leave_to' => $td,
'no_of_days' => $this->input->post('totaldays'),
'applied_date' => date('Y-m-d H:i:s'),
'reason' => $this->input->post('reason'),
'mail' => $this->input->post('empmail'),
'leave_type' => $this->input->post('type'),
'balance_leaves' => $this->input->post('bal_leave'),
'status' => 1,
);
$this->user_model->insert_leave($data);
}
}
i am trying to get connected to database .html page doesnt get connected to addtodatabase.php which has many feilds like radio button,checkbox,textarea, input field .Once given submit it doesn't display the data .
<form action="addtodatabase.php" method="post">
<div class="container">
<form class="form-inline">
<fieldset>
<legend>Security Department User Registration</legend>
<div class="form-group">
<label for="Firstname">First Name</label>
<input type="text" class="form-control" id="Firstname" name="Firstname" placeholder="Text input"><br/>
</div>
<div class="form-group">
<label for="Secondname">Second Name</label>
<input type="text" class="form-control" id="Secondname" name="Secondname" placeholder="Text input"><br/>
</div>
</form>
.....
...
<button type="submit" class="btn btn-default">Submit</button>
addtodatabase.php page as phpmyadmin username as root , pasword NULL
<?php
$connection = mysql_connect ('root','','');
mysql_select_db('form_db');
$Firstname = $_POST ['Firstname'];
$Secondname = $_POST ['Secondname'];
echo $_POST['Firstname'];
echo '<br />';
echo $_POST['Secondname'];
$query =
"INSERT INTO form_details
(Firstname,Secondname)
values
('$Firstname','$Secondname')";
$result = mysql_query($query);
Echo "Database Saved";
mysql_close($connection);
?>
i have change the code from mysql to mysqli
<?php
$connect=mysqli_connect('localhost','root','','form_db');
if(mysqli_connect_errno($connect))
{
echo 'Failed to connect';
}
$Firstname = $_POST ['Firstname'];
$Secondname =$_POST ['Secondname'];
echo $_POST['Firstname'];
echo '<br />';
echo $_POST['Secondname'];
$query =
"INSERT INTO form_details
(Firstname,Secondname)
values
('$Firstname','$Secondname')";
$result = mysqli_query($query);
if(mysqli_affected_rows($connect) > 0){
echo "<p>Employee Added</p>";
echo "Go Back";
} else {
echo "Employee NOT Added<br />";
echo mysqli_error ($connect);
}
?>
Even after changing to mysqli it not working .addtodatabase.php
I am getting this error Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in C:\wamp64\www\Form\addtodatabase.php on line 2
My entire form looks this way
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="customstyle.css">
</head>
<body>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<form action="addtodatabase.php" method="post">
<div class="container">
<h1> Group of Companies</h1>
<h3> ICT & Security Department User Registration form </h3>
<h4> To be filled by HR department for New Employee </h4>
<form class="form-inline">
<fieldset>
<legend>Security Department User Registration</legend>
<div class="form-group">
<label for="Firstname">First Name</label>
<input type="text" class="form-control" id="Firstname" name="Firstname" placeholder="Text input"><br/>
</div>
<div class="form-group">
<label for="Secondname">Second Name</label>
<input type="text" class="form-control" id="Secondname" name="Secondname" placeholder="Text input"><br/>
</div>
</form>
<form >
<div class="form-group">
<label for="location">Dpt./Location</label>
<input type="text" class="form-control" name="location" id="location" placeholder="Text input">
</div>
</form>
<form class="form-inline">
<div class="form-group">
<label for="Designation">Designation</label>
<input type="text" class="form-control" id="Designation" placeholder="Text input"><br/>
</div>
<div class="form-group">
<label for="Fileno">File No</label>
<input type="text" class="form-control" id="Fileno" placeholder="Password"><br/>
</div>
</form>
<form class="form-inline">
<div class="form-group">
<label for="Dateofapplication">Date of Application</label>
<input type="text" class="form-control" id="Dateofapplication" placeholder="Text input"><br/>
</div>
<div class="form-group">
<label for="Dateofjoining">Date of Joining</label>
<input type="text" class="form-control" id="Dateofjoining" placeholder="Password"><br/>
</div>
</form>
<form>
<fieldset>
<legend>For Head office staffs only </legend>
<label>Card Type:</label>
<div id="idcard">
<label class="checkbox-inline">
<input type="checkbox" value="">Trainee ID Card
</label>
<label class="radio-inline">
<input type="radio" name="green">Green
</label>
<label class="radio-inline">
<input type="radio" name="red">Red
</label>
<label class="checkbox-inline">
<input type="checkbox" value="">Permanent ID Card
</label>
</div>
<div class="aligncheckbox">
<label>Door Access:</label>
<label class="checkbox-inline">
<input type="checkbox" value="">Main
</label>
<label class="checkbox-inline">
<input type="checkbox" value="">Finance Division
</label>
</div>
<div class="aligncheckbox">
<label class="checkbox-inline">
<input type="checkbox" value="">Meeting Room
</label>
<label class="checkbox-inline">
<input type="checkbox" value="">Goods Receiving
</label>
</div>
<div class="aligncheckbox">
<label class="checkbox-inline">
<input type="checkbox" value="">Graphics & Media
</label>
<label class="checkbox-inline">
<input type="checkbox" value="">IT Dept
</label>
</div>
<div class="aligncheckbox">
<label class="checkbox-inline">
<input type="checkbox" value="">Server Room
</label>
<label class="checkbox-inline">
<input type="checkbox" value="">Dist.&Quality Control
</label>
</div>
<div class="aligncheckbox">
<label class="checkbox-inline">
<input type="checkbox" value="">Warehouse Supervisor
</label>
<label class="checkbox-inline">
<input type="checkbox" value="">Pur.Office Meeting Room
</label>
</div>
<div class="aligncheckbox">
<label class="checkbox-inline">
<input type="checkbox" value="">Purchase Office
</label>
<label class="checkbox-inline">
<input type="checkbox" value="">Exit
</label>
</div>
<!-- <div class="upload">
<label for="Passportcopy">Passport Copy</label>
<input type="file" id="Passportcopy">
<label for="Photo">Photo</label>
<input type="file" id="Photo">
</div>
<div>
<label class="checkbox-inline">
<input type="checkbox" value="">Finger Registration
</label>
</div> -->
<div class="container">
<div id="upload row">
<form class="form-inline">
<div class="form-group col-xs-*">
<label for="Passportcopy">Passport Copy</label>
<input type="file" class="form-control" id="Passportcopy">
</div>
<div class="form-group col-xs-*">
<label for="Photo">Photo</label>
<input type="file" class="form-control" id="Photo">
</div>
</form>
</div>
</div>
</form>
<!-- second form IT department -->
<div class="secform">
<form>
<fieldset>
<legend>IT Department User Registration </legend>
<div class="container">
<form class="form-inline">
<div class="checkbox-inline" id="erp">
<label><input type="checkbox" value="">Enroll as sales Person ERP</label>
</div>
<div class="form-group" id="textbox">
<label for="erpmodules">ERP Modules</label>
<textarea class="form-control" rows="5" id="erpmodules"></textarea>
</div>
</fieldset>
</form>
<form>
<fieldset>
<legend>For Head office staffs only </legend>
<div class="fkhaccess">
<div class="form-group" id="textbox">
<label for="fkhaccess">FKH Folder Access</label>
<textarea class="form-control" rows="5" id="fkhaccess"></textarea>
</div>
<div class="container-fluid">
<ul id="access">
<li> <label>Internet Access</label></li>
<li>
<label class="radio-inline">
<input type="radio" name="yes">Yes
</label></li>
<li>
<label class="radio-inline">
<input type="radio" name="no">No
</label></li>
</ul>
<ul id="purpose">
<li><p>If yes, Purpose </p></li>
<li>
<div class="form-group" id="textbox">
<label for="purpose">Job Purpose</label>
<textarea class="form-control" rows="5" id="purpose"></textarea>
</div></li>
</ul>
<ul id="compmail">
<li>
<div class="form-group" id="textbox" >
<label for="companyemail">Company Email</label>
<textarea class="form-control" rows="5" id="companyemail"></textarea>
</div></li>
</ul>
<form class="form-inline">
<label> Computer facilities: </label>
<label> CD ROM </label>
<label class="checkbox-inline">
<input type="checkbox" value="">Read
</label>
<label class="checkbox-inline">
<input type="checkbox" value="">Write
</label>
<label> USB PORTS </label>
<label class="checkbox-inline">
<input type="checkbox" value="">Read
</label>
<label class="checkbox-inline">
<input type="checkbox" value="">Write
</label>
</form>
<form class="form-inline">
<label class="checkbox-inline" id="label1">
<input type="checkbox" value="">Parallel Port
</label>
</form>
<form class="form-inline">
<label> Printers </label>
<label class="checkbox-inline">
<input type="checkbox" value="">MP2352(Front Office)
</label>
<label class="checkbox-inline">
<input type="checkbox" value="">MP2501(Purchase)
</label>
</form>
<form class="form-inline">
<label class="checkbox-inline">
<input type="checkbox" value="">MP1600(MD Office)
</label>
<label class="checkbox-inline">
<input type="checkbox" value="">DSM616(Finance)
</label>
</form>
<form class="form-inline">
<ul><li>
<label> Computer Utilities :</label>
<label class="checkbox-inline">
<input type="checkbox" value="">Scanner
</label></li>
<li>
<label class="checkbox-inline">
<input type="checkbox" value="">Barcode Scanner
</label></li>
<li>
<label class="checkbox-inline">
<input type="checkbox" value="">Others
</label></li>
<ul id="software">
<li>
<div class="form-group" id="textbox" >
<label for="softwarerequired">Software Required </label>
<textarea class="form-control" rows="5" id="software"></textarea>
</div></li>
</ul>
</form>
</div>
</div>
</fieldset>
</form>
<div >
<div class="fill">
<div class='sign-container'>
<div class="div1">Form Filled by</div>
<div class='sign'> </div>
<div class="div2"> </div>
<div class="div3">(HR)</div>
</div>
</div>
<div class="sign-box">
<p id="signbox"> Signature </p>
<div class="div4"> </div>
<div class="fill">
<div class='sign-container'>
<div class="div1">Form Filled by</div>
<div class='sign'> </div>
<div class="div2"> </div>
<div class="div3">(Admin Manager)</div>
</div>
</div>
<div class="sign-box">
<p id="signbox"> Signature </p>
<div class="div4"> </div>
</div>
<div class="fill">
<div class='sign-container'>
<div class="div1">Form Filled by</div>
<div class='sign'> </div>
<div class="div2"> </div>
<div class="div3">(IT Manager)</div>
</div>
</div>
<div class="sign-box">
<p id="signbox"> Signature </p>
<div class="div4"> </div>
</div>
</div>
<div class="Threeform">
<form>
<fieldset>
<legend> User Confirmation </legend>
<div class="sign-container">
<div class="div1">User Name</div>
<div class='sign'> </div>
<div class="div1">System No</div>
<div class='sign'> </div>
</div>
<div class="policy-container">
<div class="div1">Received all the above mentioned facilities and understood international & FGC Cyber policy by
<div class='sign1'> </div></div>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</div>
</form>
</div>
</body>
</html>
plz help me to rectify.
I am not able to connect from form.html page to addtodatabase.php .
Notice: Undefined index: firstname in C:\wamp64\www\Form\addtodatabase.php on line 12
You need to pass 2 parameters! $result = mysqli_query($connect, $query);
I have a form to inject data in a database. When user clicks submit button this error shows up in a <input type=text>field. I have been looking around for a solution for hours but I do not know how to fix this.
Warning: htmlentities() expects parameter 1 to be
string, array given in
C:rr\xampp\htdocs\ecommerce\helpers\helpers.php on line
11
helpers.php
<?php
function display_errors($errors){
$display = ' <ul class="bg-danger">';
foreach($errors as $error){
$display .='<li class="text-danger">'.$error. '</li> ';
}
$display .='</ul>';
return $display;
}
function sanitize ($dirty){
return htmlentities($dirty, ENT_QUOTES, "UTF-8");
}
function money($number){
return '$ '.number_format($number,2);
}
form.php
<form action="products.php?add=1" method="POST" enctype="multipart/form-data">
<div class='container_12'>
<div class="form-group col-md-3">
<label for="prod_name">Product Name*:</label>
<input type="text" name="prod_name" id="prod_name" class="form-control" value="<?=((isset($_POST['prod_name']))?sanitize($_POST):' ');?>">
</div>
<div class="form-group col-md-3">
<label for="parent">Parent Category*:</label>
<select class="form-control" id="parent" name="parent">
<option value=""<?=((isset($_POST['parent']) && $_POST['parent'] == '')?'selected':'');?>></option>
<?php while($parent = mysqli_fetch_assoc($parentQuery)): ?>
<option value=" <?=$parent['id'];?>"<?=((isset($_POST['parent']) && $_POST['parent'] == $parent['id'])?' select':'');?>><?=$parent['category_name'];?></option>
<?php endwhile; ?>
</select>
</div>
<div class='form-group col-md-3'>
<label for='child'>Second Category*:</label>
<select id='child' name='child' class='form-control'></select>
</div>
</div>
<div class='container_12'>
<div class='form-group col-md-3'>
<label for='list_price'>List Price(OPTIONAL): </label>
<input type="text" id="list_price" name="list_price" class="form-control" value="<?=((isset($_POST['list_price']))?sanitize($_POST['list_price']):'');?>">
</div>
<div class="form-group col-md-3">
<label for="price">Price*:</label>
<input type="text" id="price" name="price" class="form-control" value="<?=((isset($_POST['price']))?sanitize($_POST['price']):'');?>">
</div>
<div class='form-group col-md-3'>
<label for='prod_width'>Width* (in inches):</label>
<input type="text" id="prod_width" name="prod_width" class="form-control" value="<?=((isset($_POST['prod_width']))?sanitize($_POST['prod_width']):'');?>">
</div>
<div class='form-group col-md-3'>
<label for='prod_depth'>Height*(in inches):</label>
<input type="text" id="'prod_depth" name="'prod_depth" class="form-control" value="<?=((isset($_POST['prod_depth']))?sanitize($_POST['prod_depth']):'');?>">
</div>
</div>
<div class='container_12'>
<div class='form-group col-md-3'>
<label for='prod_height'>Depth*(in inches):</label>
<input type="text" id="prod_height" name="prod_height" class="form-control" value="<?=((isset($_POST['prod_height']))?sanitize($_POST['prod_height']):'');?>">
</div>
<div class='form-group col-md-3'>
<label for='prod_material'>Construction Material:</label>
<input type="text" id="prod_material" name="prod_material" class="form-control" value="<?=((isset($_POST['prod_material']))?sanitize($_POST['prod_material']):'');?>">
</div>
<div class='form-group col-md-6'>
<label>Quantity * :</label>
<input type="text" id="quantity" name="quantity" class="form-control" value="<?=((isset($_POST['quantity']))?sanitize($_POST['quantity']):'');?>">
</div>
</div>
<div class='container_12'>
<div class="form-group col-md-3"> <label for="image_1">Product Photo #1:</label>
<input type="file" name="image_1" id="image_1" class="form-control">
</div>
<div class="form-group col-md-3"> <label for="image_2">Product Photo #2:</label>
<input type="file" name="image_2" id="image_2" class="form-control">
</div>
<div class="form-group col-md-3"> <label for="image_3">Product Photo #3:</label>
<input type="file" name="image_3" id="image_3" class="form-control">
</div>
<div class="form-group col-md-3"> <label for="image_4">Product Photo#4:</label>
<input type="file" name="image_4" id="image_4" class="form-control">
</div>
</div>
<div class='container_12'>
<div class="form-group col-md-6">
<label for="description">Description:</label>
<textarea id="description" name="description" class="form-control" rows="6"><?=((isset($_POST['description']))?sanitize($_POST['description']):'');?></textarea>
</div>
<div class="form-group col-md-6">
<label for="care_instructions">Care Instructions*:</label>
<textarea id="care_instructions" name="care_instructions" class="form-control" rows="6"><?=((isset($_POST['care_instructions']))?sanitize($_POST['care_instructions']):'');?></textarea>
</div></div>
<div class='container_12'>
<div class="form-group pull-right">
<input type='submit' value='Add Product' class='form-control btn-success pull-right'>
</div></div>
</form>
You are passing the whole $_POST variable to serialize in the product name input (that's why the error says 'array given').
Check this line:
<input type="text" name="prod_name" id="prod_name" class="form-control" value="<?=((isset($_POST['prod_name']))?sanitize($_POST):' ');?>">
And change it with this:
<input type="text" name="prod_name" id="prod_name" class="form-control" value="<?=((isset($_POST['prod_name']))?sanitize($_POST['prod_name']):' ');?>">