Undefined array key error in Codeigniter 4 even when var_dump() is showing data - postgresql

This is my Controller Code:
public function edit_manpower($Manpower_id=null)
{
$session = session();
$db = \Config\Database::connect();
$builder = $db->table('Manpower');
$builder->select('Manpower.*');
$builder->where('Manpower_id', $Manpower_id);
$query = $builder->get();
$data['row'] = $query->getResultArray();
return view("admin/update_manpower_form",$data);
}
This is my View Code where the error arises:
<form action = "<?php echo site_url('Admin/updatemanpower/'.$row['Manpower_id']);?>" method=
"post" enctype="multipart/form-data">
<input type="hidden" name="_method" value="PUT"/>
<div class="form-group">
<div class=required-field><label for="Name">NAME:</label></div>
<input type="text" class="form-control" placeholder="Enter name" id="Name" value="<?php echo
$row['Name'];?>" name="Name" autocomplete="off">
</div>
<br>
<div class="form-group">
<div class=required-field><label for="Contact_no">CONTACT NUMBER:</label></div>
<input type="text" class="form-control" placeholder="Enter phone number" id="Contact_no"
value="<?php echo $row['Contact_no'];?>" name="Contact_no" autocomplete="off">
</div>
<br>
When I place the following line at the top of View, the required data is displayed:
<?php var_dump($row); ?>
The error message displayed is:
Undefined array key "Manpower_id"
APPPATH\Views\admin\update_manpower_form.php at line 65
64 <br><br>
65 <form action = "<?php echo site_url('Admin/updatemanpower/'.$row['Manpower_id']);?>"
method= "post" enctype="multipart/form-data">
66 <input type="hidden" name="_method" value="PUT"/>
I am unable to find what I am doing wrong. Kindly help.

Your $row maybe an Object array.
#std[]

Related

update in codeigniter 3, it's run but database didnt changed

i dont understand with my code. it's run, and work. but when i see in database didnt changed.
here's my controller, function perbarui for get id data, get the data id from model (MMobil).
public function perbarui($id = NULL){
$this->load->model('merek');
$this->load->library('form_validation');
$data['merk'] = $this->merek->getList();
$data['detail'] = $this->MMobil->detail($id);
$submit = $this->input->post('submit');
print_r($data);
if ($submit) {
$nomor_kendaraan = $this->input->post('nomor_kendaraan');
$nomor_mesin = $this->input->post('nomor_mesin');
$id_merek = $this->input->post('id_merek');
$tahun_beli = $this->input->post('tahun_beli');
$nama_mobil = $this->input->post('nama_mobil');
$this->form_validation->set_rules('nama_mobil', 'Nama_Mobil', 'required');
$this->form_validation->set_rules('nomor_kendaraan', 'Nomor_Kendaraan', 'required');
$data['detik'] = $this->MMobil->setData($nomor_kendaraan, $nomor_mesin, $id_merek, $tahun_beli, $nama_mobil);
if ($this->form_validation->run() == FALSE) {
$data['errors'] = TRUE;
}else{
$data['detail'] = $this->MMobil->detail($id);
print_r($data);
}
//redirect('Master');
}
$this->load->view('master-detail-mobil', $data);
}
here's my models. function setData for storage variable after click submit. function edit get id data and get variable arrayData from function setData
public function setData($nomor_kendaraan, $nomor_mesin, $id_merek, $tahun_beli, $nama_mobil){
$this->nomor_kendaraan = $nomor_kendaraan;
$this->nomor_mesin = $nomor_mesin;
$this->id_merek = $id_merek;
$this->tahun_beli = $tahun_beli;
$this->nama_mobil = $nama_mobil;
}
public function edit($id){
$arrayData = array(
'nomor_kendaraan' => $this->nomor_kendaraan,
'nomor_mesin' => $this->nomor_mesin,
'id_merek' => $this->id_merek,
'tahun_beli' => $this->tahun_beli,
'nama_mobil' => $this->nama_mobil,
);
$this->db->where('nomor_mesin', $id);
return $this->db->update($this->table,$arrayData);
}
here's my views
<?php $this->load->view('header.php'); ?>
<?php
echo"<h4>UPDATE DATA</h4>";
?>
<form class="form-horizontal" role="form" method="post" action="<?php echo site_url()?>/Master/perbarui/">
<div class="form-group">
<label class="control-label col-sm-2" for="noken">Nomor Kendaraan :</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="nomor_kendaraan" value="<?php
echo #$detail->nomor_kendaraan;
?>">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="nomes">Nomor Mesin :</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="nomor_mesin" value="<?php
echo #$detail->nomor_mesin;
?>">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="merk">Merek Mobil :</label>
<div class="col-sm-10">
<select class="form-control" name="id_merek">
<?php foreach ($merk as $mrk) :?>
<option value="<?php echo $mrk->id_merek ?>">
<?php
echo $mrk->nama_merek;
?>
</option>
<?php endforeach?>
</select>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="thn">Tahun Beli :</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="tahun_beli" value="<?php
echo #$detail->tahun_beli; ?>">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="namo">Nama Mobil :</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="nama_mobil" value="<?php
echo #$detail->nama_mobil; ?>">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="nomes"></label>
<div class="col-sm-2">
<input type="submit" class="form-control" name="submit" value="simpan">
</div>
</div>
</form>
<?php $this->load->view('footer.php'); ?>
and here my pictures before click submit and after click submit. i typed print_r for see the data array. if you see data array detail before click submit, there is data in detail. but after click submit data array detail gone.
enter image description here enter image description here

