Why my Data is not inserted in Table - codeigniter

Hey I'm new to PHP and codeigniter. I have started with CRUD operation and I'm trying to insert data in my table. Everything seems to work fine but in my table the data is not inserted.
Here is my Model, Controller and View. Please elaborate my mistake in the code. What improvement my code needs.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class stdmain_controller extends CI_Controller
{
public function index()
{
echo "Hello";
}
public function insert_std_record()
{
$this->load->model('stdmain_model');
$this->load->view('std_insert_record');
}
Model
class Stdmain_model extends CI_Model
{
public function insert_std_record()
{
$data = array('std_name'=>$this->input->post('std_name'),'std_course'=>$this->input->post('std_course'),'std_mob'=>$this->input->post('std_mob') );
$this->db->insert('student_record',$data);
}
}
?>
View
<html>
<head>
<title>Insert Student Record</title>
</head>
<body>
<h1><center>Add Student Data</center></h1>
<form>
<table border="1">
<tr>
<th>Name</th>
<th><input type="text" name="std_name"></th>
</tr>
<tr>
<th>Couse</th>
<th><input type="text" name="std_course"></th>
</tr>
<tr>
<th>Mob No</th>
<th><input type="text" name="std_mob"></th>
</tr>
<tr>
<th>City</th>
<td colspan="2"><select>
<option value="Islamabad">Islamabad</option>
<option value="Lahore">Lahore</option>
</select>
</td>
</tr>
<tr>
<td colspan="2"><center><input type="submit"></center></td>
</tr>
</table>
</form>
</body>
</html>

Change your form tag like this
<form method="post" action="<?php echo base_url()?>index.php/stdmain_controller/insert_std_record">

Your form do not have any action.
Add this in you form opening tag:
<form method="post" action="<?php echo base_url("stdmain_controller/insert_std_record")?>">

Related

Button in table row redirect to another page with the data of the row in Laravel

I would like to insert a button in every row of a table and when I click on this button, it redirect me to another page with the data of the single table row using Laravel
How can I do this?
This is my form:
<html>
<table class="table">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">USERNAME</th>
<th scope="col">MAC ADDRESS</th>
</tr>
</thead>
<tbody>
#foreach ($data as $item)
<tr>
<th scope="row">{{$item->id}}</th>
<td>{{$item->username}}</td>
<td>{{$item->mac_addr}}</td>
<td>
<form action="{{url('singleDevice')}}" method="get">
<button class="btn btn-primary" type="submit">Select</button>
</form>
</td>
</tr>
#endforeach
</tbody>
</table>
</body>
</html>
This is my controller:
class DeviceController extends Controller
{
public function index()
{
$data=Device::all();
return view('device', compact("data"));
}
public function create()
{
return view('registrationDevice');
}
public function store(Request $request)
{
$input = new Device;
//On left field name in DB and on right field name in Form/view
$input -> username = $request->username;
$input -> mac_addr = $request->mac_address;
$input->save();
return redirect('registrationDevice')->with('message', 'DATA SAVED');
}
public function show(Device $device)
{
return view('singleDevice');
}
}
Thanks in advance
Change your form like :
<tbody>
#foreach ($data as $item)
<tr>
<th scope="row">{{$item->id}}</th>
<td>{{$item->username}}</td>
<td>{{$item->mac_addr}}</td>
<td>
Select
</td>
</tr>
#endforeach
</tbody>
If you want to use route name, you can change by :
<td>
Select
</td>
Change your route like :
Route::get('/singleDevice/{deviceID}', [DeviceController::class, 'show'])->name('show');
Change your show function of the controller like:
public function show($deviceID)
{
$device = Device::firstWhere('id', $deviceID);
return view('singleDevice', compact("device"));
}
Why do you need a form? use the link
<form action="{{url('singleDevice')}}" method="get">
<button class="btn btn-primary" type="submit">Select</button>
</form>
replace with
Select
and configure the URL in the router as /singleDevice/{device}
Route::get('/singleDevice/{device}', [DeviceController::class, 'show'])->name('show');
You also can use this as you have already used form in your code
<form action="{{ route('show', $item->id) }}" method="get">
<button class="btn btn-primary" type="submit">Select</button>
</form>
public function show(Device $device)
{
return view('singleDevice');
}
For Route you can pass $device:
Route::get('/singleDevice/{$device}', [DeviceController::class, 'show'])->name('show');

Undefined variable in action route with parameter for laravel

This is my view, and I want to pass r{{$i}}_selected to controller, but it gives me an error. Undefined variable: enterprise . How can I solve this?
<form name="frm-example" id="frm-example"
action="{{ route('pengawas_alokasi.store', $enterprise ) }}"
method="post">
{{ csrf_field() }}
<div class="table-responsive text-center">
<table class="table table-borderless table-striped table-hover" id="myTable">
<thead class="thead-dark">
<tr>
<th></th>
<th class="text-center">#</th>
<th class="text-center">Nama Perusahaan</th>
</tr>
</thead>
<?php $i=1;?> #foreach($enterprises as $enterprise)
<tr>
{{--
<td></td> --}}
<td>
<input type="checkbox" name="r{{$i}}_selected" value="{{$enterprise->id}}">
</td>
<td>{{$i}}</td>
{{-- <td>
<input type="hidden" name="nomor{{$i}}" value="{{$i}}" size="3">{{$i}}</td> --}}
<td>{{$enterprise->nama_perusahaan}}</td>
{{-- <td>
<input type="hidden" name="nama_perusahaan{{$i}}" value="{{$enterprise->nama_perusahaan}}" size="3">{{$enterprise->nama_perusahaan}}</td> --}}
</tr>
<?php $i=$i+1;?> #endforeach
</table>
</div>
<div class="form-group">
<input type="submit" name="" class="btn btn-primary" value="Submit">
</div>
</form>
This is the controller that responsible to the view above
public function insert($id)
{
$enterprises = Enterprise::get();
return view('pengawas.pengawas-insert-alokasi', compact('enterprises'));
}
This is the controller that should store the value that I want to retrieve in my view form
public function store_enterprise(Request $request){
//I'm not doing it yet
dd($request);
}
because you are sending "enterprises" from your controller and you are trying to send "enterprise" in your form's action
you can use hidden variable in your form instead of use URL variable :
<input type="hidden" name="enterprise" value="{{$enterprise}}">
and in your controller :
public function insert(Request $request)
{
//get enterprise value here
$enterprise = $request->enterprise;
}

variable showing null value in view laravel

i am trying to sent data to view from function in controller but the variable view is not showing any result
public function goedit($id)
{
$catg = Category::where('cat_id',$id)->first();
return view('product.gocatedit')->with('row', $catg);
}
and my view is
#if(isset($row))
<form action="{{action('ProductController#edit')}}" method="post">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="col-sm-12">
<h1 style="text-align:center;">Edit Items</h1>
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Category</th>
<th>Item</th>
<th>Price</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<input type="hidden" name="item_id" value="{{ $row->item_id}}">
<td><input class="form-control" name="item_name" value="{{$row->item_name}}" /></td>
<td><input class="form-control" name="item_price" value="{{$row->item_price}}" /></td>
<td><input class="btn btn-primary btn-block btn-flat" type="submit" value="Edit"></td>
</tr>
</tbody>
</table>
</div>
</form>
#endif
please help thanks
It seems you query returning null, that's the problem (there is no such ID in a database).
If you want to check for null, use is_null:
#if(is_null($var))
Or maybe you want to use empty:
#if(!empty($row))
public function goedit($id)
{
$catg = Category::where('cat_id',$id)->first();
return view('product.gocatedit')->with('row', $catg);
}
practically should be:
public function goedit($id)
{
$catg = Category::findOrFail($id);
return view('product.gocatedit')->with('row', $catg);
}
and then it seems that you aren't using default primary key, which is "id", so in your model add
public class Category{
...................
protected $primaryKey = "cat_id";
.....................
}
If you use ->with('something', 'data'), then the data passed will be in the session('something') and it's purpose is to 'flash' messages.
You need this (view data is supposed to be passed as an array, and as a second parameter of the view() function):
public function goedit($id)
{
$catg = Category::where('cat_id',$id)->first();
return view('product.gocatedit', ['row' => $catg]);
}

Message: Undefined property: stdClass::$Date using codeigniter

my code runs well but im getting this error Message: Undefined property: stdClass::$Date and i dont know where this error came from, as far as ive seen my codes are correct. The problem is in my foreach code
here's my controller below
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start();
class News_and_events extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->library('form_validation');
$this->load->model('admin_model', 'am');
}
public function index(){
if($this->session->userdata('logged_in')){
$this->data['title'] = 'News and Events | Spring Rain Global Consultancy Inc Admin Panel';
$this->data['logout'] = 'Logout';
$session_data = $this->session->userdata('logged_in');
$this->data['id'] = $session_data['id'];
$this->data['username'] = $session_data['username'];
$this->data['allData'] = $this->am->getAllData();
$this->load->view('pages/admin_header', $this->data);
$this->load->view('content/news_and_events', $this->data);
$this->load->view('pages/admin_footer');
}else{
redirect('login', 'refresh');
}
}
}
my model
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Admin_model extends CI_Model{
public function saveData($array){
$this->db->insert('news_and_updates', $array);
}
public function getAllData(){
return $this->db->select(
'news_and_updates.Event',
'news_and_updates.Description',
'news_and_updates.Date'
)
->from('news_and_updates')
->order_by("Date", "desc")
->get()->result_object();
}
}
?>
and my views
<script type="text/javascript">
$(document).ready(function(){
$("#add_another").click(function(){
alert('test');
});
});
</script>
<div class="container" >
<br />
<br />
<br />
<ul id="nav">
<li><h4>Home</h4></li>
<li><h4>News and Events</h4></li>
<li><h4>Activities</h4></li>
</ul>
<div class="starter-template">
<h1>News And Events</h1>
<?php if($this->session->flashdata('add_another')):?>
<div id="add_another" style="float:left;">
<input type="button" value="Add Another" class="btn btn-primary" />
</div>
<?php else: ?>
<form action="<?php echo base_url().'news-and-events/add'?>" method="post">
<?php echo validation_errors('<div class="error">', '</div>');?>
<table class="table-striped">
<tr>
<td>Date: </td>
<td><input type="text" id="datepicker" name="date" value="<?php echo set_value('date');?>" /></td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td >Event: </td>
<td ><input type="text" name="event" value="<?php echo set_value('event');?>" /></td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td width="20%">Description: </td>
<td><textarea cols="30" rows="5" name="description" ><?php echo set_value('description');?></textarea></td>
</tr>
<tr>
<td width="20%"> </td>
<td><input type="submit" value="Add" class="btn btn-success" /></td>
</tr>
</table>
</form>
<?php endif; ?>
<br />
<br />
<table class="table" >
<tr>
<th>Date</th>
<th width="51%">Event</th>
<th>Description</th>
<th>Options</th>
</tr>
<?php foreach($allData as $x => $allDatas): ?>
<tr>
<td><?php echo $allDatas->Date; ?></td>
<td><?php echo $allDatas->Event; ?></td>
<td></td>
<td></td>
</tr>
<?php endforeach; ?>
</table>
</div>
</div><!-- /.container -->
<script>
var date = new Date();
var currentMonth = date.getMonth();
var currentDate = date.getDate();
var currentYear = date.getFullYear();
$('#datepicker').datepicker({
minDate: new Date(currentYear, currentMonth, currentDate),
dateFormat: "yy-mm-dd"
});
</script>
in my foreach code the error is this one $allDatas->Date; it says Message: Undefined property: stdClass::$Date
and that's the field name in my table Date
can someone help me figured this out??
any help is much appreciated! thanks! can't find the error
In your model or controller - after the getAllData method is executed:
echo the result of $this->db->last_query()
Manually execute the returned query in your database, then fix your query
As the comments started, The issue is that Date isn't generated from the query, which means something is wrong in that level, not the output itself.
Make sure the query is correct, dump it before you load the view directly from the controller to see what it gets, if it doesn't get a Date, your issue is elsewhere, probably in the query itself.

