I am beginner in CodeIgniter. I want to insert records in database. Here is my view file. I save view file as a register.php and my model file as register_insert.php and controller file as register.php and my table name in mysql is "register".
View
<?php $this->load->view('layot/header'); ?>
<html>
<head>
<title>Register</title>
<style>
td {
border:none;
}
</style>
</head>
<form method="POST" action="register">
<table border="1" align="center">
<tr>
<td>User Name : <input type="text" name="u_nm"></td>
</tr>
<tr>
<td>Password : <input type="text" name="pwd"></td>
</tr>
<tr>
<td>E-Mail: <input type="text" name="eid"></td>
</tr>
<tr>
<td>Contact No : <input type="text" name="cno"></td>
</tr>
<tr>
<td>Mobile No : <input type="text" name="mbo"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="submit" value="Register"></td>
</tr>
</table>
</form>
</html>
</table>
This is my controller file..
public function index()
{
if(isset($_POST['submit']))
{
$data = array(
'u_nm' => $this->input->post('u_nm'),
'pwd' => $this->input->post('pwd'),
'eid' => $this->input->post('eid'),
'cno' => $this->input->post('cno'),
'mbo' => $this->input->post('mbo'),
);
$this->register_insert->form_insert($data);
}
$this->load->view('register');
}
}
?>
This is my model file..
<?php
class Register extends CI_Model
{
function __construct() {
parent::__construct();
}
function form_insert($data){
$this->db->insert('register', $data);
}
}
?>
}
?>
I think you need to load model register in controller
public function __construct(){
parent::__construct();
$this->load->model('register');
}
In function index, you need to call class name register
public function index()
{
if(isset($_POST['submit']))
{
$data = array(
'u_nm' => $this->input->post('u_nm'),
'pwd' => $this->input->post('pwd'),
'eid' => $this->input->post('eid'),
'cno' => $this->input->post('cno'),
'mbo' => $this->input->post('mbo'),
);
$this->register->form_insert($data);
}
$this->load->view('register');
}
}
And in model you can return the last insert id after inserted
public function form_inssert($data){
$this->db->insert('register',$data);
return $this->db->insert_id();
}
This is my controller file
<?php
class Register extends CI_Controller{
public function index()
{
$this->load->model('register_insert');
if(isset($_POST['submit']))
{
$data = array(
'u_nm' => $this->input->post('u_nm'),
'pwd' => $this->input->post('pwd'),
'eid' => $this->input->post('eid'),
'cno' => $this->input->post('cno'),
'mbo' => $this->input->post('mbo'),
);
$this->register_insert->form_insert($data);
}
$this->load->view('register');
}
}
?>
And This is my model file
<?php
class Register_insert extends CI_Model
{
function __construct()
{
parent::__construct();
}
function form_insert($data)
{
$this->db->insert('register', $data);
}
}
?>
My mistake is naming of my model class name is "Register_insert" and my old class name is Register that is why my model is not load properly..
in your controller function as given below:
public function index()
{
if(isset($_POST['submit']))
{
$data = array(
'u_nm' => $this->input->post('u_nm'),
'pwd' => $this->input->post('pwd'),
'eid' => $this->input->post('eid'),
'cno' => $this->input->post('cno'),
'mbo' => $this->input->post('mbo'),
);
$this->register_insert->form_insert($data);
redirect('register');
}
$this->load->view('register');
}
}
in your model file as given below :
function form_insert($data){
$this->db->insert('register', $data);
return true;
}
try this now u get result
You may try this for your view code:
<html>
<head>
<title>Register</title>
<style>
td {
border:none;
}
</style>
</head>
<table border="1" align="center">
<?php echo form_open('register/register_insert'); ?>
<tr>
<td>User name:<?php echo form_input('u_nm'); ?></td>
</tr>
<tr>
<tr>
<td>Password :<?php echo form_input('pwd'); ?></td>
</tr>
<tr>
<td>E-Mail: <?php echo form_input('eid'); ?></td>
</tr>
<tr>
<td>Contact No : <?php echo form_input('cno'); ?></td>
</tr>
<tr>
<td>Mobile No : <?php echo form_input('mbo'); ?></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="submit" value="Register"></td>
</tr>
<?php echo form_close(); ?>
</table>
</form>
</html>
</table>
It is more easier using the form helper. You can autoload this in the autoload.php config file like this:
$autoload['helper'] = array('form');
This will be your controller:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class register extends CI_Controller {
public function index(){
$this->load->view('register');
}
public function register_insert()
{
//Here you can add validations for your form before trying to insert data to your Database
if(isset($_POST['submit']))
{
$this->load->model('register_insert');
$agregar=$this->register_insert->add_user();
}
$this->load->view('register');
}
}
?>
And finally your model code:
<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');
class register_insert extends CI_Model {
public function __construct(){
parent::__construct();
}
public function add_user (){
$data = array(
'u_nm' =>$this->input->post('u_nm'),
'pwd' => $this->input->post('pwd'),
'eid' => $this->input->post('eid'),
'cno' => $this->input->post('cno'),
'mbo' => $this->input->post('mbo'),
);
$this->db->insert('register', $data);
}
}
For this model you will also need to change your autoload.php
you need to autoload the database library:
$autoload['libraries'] = array('database');
Related
I have a planet overview and now I want to make that you can edit the planets, but instead of updating an existing database record, my Laravel creates a new database record.
I looked on the internet and I saw the I wasn't the only one having issues with this and tried to solve it myself. I tried to add it all at once using
$member->update($request->only(['name','description', 'size_in_km', 'solar_systems_id']);
I've also tried to do it individually but nothing is working, when I submit the form, there are two planets in my database, a new planet with the entered input and the old one is also still there, instead of one planet with the new entered input.
PlanetController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Planet;
use App\Models\SolarSystems;
class PlanetController extends Controller
{
public function home()
{
echo "Welcome at the home pagina!<br>
<a href='planets'>Click here to see all the planets!</a>";
}
// Alle planeten weergeven
public function index()
{
$planets = Planet::with('solar')->get();
echo view('planets', ['planets'=>$planets]);
echo "
<a href='../planets/insert'>Insert new planets!</a>
<br>
<a href='/solarsystems'>Look at the solar systems!</a>
";
}
public function show($planeet)
{
// Alleen de gevraagde planeten weergeven
$planets = Planet::with('solar')->where('name', $planeet)->get();
echo view('planets', ['planets' => $planets]);
echo "
<a href='$planeet/edit'>Edit planet data</a><br>
<a href='../planets'>Go back!</a>
";
}
public function insertForm()
{
$solarsystems = SolarSystems::all();
return view('insertplanets', ['solarsystems' => $solarsystems]);
}
public function insertData(Request $request) {
$name = $request->input('name');
$description = $request->input('description');
$size_in_km = $request->input('size_in_km');
$solar_systems_id = $request->input('solar_systems_id');
$data = array('name' => $name, 'description' => $description, 'size_in_km' => $size_in_km, 'solar_systems_id' => $solar_systems_id);
Planet::insert($data);
return redirect('./planets');
}
public function editPlanetDataForm($planeet)
{
$dataPlanet = Planet::where('name', $planeet)->first();
echo view('editplanetdataform', ['dataPlanet' => $dataPlanet]);
}
public function editPlanetData($planeet, Request $request)
{
return Planet::where('name', $planeet)->first()->update([
'name' => $request->input('name'),
'description' => $request->input('description'),
'size_in_km' => $request->input('size_in_km'),
'solar_systems_id' => $request->input('solar_systems_id')
]);
}
}
editplanetformdata.blade.php
<html>
<head>
<style>
body {
font-family: 'Nunito', sans-serif;
padding: 0;
margin: 0;
text-align: left;
}
</style>
</head>
<body>
<h2>Update data of the planet!</h2>
<table>
<form action='/planets/create' method='POST'>
<input type = "hidden" name = "_token" value = "<?php echo csrf_token();?>"><input type = "hidden" name = "_token" value = "<?php echo csrf_token();?>">
<tr>
<th>New name of the planet</th>
<td><input name='name' type='text' required></td>
</tr>
<tr>
<th>New size in km of the planet</th>
<td><input name='size_in_km' type='number' required></td>
</tr>
<tr>
<th>New id of the solar system</th>
<td><input type='number' name='solar_systems_id' required></td>
</tr>
<tr>
<th>New information about the planet</th>
<td><textarea rows='5' cols='40' name='description' required></textarea></td>
</tr>
<tr>
<th><input type='submit' value='Add planet'></th>
</tr>
</form>
</table>
<a href='../../planets'>Go back!</a>
</body>
</html>
public function editPlanetData($planeet, Request $request)
{
return Planet::where('name', $planeet)->update([
'name' => $request->input('name'),
'description' => $request->input('description'),
'size_in_km' => $request->input('size_in_km'),
'solar_systems_id' => $request->input('solar_systems_id')
]);
I am brand new to Livewire/Jetstream and trying to make a little inventory application to try it out. In my example below I'm trying to show inventory items from DB on a table with the ability to update the inventory name and quantity from the table without going to an edit page.
I have a nested foreach and when I render the page the input fields in the loop show the value and then disappear but the value is showing correctly in the HTML. Any help would be appreciated!
**Show Inventory**
namespace App\Http\Livewire;
use App\Models\Inventory;
use Livewire\Component;
class ShowInventory extends Component
{
public $inventories;
public function mount()
{
$this->inventories = Inventory::orderBy('name')->get();
}
public function render()
{
return view('livewire.show-inventory');
}
public function name()
{
$this->name = $name;
}
public function update($id)
{
$data = $this->validate([
'name' => 'required',
'available_on_hand' => 'required',
]);
$this->item_id = $id;
$item = Inventory::find($this->item_id);
$item->update([
'name' => $this->name,
'available_on_hand' => $this->available_on_hand,
]);
}
}
**Show Item**
namespace App\Http\Livewire;
use App\Models\Inventory;
use Livewire\Component;
class ShowItem extends Component
{
public $inventory;
public function mount(Inventory $inventory)
{
$this->inventory = $inventory;
}
public function render()
{
return view('livewire.show-item');
}
}
**Parent Blade**
<table class="table-auto">
<thead>
<tr>
<th>Name</th>
<th>Quantity</th>
<th></th>
<th>View</th>
</tr>
</thead>
<tbody>
#foreach($inventories as $inventory)
#livewire('show-item', ['inventory' => $inventory], key($inventory->id))
#endforeach
</tbody>
</table>
**Nested Blade**
<form wire:submit.prevent="update({{ $inventory->id }})">
<tr>
<td><input type="input" wire:model="name" value="{{$inventory->name}}" /></td>
<td><input type="input" wire:model="available_on_hand" value="{{$inventory->available_on_hand}}" /></td>
<td><button type="submit">Save</button></td>
<td>View</td>
</tr>
</form>
<form wire:submit.prevent="update({{ $inventory->id }})">
<tr>
<td><input type="input" wire:model="name"/></td>
<td><input type="input" wire:model="available_on_hand"/></td>
<td><button type="submit">Save</button></td>
<td>View</td>
</tr>
</form>
in component
public $name, $available_on_hand;
//....
public function getModel($modelID)
{
$model = Inventory::find($modelID);
$this->name = $model->name;
$this->available_on_hand = $model->available_on_hand;
}
as you can see, always you call the getModel method, it bind to your properties the current values of model also you can edit them and save it.
You can't use both, value attribute and the wire:model, that give you the conflicts you have now
I'm making a fuction on the CodeIgniter framework so users are able to edit/update product detail information. But when I submit the form I get a blank page and the product isn't updated in my database. I couldn't find a mistake myself.
Here is my view file form (cadeaubewerken.php):
<table class="aanbieding-cadeau">
<form action="<?php echo base_url() . "KdGwController/update_product"; ?>" method="post">
<tr>
<h4>Cadeau naam</h4>
<td><?php echo form_input(array('id'=>'product_naam', 'name'=>'product_naam', 'value' => $product["product_naam"] , 'size'=>25));?></td>
<td><?php echo form_input(array('type'=>'hidden','id'=>'product_id', 'name'=>'product_id', 'value' => $product['product_id']));?>
</tr>
<tr>
<td><?php echo form_open_multipart('Product/upload'); ?> </td>
</tr>
<tr>
<td>
<h4>Kies een categorie</h4>
<select name="category_id">
<?php foreach (get_categories_h() as $category) :
if ($category->id == $product["category_id"]){
echo '<option value="'.$category->id .'" selected>' . $category->name . '</option>';
}else{
echo '<option value="'.$category->id .'">'.$category->name . '</option>';
}
endforeach; ?>
</select>
</td>
</tr>
<tr>
<td><h4>Ophaal plaats</h4><?php echo form_input(array('id'=>'ophaal_plaats', 'name'=>'ophaal_plaats', 'value' => $product["ophaal_plaats"], 'size'=>25));?></td>
<td><?php echo form_input(array('type'=>'hidden','id'=>'ophaal_plaats', 'name'=>'ophaal_plaats', 'value' => $product['ophaal_plaats']));?>
</tr>
<tr>
<td>
<div class="checkbox">
<label><input type="checkbox" value="">Gebruik adres van mijn account</label>
</div>
</td>
</tr>
<tr>
<td>
<h4>Huidig cadeau profiel foto</h4>
<img src="<?php echo base_url(); ?>upload/<?php echo $product['product_foto_thumb']; ?>" class="img-responsive">
</td>
</tr>
<tr>
<td>
<h4>Cadeau profiel foto veranderen</h4>
<input type="file" name="userfile"/>
</td>
</tr>
<tr>
<td><?php echo form_textarea(array('type'=>'textarea','id'=>'product_beschrijving', 'name'=>'product_beschrijving', 'value' =>$product['product_beschrijving'], 'size'=>25));?></td>
<td><?php echo form_input(array('type'=>'hidden','id'=>'product_beschrijving', 'name'=>'product_beschrijving', 'value' => $product['product_beschrijving']));?>
</tr>
<tr>
<td><input type="submit" class="btn btn-primary" name="submit" value="Cadeau bewerken!" /></td>
</tr>
</table>
</form>
This is my controller file (KdGwController.php):
<?php
class KdGwcontroller extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('Product_model');
$this->load->model('Update_product_model');
$this->load->helper(array('form', 'url'));
}
public function details_bewerken($product_id) {
//load the Product_model
$this->load->model('Product_model');
//call function getdata in de Product_model
$data['userdetail_list'] = $this->Product_model->getdata();
//get product details
$data['product'] = $this->Product_model->get_product_details($product_id);
//laad view
$data['main_content'] = 'cadeaubewerken';
$this->load->view('cadeaubewerken',$data);
}
public function update_product() {
$id= $this->input->post('product_id');
$data = array(
'product_naam' => $this->input->post('product_naam'),
'category_id' => $this->input->post('category_id'),
'ophaal_plaats' => $this->input->post('ophaal_plaats'),
);
}
}
And this is my model file : (Update_product_model.php):
<?php
class Update_product_model extends CI_Model {
function update_product($id,$data){
$this->db->where('product_id', $id);
$this->db->update('products', $data);
header ('location:https://kadokado-ferran10.c9users.io//AlleCadeausController');
}
}
?>
No need to add update in model.
Also, your controller update function should be like
public function update_product() {
$id= $this->input->post('product_id');
$data = array(
'product_naam' => $this->input->post('product_naam'),
'category_id' => $this->input->post('category_id'),
'ophaal_plaats' => $this->input->post('ophaal_plaats'),
);
// here I am assuming that `Product` is model
$this->Product->where($id);
$this->Product->update($data);
}
You have not call model function in your controller.
Change your controller update_product() function like this
public function update_product() {
$id= $this->input->post('product_id');
$data = array(
'product_naam' => $this->input->post('product_naam'),
'category_id' => $this->input->post('category_id'),
'ophaal_plaats' => $this->input->post('ophaal_plaats'),
);
$this->Update_product_model->update_product($id,$data);
}
You are not calling function update_product() of Update_product_model.php from KdGwController.php
Change this in your controller...
public function update_product() {
$id= $this->input->post('product_id');
$data = array(
'product_naam' => $this->input->post('product_naam'),
'category_id' => $this->input->post('category_id'),
'ophaal_plaats' => $this->input->post('ophaal_plaats'),
);
$this->Update_product_model->update_product($id,$data); // this line you missed
}
i have a problem : I have a view that needs to call a method in my controller EmployeeController to go to the page to add an employee. But it don't work.. also with other views it doe
This is the view:
<body>
<form class="form-horizontal" id='employeeform' id='employeeform' action="<?php echo base_url();?>EmployeeController/addEmployee" method="post">
<div class="container">
<div class="row">
<div class="col-lg-12 col-sm-12">
<table class="table table-striped table-hover">
<thead>
<tr>
<th>#</th>
<th>ID</th>
<th>Naam werknemer</th>
<th>Datum aanwerving</th>
<th>Salaris</th>
</tr>
</thead>
<tbody>
<?php for ($i = 0; $i < count($employeelist); ++$i) { ?>
<tr>
<td><?php echo ($i+1); ?></td>
<td><?php echo $employeelist[$i]->employee_no; ?></td>
<td><?php echo $employeelist[$i]->employee_name; ?></td>
<td><?php echo $employeelist[$i]->hired_date; ?></td>
<td><?php echo $employeelist[$i]->salary; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
<input id="btn_add" name="btn_add" type="submit" class="btn btn-primary" value="Add employee" />
</div>
</div>
</div>
</form>
</body>
THIS IS THE CONTROLLER:
class EmployeeController extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->library('session');
$this->load->helper('form');
$this->load->helper('url');
$this->load->database();
$this->load->library('form_validation');
//load the employee model
$this->load->model('employee_model');
}
//index function
function index() {
$employeelist = $this->employee_model->get_employee_list();
$data['employeelist'] = $employeelist;
$this->load->view('employee/employee_add', $data);
}
function createEmployee() {
$this->form_validation->set_rules('employee_no', 'Employee No', 'trim|required|numeric');
$this->form_validation->set_rules('employee_name', 'Employee Name', 'trim|required|xss_clean|callback_alpha_only_space');
$this->form_validation->set_rules('hired_date', 'Hired Date', 'required');
$this->form_validation->set_rules('salary', 'Salary', 'required|numeric');
if ($this->form_validation->run() == FALSE) {
//fail validation
$this->load->view('employee/employee_add');
} else {
//pass validation
$data = array(
'employee_no' => $this->input->post('employeeno'),
'employee_name' => $this->input->post('employeename'),
'hired_date' => #date('Y-m-d', #strtotime($this->input->post('hireddate'))),
'salary' => $this->input->post('salary'),
);
//insert the form data into database
$this->db->insert('tbl_employee', $data);
//display success message
$employeeresult = $this->employee_model->get_employee_list();
$data['employeelist'] = $employeeresult;
//load the department_view
$this->load->view('employee/employee_view', $data);
redirect('employee/employee_view');
}
}
function addEmployee() {
$this->load->view('employee/employee_add');
//set validation rules
}
}
?>
THE ERROR I GET IS :
The requested URL /BoMaTec_afstudeerproject/CodeIgniter-3.0.1/EmployeeController/createEmployee was not found on this server.
Seems like you don't have any router for that url, or the router is wrong. As per the error, it's trying to load a folder named EmployeeController and within that folder, another folder named createEmployee. Check your .htaccess file and your routers in the framework you are using.
Adding products to my cart is working fine, but after adding product to cart its not able to view it in my view page without refreshing view page,
I tried redirect() method still no use.
How do i view my page after adding products to cart.
Any help??
my controller:
<?php
class Cart extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function addtocart($cal_id, $f_id) {
if (empty($cal_id)) {
$this->load->view('Cart_view');
}
$this->load->model('cart_model');
$product = $this->cart_model->cart_contents($cal_id, $f_id);
foreach ($product as $row) {
$name = $row->title;
$fees = $row->fees;
}
$product = array(
'id' => $cal_id,
'qty' => 1,
'price' => $fees,
'name' => $name,
);
print_r($product);
$this->cart->insert($product);
$this->load->view('Cart_view');
}
public function destroy() {
$this->cart->destroy();
}
}
?>
my model:
<?php
class cart_model extends CI_Controller
{
public function __construct() {
parent::__construct();
}
public function cart_contents($cal_id,$f_id)
{
$product=$this->db->select('*')
->from('calander')
->join('fees','calander.cal_id=fees.cal_id')
->where('fees.cal_id',$cal_id)
->where('fees.f_id',$f_id)
->get();
return $product->result();
}
}
?>
my view
<html>
<head><title></title></head>
<body>
<?php
if ($cart = $this->cart->contents()) {
?><table>
<tr>
<td>
<h3><u>Cart page</u></h3>
</td>
</tr>
<tr>
<th>course</th>
<th>Quantity</th>
<th>Price</th>
</tr>
<?php
foreach ($cart as $contents) {
?><tr>
<td><?php echo $contents['name']; ?></td>
<td><input type="text" value="<?php echo $contents['qty']; ?>"></td>
<td><?php echo $contents['price']; ?></td>
</tr>
<?php } ?>
</table> <?php
// redirect(base_url('/index.php/Cart/Cart_view'));
}
?>
</body>
</html>
I dont understand what you are trying to do. When you load your view $this->load->view('Cart_view');, it is required to pass the data array, containing all the variables into the view like this $this->load->view('Cart_view',$data); . Without data array, view cannot display data. When ever you write code atleast, make variables names in such a way that other people will be able to understand what that variable stands for. Without enough data, i am taking the liberty of modifying your code
Controller
public function addtocart($cal_id, $f_id) {
$data=array(
'view'=>''
);
if (empty($cal_id)) {
$this->load->view('Cart_view',$data);
} else {
$this->load->model('cart_model');
$product = $this->cart_model->cart_contents($cal_id, $f_id);
$data['view']='';
foreach($product as $row):
$data['view'].=' <tr>
<td>'.$row->title.'</td>
<td>'.$row->qty.'</td>
<td>'.$row->price.'</td>
</tr>';
endforeach;
$this->load->view('Cart_view',$data);
}
}
MODEL
public function cart_contents($cal_id,$f_id){
$this->db->select('*')
$this->db->from('calander')
$this->db->join('fees','calander.cal_id=fees.cal_id')
$this->db->where('fees.cal_id',$cal_id)
$this->db->where('fees.f_id',$f_id);
$query = $this->db->get();
return $query->result();
}
view
<html>
<head><title></title></head>
<body>
<table>
<tr>
<td>
<h3><u>Cart page</u></h3>
</td>
</tr>
<tr>
<th>course</th>
<th>Quantity</th>
<th>Price</th>
</tr>
<?php if($count>0){
echo $view;
}?>
</table>
</body>
</html>