how to manipulate with codeigniter

How I get my data array from view? and how to insert in my database?
my view, I try much code but still an error, sorry about my question and my bad English
<form role="form" action="<?php echo base_url().'potongan/validasi'?>" action="GET">
<div class="box-body">
<div class="box-header with-border">
<div class="row">
<div class="col-xs-3">
<label >NIM</label>
<input type="text" class="form-control" name="nim" id="nim" placeholder="NIM" value="<?php echo $nime ?>" readonly>
</div>
<div class="col-xs-3">
<label >Nama Mahasiswa</label>
<input type="text" class="form-control" name="namamhs" placeholder="Nama Mahasiswa" value="<?php echo $nim[0]->namamhs ?>" disabled>
</div>
<div class="col-xs-3">
<label >Jurusan</label>
<input type="text" class="form-control" name="jurusan" placeholder="Jurusan" value="<?php echo $nim[0]->jurusan ?>" disabled>
</div>
</div>
<br>
<div class="form-group">
<label >Kode Beasiswa</label>
<input type="text" class="form-control " name="kdbeasiswa" value="<?php echo $kdbeasiswa ?>" readonly>
</div>
<div class="form-group">
<label >Keterangan</label>
<input type="text" class="form-control " name="keterangan" value="<?php echo $keterangan ?>" readonly>
</div>
<div>
<table class="table table-bordered table-striped">
<tr>
<th></th>
<th>Kode</th>
<th>Deskripsi</th>
<th>Tahun Akademik</th>
<th>semester</th>
<th>Tagihan</th>
<th>Beasiswa</th>
<th>Terbayar</th>
<th>Potongan</th>
</tr>
<?php foreach ($nim as $n): ?>
<tr>
<td>
<input type="hidden" class="form-control " name="id[]" value="<?php echo $n->thakad.$n->smtakad.$n->kdkeu; ?>" maxlength="3" >
</td>
<td><input type="hidden" class="form-control " name="kdkeu[]" value="<?php echo $n->kdkeu; ?>" maxlength="3" ><?php echo $n->kdkeu; ?></td>
<td><?php echo $n->deskeu; ?></td>
<td><input type="hidden" class="form-control " name="thakad[]" value="<?php echo $n->thakad; ?>" maxlength="3" ><?php echo $n->thakad; ?></td>
<td><input type="hidden" class="form-control " name="smtakad[]" value="<?php echo $n->smtakad; ?>" maxlength="3" ><?php echo $n->smtakad_view; ?></td>
<td><?php echo $n->tagihan; ?></td>
<td><?php echo $n->beasiswa; ?></td>
<td><?php echo $n->terbayar; ?></td>
<td>
<input type="text" class="form-control " name="prosentase_bea[]" value="<?php echo $n->potongan; ?>" maxlength="3">
</td>
</tr>
<?php endforeach ?>
</table>
</div>
</div>
<br>
<button class="btn btn-info" style="float: right;">submit</button>
</div>
<!-- /.box-body -->
</form>
my controller looks like this when I submit no data insert to the database and error like
Filename: C:/xampp/htdocs/tagihan/system/database/DB_query_builder.php
Line Number: 1498
$data = array();
$count = count($this->input->post('device'));
for($i=0; $i<$count; $i++) {
$data[] = array(
$thakad= $this->input->get('thakad'),
$smtakad= $this->input->get('smtakad'),
$kb = $this->input->get('kdbeasiswa'),
$kk = $this->input->get('kdkeu'),
$nim= $this->input->get('nim'),
$pb = $this->input->get('prosentase_bea'),
$status = 1,
$user = $this->session->userdata('username'),
$kt = $this->input->get('keterangan'),
$curentDate = date("Y-m-d"));
}
$this->db->insert_batch('keu_beasiswapermhs',$data);
redirect('http://localhost/tagihan/potongan');
Make a habit using the proper MVC
VIEW
<form action="URL" method="post">
</form>
CONTROLLER
$this->model->function_name();
redirect('URL');
MODEL
function function_name(){
$dataArr = array(
'field_name'=>$this->input->post('form_input_name'),
);
$this->db->INSERT('tbl_name',$dataArr);
}
You have fields like "namamhs" and "jurusan" that are not being put into the db.
You do not need for or while loops to insert data
You are not assigning what fields in your db each posted item goes to. Your controller has no idea where each value should be inserted. Do not use get, you should use post. Here is an example for your controller you can adapt to your form:
$data = [
'url' => $this->input->post('url'),
'name' => $this->input->post('name'),
'genre' => $this->input->post('genre'),
'counter' => $this->input->post('counter'),
'type' => $this->input->post('type')
];
where 'url' is a field in your db that you are putting $this->input->post('url') into.
Next you check the values:
$this->form_validation->set_rules('url', 'url', 'required|trim');
$this->form_validation->set_rules('name', 'Name', 'required|trim|callback_trims');
$this->form_validation->set_rules('genre', 'Genre', 'required|trim');
$this->form_validation->set_rules('counter', 'Counter', 'numeric');
$this->form_validation->set_rules('type', 'Type', 'required|min_length[3]');
If the values pass the validation, inserting is simple
if( $this->form_validation->run() == FALSE) {
echo validation_errors();
}else
{
$this->db->insert('music', $data);
}
You do not need batch insert. Just use insert
Your $data array is empty...always check if array not empty before insert batch...that cause error DB_query_builder.php Line Number: 1498

