I cant seem to update the Phone Number, I am new to Codeigniter and been trying to figure out what I am doing wrong.
This is my MODEL:
function updatePhone($submit_userid, $submit_phone) {
$this->db->where("intMemberID", $submit_userid);
$this->db->update("tblMembers", array(
"strPhoneNumber" => $submit_phone
));
}
This is my CONTROLLER:
public function updateAccount() {
$this->load->model('updateModel');
$this->load->model('userModel');
$data['userdata'] = $this->session->userdata('userobject');
$this->form_validation->set_rules('phone', 'Phone', 'required|min_length[8]|max_length[12]|xss_clean');
if ($this->form_validation->run() == false) {
$this->load->view('updateView', $data);
} else {
$submit_userid = $this->input->post('userid');
$submit_phone = $this->input->post('phone');
$this->userModel->updatePhone($submit_userid, $submit_phone);
redirect('updateController/updateAccount');
}
}
This is my VIEW:
<?php
echo validation_errors();
echo form_open('updateController/updateAccount');
echo '<input type="hidden" name="userid" value=' . $userdata->intMemberID . '/> <br/>';
echo "$userdata->strMemberFirstName $userdata->strMemberLastName";
echo "<br>";
echo "Phone Number: <input type='text' name='phone' placeholder='$userdata->strPhoneNumber'/>";
echo '<INPUT type="submit" value="Update" name="submit"/>';
echo form_close();
?>
you can try this update statement, even if this doesnot works out then please check the column type you are using (varchar or whatever and its size if its conflicting or not)
<?php
$data['strPhoneNumber'] = $submit_phone;
$this->db->where('intMemberID', $submit_userid)->update('tblMembers', $data);
?>
You never perform an update to the database. The select statement is not needed in an update, you pass the table name to the update method.
function getUpdate($submit_userid, $submit_phone) {
$this->db->where("intMemberID", $submit_userid);
$this->db->update("tblMembers", array(
"strPhoneNumber" => $submit_phone
));
}
And you shouldn't really call the method getUpdate, it's not really semantic since you're not actually getting anything. updatePhone might be a better name.
try to print out the error
return $this->db->update("tblMembers", array );
var_dump the updatePhone result and you will see whether there is an error with table or something else
make sure it returns true before redirecting
$data = array(
"strPhoneNumber" => $submit_phone
);
$where = "intMemberID = ".$intMemberID;
//generate the query string not executing
$str = $this->db->update_string('tblMembers', $data, $where);
echo '<pre>';
echo $str;
die;
Put the above code in your function, verify the query string
Related
I'm a new user of CodeIgniter 4. I got this error when I want to show 'prodi_name' of table 'prodi' on a form.
This is my ProdiModel
protected $table = 'prodi';
protected $primaryKey = 'id_prodi';
public function getProdi($id = false)
{
if ($id == false) {
return $this->findAll();
}
return $this->where('prodi_code', $id)->first();
}
I have setting my BaseController with
$this->prodiModel = new \App\Models\ProdiModel();
so my Controller is
public function add()
{
$data = [
'title' => 'Admin',
'prodi' => $this->prodiModel->getProdi(),
'validation' => \Config\Services::validation()
];
return view('admin/add', $data);
}
And this is my form, I want to show the prodi_name on a multiple select field
<div class="form-group">
<label>Prodi</label>
<select class="form-control select2 multiple" id="prodi" name="prodi[]" multiple="multiple" data-placeholder="Select prodi">
<?php
foreach ($prodi as $prodi) :
echo "<option value='" . $prodi["prodi_name"] . "'";
if (old('prodi')) {
if (in_array($prodi['prodi_name'], old('prodi'))) echo "selected";
}
echo ">" . $prodi['prodi_name'] . "</option>";
endforeach; ?>
</select>
</div>
Please help me to solve this problem. Thanks
first of all in CI 4 there is no first() method,
use
public function getProdi($id = false)
{
if ($id == false) {
return $this->findAll();
}
return $this->where('prodi_code', $id)->getFirstRow(‘array’);
}
On the second thought you are returning two types of data here, if id is false you return multidimensional array and if id is there code returns single row.
Again in the same method, you have called findAll(), if it returns and associative array, your code should run, but if that returns a object then you should change to,
<?php
foreach ($prodi as $prodi) :
echo "<option value='" . $prodi->prodi_name . "'";
if (old('prodi')) {
if (in_array($prodi->prodi_name, old('prodi'))) echo "selected";
}
echo ">" . $prodi->prodi_name . "</option>";
endforeach; ?>
Can somebody tell me why my update did not work ?
Here are the data in my variable
$id = '1';
$table = 'tb_home_content';
$data = Array ( [title] => SLEEK DESIGN [content] => We have a sleek design which not only attractive but simple in design for our user to see. [classicon] => fa fa-clone );
Here are my model function
public function updateData($table, $data)
{
$id = $data['id'];
array_shift($data);
// echo $id.'<br>';
// echo $table.'<br>';
// print_r($data);
$this->db->where('id', $id);
$this->db->update($table, $data);
if($this->db->affected_rows() == '1')
{
echo "<script>";
echo "alert('Success');";
echo "</script>";
}
else
{
echo "<script>";
echo "alert('Failed');";
echo "</script>";
}
What is the error message you are getting? Are you sure the value at the position of id is the first element in the array?
I am making an semi-automatic attendance register so that whenever a person submits his or her name a table will get updated and change their status.
Here is the code for the form which I made:
<form action="Attendance.php" method="post">
<input type="text" name="name" placeholder="Name of
Employee" style="width: 210px;"><br>
<input type="number" name="refNo" placeholder="Reference
Number of Employee" style="width: 210px;" disabled="disabled"><br>
<input type="submit" name="submit" value="Submit"><br>
</form>
Here is the code I wrote which is a function. This function checks if the person present or not and change a variable that imports its value from the form I mentioned above:
<?php
function check_attendance($eNAME, $NAME, $eNUM){
if ($eNAME == $NAME) {
$eNUM = 'Present';
}
else{
$eNUM = 'Absent';
}
}
$eNAME = $_POST['name'];
//$eNAME is the name of the employee given in form I wrote above.
$eNUM1 = '';
//$eNUM1 is the status of the employee that is whether an employee is present or absent.
check_attendance($eNAME, 'John D', $eNUM1);
echo $eNUM1;
?>
I have yet not written the code to integrate the person being present or absent into a table.
I cannot get to update $eNUM1 using function check_attendance.
There are two ways of achieving this, the first using virtually the same code as you have is to pass the variable by reference...
function check_attendance($eNAME, $NAME, &$eNUM){
this allows the function to alter the value in the calling scope.
BUT
As you want to just set a value from the function, it would be better(IMHO) to just return the value...
function check_attendance($eNAME, $NAME){
if ($eNAME == $NAME) {
$eNUM = 'Present';
}
else{
$eNUM = 'Absent';
}
return $eNUM;
}
$eNAME = $_POST['name'];
//$eNAME is the name of the employee given in form I wrote above.
//$eNUM1 is the status of the employee that is whether an employee is present or absent.
$eNUM1 = check_attendance($eNAME, 'John D' );
echo $eNUM1;
You need to return $eNum
<?php
function check_attendance($eNAME, $NAME, $eNUM){
if ($eNAME == $NAME) {
$eNUM = 'Present';
} else{
$eNUM = 'Absent';
}
return $eNUM;
}
$eNAME = $_POST['name'];
//$eNAME is the name of the employee given in form I wrote above.
$eNUM1 = '';
//$eNUM1 is the status of the employee that is whether an employee is present or absent.
$eNUM1 = check_attendance($eNAME, 'John D', $eNUM1);
echo $eNUM1;
?>
hi am making this edit function in my code igniter hmvc project. i am getting the value of the things i want to edit, and it is posted to the textbox i want to. but, i cannot save the changes. my model is this,
public function update(){
$sql = "UPDATE $this->table SET PROVINCE = ? WHERE PROV_ID = ?";
$input = array(
'PROVINCE' => $this->input->post('PROVINCE'),
'PROV_ID' =>$this->uri->segment(3)
);
// print_r($input);
// exit;
$query = $this->db->query($sql, $input);
return $query;
}
when i print_r the input, it says
Array ( [PROVINCE] => Province [PROV_ID] => )
i think dont get the uri value. how can i fix this?
here is my controller
/// EDIT
public function update(){
$data['content'] = $this->Provinces_Model->getrow();
$data['content_view'] = 'Provinces/edit_view';
$this->templates->admin_template($data);
}
public function update_row(){
if($this->Provinces_Model->update()){
redirect('Provinces/index');
}else{
$this->update();
}
}
}
here is my full model
//// EDIT
public function getrow(){
$this->db->where('PROV_ID', $this->uri->segment(3));
$query = $this->db->get($this->table);
return $query->row();
}
public function update(){
$sql = "UPDATE $this->table SET PROVINCE = ? WHERE PROV_ID = ?";
$input = array(
'PROVINCE' => $this->input->post('PROVINCE'),
'PROV_ID' =>$this->uri->segment(3)
);
// print_r($input);
// exit;
$query = $this->db->query($sql, $input);
return $query;
}
here is my edit view
<?php
echo form_open('Provinces/update_row');
?>
<p>
<label class="field" for="PROVINCE"><span>*</span>Province Name:</label>
<input type = "text" name="PROVINCE" class ="textbox-300" value= "<?php echo $content->PROVINCE; ?>">
<label class = "error"><?php echo form_error("PROVINCE"); ?></label>
</p>
<?php
echo form_submit('submit','Update');
echo form_close();
?>
here is the part of my table where i click to edit
<td><?php echo anchor("Provinces/update/$i->PROV_ID","<i class='fa fa-edit'>" ); ?></td>
Try this
public function update() {
$input = array(
'PROVINCE' => $this->input->post('PROVINCE'),
'PROV_ID' =>$this->uri->segment(3)
);
$this->db->where('prov_id',$input['PROV_ID']);
$this->db->update('province',$input);
}
get the all segments of URI in an array
For that use this line...
$segs = $this->uri->segment_array();
foreach ($segs as $segment)
{
echo $segment;
echo '<br />';
}
and use that specific key to get the ID or something else!!
i want to search coupon number in a row if coupon number is match given by user get all values in this single row using Codeigniter.
Here is My Code.
Model
public function ven_coupon($coupon)
{
$where = array(
'number' => $coupon,
);
$this->db->select()
->from('vendor_coupons')
->where($where);
$data = $this->db->get();
return $data->result_array();
}
Controller
public function ven_coupon()
{
$this->load->model('front');
if ($_POST) {
$number = $_POST['coupon'];
if (!$this->front->ven_coupon($number)) {
echo "Not Valid";
}
$payment = $this->cart->total();
$discount['discount'] = ($payment*10)/100;
$this->load->view('checkout',$discount);
}
}
View
<form action="<?=base_url();?>home/ven_coupon" method="post">
Coupon Number: <input type="text" name="coupon">
<input type="submit" name="submit" value="Apply Coupon">
</form>
You question is not clear what actually you need. But if you want all values from database than you need to modify your controller as:
$data = $this->front->ven_coupon($number);
if(count($data) <= 0){
echo "not valid"; // print error
}
else{
print_r($data); // print all data
}
This is basic example you can store them into variable and pass into load view.