PHP Codeigniter: fetch data in dropdown and populate into textbox

What I'm trying to say is: when you click dropdown or select, it will fetch the data of prod_temno, prod_desc,prod_selluom then populate to the textbox named: idesc and inventoryuom. I hope you can help me. Thanks in advance.
<table>
<tr><form name="form1">
<td>Item No:</td>
<td>
<select type="text" id="inventoryitemno" name="inventoryitemno" >
<?php
foreach($itemno as $row){
echo "<option id=".$row->prod_id.">".$row->prod_itemno."</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td>Description:</td>
<td><input type="text" id="idesc" name="idesc"/></td>
</tr>
<tr>
<td>UOM:</td>
<td><input type="text" id="inventoryuom" name="inventoryuom" /></td>
</tr>
</table>
This is my products_model
public function view_product_itemno(){
$this->load->database();
$data = $this->db->query('select prod_id, prod_itemno, prod_desc, prod_inventoryuom from product;');
return $data->result();
}
I hope this is clear for you guys. Thanks.
is this what you want?
<?php
$itemno = array('0'=>'a','1'=>'b','2'=>'c');
?>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.1.min.js" ></script>
<table>
<tr><form name="form1">
<td>Item No:</td>
<td>
<select type="text" id="inventoryitemno" name="inventoryitemno" onchange="javascript:set_to(this.value);" >
<?php
foreach($itemno as $key=>$val){
echo "<option id=".$key.">".$val."</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td>Description:</td>
<td><input type="text" id="idesc" name="idesc"/></td>
</tr>
<tr>
<td>UOM:</td>
<td><input type="text" id="inventoryuom" name="inventoryuom" /></td>
</tr>
</table>
<script type="text/javascript">
function set_to(id)
{
$('#idesc').val(id);
}
</script>
You can also refer this link -> chaneg event
I am assuming that you have to call the function when someone change the select box. You have to call the model through ajax and then put reponse into textbox:-
$('#inventoryitemno').change(function()
{
// write ajax code and call your model and dump the result into your textbox
$.ajax({
type:"POST",
data:"value="+$(this).val(),
url:"your_controller_path",
success:function(msg)
{
$('#idesc').val(msg);
}
});
});
Your controller should be like this say controller name is product_controller :-
function product_details()
{
$value = $this->input->post('value'); // value from ajax
$this->products_model->products_model($value);
echo "response"; //return the result back to ajax success function
}
What about "<DATALIST>"?
<input type=text name='somename' list='somelist'>
<datalist id="somelist">
<option value=1>One</option>
.
.
<option value=99>Ninety-Nine</option>
</datalist>
You can even fill the list from a DB with mysql
<datalist = id=somelist>
<?
$sql="select * from my_table";
$rs=mysql_query($sql,$mysql_connection);
$row=mysql_fetch_assoc($rs);
do {
printf("<option value='%s'>",$row['some_field'])}
while($row=mysql_fetch_assoc($rs));
Hope that helps.

Resources