Can't receive all contents of mail

(This are just some of my code for the HTML to cut it short), i don't get it why i can't get the value inputted by the user for the email while i can get values of others like NAME, PHONE, COMPANY etc. can you help me what's wrong? i'm a beginner so please try to be nice. THANK YOU
HTML:
<form action="contact_sendmail.php" method="post">
<label for="first name"> First Name *</label><br>
<input class="textbox" type=text autofocus id="Fname" name="Fname" maxlength="50"size="30" value="" required>
<br>
<label for="last name"> Last Name *</label><br>
<input class="textbox" type=text id="Lname" name="Lname" maxlength="50"size="30" value="" required>
<br>
<label for="email">Email * </label> <br>
<input class="textbox" type="text" id="email" maxlength="50"size="30" value="" required>
<input class="button" name="submit" type="submit" value="Submit">
MAIL FUNCTION:
<?php
if(isset($_POST['submit'])){
$msg = 'Name: '.$_POST['Lname'] .$_POST['Fname'] ." \n"
.'Email: '.$_POST['email'] ."\n"
.'Phone Number: '.$_POST['phone'] ."\n"
.'Company: '.$_POST['company'] ."\n"
.'Number of Employees: '.$_POST['employee_num'] ."\n"
.'Comment: '.$_POST['comment'] ;
mail('example#gmail.com', 'sample contact us Form', $msg);
?>
"<script>" alert('successfully sent'); "</script>"
<?php
}else {
header('location:contact.php');
exit(0);}
?>

mark the input box red if the required field of a form is not filled

i'm using Codeigniter. i have a form which takes information about users. What i want to do is to check whether all the required fields are filled up or not.If any of the required fields is not filled up i want mark that input box red. Right now my codes only check if the required fields are filled up or not. if not it says the "field is required" but how to mark input box.I'm a bit confused how to do this thing.Can somebody help me out with a little hint. Thanks.
the view for my form:
<?php
$attributes = array('class' => '', 'id' => '');
echo form_open('register', $attributes); ?>
<p>
<label for="name">Name <span class="required">*</span></label>
<?php echo form_error('name'); ?>
<br /><input id="name" type="text" name="name" value="<?php
echo set_value('name'); ?>"
</p>
<p>
<label for="username">Username <span class="required">*</span></label>
<?php echo form_error('username'); ?>
<br /><input id="username" type="text" name="username" value="<?php echo set_value('username'); ?>"
</p>
<p>
<label for="password">Password <span class="required">*</span></label>
<?php echo form_error('password'); ?>
<br /><input id="password" type="password" name="password" value="<?php echo set_value('password'); ?>"
</p>
<p>
<label for="email">Email <span class="required">*</span></label>
<?php echo form_error('email'); ?>
<br /><input id="email" type="text" name="email" value="<?php echo set_value('email'); ?>"
</p>
<p>
<label for="phone">Phone</label>
<?php echo form_error('phone'); ?>
<br /><input id="phone" type="text" name="phone" value="<?php echo set_value('phone'); ?>"
</p>
</p>
<p>
<input type="submit" value="Submit information" class="formbutton"/>
</p>
<?php echo form_close(); ?>
Generally, I'd recommend creating an textInputError class where you adjust the input style, and then apply it based on the existence of the error...
class="<?php echo (form_error('username') ? 'textInputError' : '') ?>"
in place in the input element...
<input id="username" type="text" name="username" value="<?php echo set_value('username'); ?>" class="<?php echo (form_error('username') ? 'textInputError' : '') ?>">

Codeigniter Form Helper Duplicate from_hidden?

Im totally stumped.. Im creating some form elements using the CI form helper and for some weird reason, its creating a duplicate version.
Here is my PHP
<div id="receiveInventoryItemDetails">
<p><?php echo form_open('#', array("class" => "nyroModal form label-inline"));?></p>
<?php echo form_hidden('item_id', '', "readonly = true"); ?>
<?php echo form_hidden('purchase_order_id', '', "readonly = true"); ?>
<p><?php echo form_label('Item Name', 'item_name');?><?php echo form_input('item_name', '', "readonly = true"); ?></p>
<p><?php echo form_label('Item QTY', 'item_qty');?><?php echo form_input('item_qty', ''); ?></p>
<?php echo form_close();?>
</div>
<div class="buttonrow">
<button class="btn-sec" onclick="inventoryC.receiveSubmitItem();"><span>Add To Inventory</span></button>
</div>
Here is the HTML output
<div id="receiveInventoryItemDetails">
<p><form action="https://mysite.com/#.abl" method="post" accept-charset="utf-8" class="nyroModal form label-inline"></p>
<input type="hidden" name="item_id" value="" />
<input type="hidden" name="item_id" value="" />
<input type="hidden" name="purchase_order_id" value="" />
<p><label for="item_name">Item Name</label><input type="text" name="item_name" value="" readonly = true /></p>
<p><label for="item_qty">Item QTY</label><input type="text" name="item_qty" value="" /></p>
</form>
</div>
<div class="buttonrow">
<button class="btn-sec" onclick="inventoryC.receiveSubmitItem();"><span>Add To Inventory</span></button>
</div>
You can't use html attributes via third parameter. Look at form helper source code
This should be work:
<?php echo form_hidden('item_id', ''); ?>