Update database table from Radio button. I'm using Codeigniter 4 - php

I can't update my table. Anyone to help me
Code in view :
<form method="post"
<?php echo form_open_multipart('home/savecovid') ?>>
<div class="form-group">
<input type="radio" name="anotasi1" value="Positif"
<?php echo set_radio('anotasi','positif'); ?>/>Positif<br>
<input type="radio" name="anotasi1" value="Negatif"
<?php echo set_radio('anotasi','negatif'); ?>/>Negatif<br>
<input type="radio" name="anotasi1" value="Netral"
<?php echo set_radio('anotasi','netral'); ?>/>Netral
</div>
<button type="submit" name="saveCovid">Submit</button>
</form>
Code in my controller :
public function savecovid()
{
$db = \Config\Database::connect();
$anotasi1 = $this->request->getPost('anotasi1');
$builder = $db->table('tbl_anotasi');
$data = [
'anotasi' => $anotasi1
];
$builder->set('anotasi');
$builder->update($data);
}
I want to update my table
My Table

use model oky and entity
$model =new Mymodel();
$model->update($id,$data)
go this link
https://codeigniter.com/user_guide/models/model.html

Related

Codeigniter Form post controller advice

would like some advice/help on how to connect form controller to post form method in my CI site. I want to data submitted from one viewer to another. Thank you for the help!!
Here is the controller Im using (Form.php), took if from another site:
Form.php
<?php
class Form extends CI_Controller {
public function __construct() {
parent::__construct();
}
// Show form in view page i.e view_page.php
public function form_show() {
$this->load->view("addEdit");
}
// When user submit data on view page, Then this function store data in array.
public function data_submitted() {
$data = array(
'file_name' => $this->input->post('file'),
'title' => $this->input->post('title')
);
// Show submitted data on view page again.
$this->load->view("profile", $data);
}
}
?>
Its to connect to this code:
addEdit.php
<form method="post" action="postAction.php" enctype="multipart/form-data">
<div class="form-group">
<label>Image</label>
<?php if(!empty($imgData['file_name'])){ ?>
<img src="uploads/images/<?php echo $imgData['file_name']; ?>">
<?php } ?>
<input type="file" name="image" class="form-control" >
</div>
<div class="form-group">
<label>Title</label>
<input type="text" name="title" class="form-control" placeholder="Enter title" value="<?php echo !empty($imgData['title'])?$imgData['title']:''; ?>" >
</div>
Back
<input type="hidden" name="id" value="<?php echo !empty($imgData['id'])?$imgData['id']:''; ?>">
<input type="submit" name="imgSubmit" class="btn btn-success" value="SUBMIT">
</form>
When I first tried to make it work I got this error:
404 Page Not Found
The page you requested was not found.
http://culturedkink.com/index.php/register/postAction.php(the url)
postAction.php is the form Im trying to get the data to work from
The end result is to have info submitted from addEdit.php be seen on profile.php with the help of postAction.php
make routes for it first.
config/routes.php
$route['add'] = 'Controller_name/data_submitted';
$route['edit/(:any)'] = 'Controller_name/data_submitted/$1';
where is your add/edit button put this there
for add
Add New
for edit button
$row['id'] is an example i m giving. you can get data by name and id..whatever you want.
Update
//controller
public function data_submitted($id=0) {
$data=array();
$data['dataDetails']=$this->get_profile_data_by_id($id);
$data['view'] = 'folder_name/addEdit';
if ($id > 0) {
$profileArray = [
'file_name' => $this->input->post('file'),
'title' => $this->input->post('title')
];
if ($this->User_model->editById($id, $profileArray)) {
$id = $id;
}
}
else{
$profileArray = [
'file_name' => $this->input->post('file'),
'title' => $this->input->post('title')
];
if ($this->User_model->add($id, $profileArray)) {
$id = $id;
}
}
$this->load->view("profile", $data);
}
form view page
<?php echo isset($dataDetails) ? "Update" : "Add"; ?>
First check your form method and action. Your action does not exist. First check how CI works with form. The action should have a method declared in a controller. The url looks like this,
When you submit the form the data will be submitted in this method. Whatever you need to do with this form data you can do that in this method.

Need to insert a single data in the database

I'm having a problem to insert a single data from my database
Example: I have two data's which is the question1 with ID:1 and question2: with ID:2. Those 2 questions does have different button. The problem is once I click the question1 or question2 button it inserting both of the ID's in my database.
Like this:
Here's My Controller
$data['posts'] = $this->Post_Model->get_posts();
$this->load->view('templates/header');
$this->load->view('posts/index', $data);
$this->load->view('templates/footer');
$this->load->library('form_validation');
if($this->form_validation->run()==FALSE) {
if($this->input->post("add")) {
$this->Post_Model->count_up();
redirect('posts');
}
}
My Model
function count_up(){
for($i=0; $i<count($this->input->post('hidden')); $i++){
$data = array(
'post_id' => $this->input->post("hidden[$i]")
);
$this->db->insert("userspost", $data);
}
}
My View
<?php
$iq = 0;
$i = 0;
$arrtry = array();
foreach($posts->result() as $post){
?>
<br>
<div class="card card-nav-tabs">
<div class="card-header card-header-primary">
<!-- colors: "header-primary", "header-info", "header-success", "header-warning", "header-danger" -->
<div class="nav-tabs-navigation">
<input class="form-control" value="<?php echo $arrtry[$iq++] = $post->id; ?>" name="hidden1[<?php $i; ?>]" type="hidden">
<input class="btn btn-primary" type="submit" name="add" value="UP" />
</div>
</div>
</div>
<?php
$i++;
}
?>
My problem is i want to insert question2 ID without inserting question1 ID. Hope you guys can help thanks!
What is happening is that you have all your inputs within the same form with multiple submit buttons.
I can imagine a few ways you could solve this:
Ajax way. Require some JS and another controller method.
One form for each button: So you will always have one $this->input->post('hidden'). No need to iterate over it.
With only one form You could set the button for something like this:
<input class="btn btn-primary" type="submit" name="add-up" value="<?= $i; ?>" />
So on your controller/model you can get the index that was clicked:
/* controller/model */
$index = $this->input->post('add-up');
$hidden_value = $this->input->post("hidden")[$index]

What to do when a declare array variable is undefined at view but working with controller laravel 5.7

Here is the code:
public function chat($id=1){
Route::view('/chat', 'chat');
$id = View::make('chat.blade', ['reviewer_id' => Reviewer::findOrFail($id)]);
$audiences = DB::table('audience')->get();
$data = [
'id'=>$id,
'audiences'=>$audiences,
'audience_id'=> 2
];
return View::make('chat.blade', ['data'=>$data]);
}
As the code is simple I route to blade view, get data from database, get audience data, initialize data array return data to chat.blade simple code but in view
Undefined variable: data (View:
/Users/userinfo/Sites/chat/resources/views/chat.blade.php)
View code:
<div>
#foreach($data->audiences as $info->audience)
{{$info->audience->id}};
#endforeach
</div>
<div>
<form action="/" method="post">
<input type="hidden" value={{$reviewer_id}} name="id">
<input type="hidden" value={{$audience_id}} name="id">
<input type="text" name="message">
<input type="submit" value="submit">
</form>
</div>
<?php $__currentLoopData = $data->audiences; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $info->audience): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
<?php echo e($info->audience->id); ?>;
<?php endforeach; $__env->popLoop(); $loop =
$__env->getLastLoop();
?>
> undefined variable $data
change $data->audiences to $data['audiences'] in your view. $data is an array not an object
If i do it then i do it like this
public function chat($id = 1){
Route::view('/chat', 'chat'); // I don't know what that is
$reviewer_id = Reviewer::findOrFail($id); // or Reviewer::find($id);
//I Update this little bit : $audiences = DB::table('audience')->get();
$audiences = Audience::all();
$audience_id = 2 ;
return view('chat.blade', compact(['id','audiences','audience_id','reviewer_id']));
}
Now you can access all the variables passed in compact in your blade file like this
<div>
// Depends on what is in the $audiences could be with "$key => $value"
#foreach($audiences as $key)
{{$key->id}};
#endforeach
</div>
<div>
<form action="/" method="post">
<input type="hidden" value={{$reviewer_id}} name="id">
<input type="hidden" value={{$audience_id}} name="id">
<input type="text" name="message">
<input type="submit" value="submit">
</form>
</div>

Codeigniter not updating my data

So I'm new to codeigniter and developing a viewing catalogue website. And so far my update function isn't working and I've been at this for 3 hours
so here's my view:
<?php echo validation_errors(); ?>
<?php echo form_open('catalogPages/updateEvent'); ?>
<input type="hidden" name="id" value="<?php echo $events['event_id']; ?>">
<div class="form-group">
<label>Title</label>
<input type="text" class="form-control" name="title" placeholder="Add
Title" value= "<?php echo $events['event_name']; ?>">
</div>
<div class="form-group">
<label>Body</label>
<textarea id="editor1" class="form-control" name="body" placeholder="Add
Body" value = "<?php echo $events['event_desc']; ?>"></textarea>
</div>
<button type="submit" class="btn btn-default">Submit</button>
My Model:
public function editEvent()
{
$data = array(
'event_name' => $this->input->post('event_name'),
'event_desc' => $this->input->post('event_desc'),
'event_id' => $this->input->post('event_id')
);
$this->db->where('event_id', $this->input->post('event_id'));
return $this->db->update('events', $data);
}
Controller:
public function editEvent($id = NULL)
{
$this->load->model('event_model');
$data['events'] = $this->event_model->get_event($id);
$this->load->view('adminEventUpdate', $data);
}
public function updateEvent()
{
$this->load->model('event_model');
//$id = $this->input->posts('event_id');
$this->event_model->editEvent();
}
It works but so far when I try to update the data doesn't seem to be updated but rather doesn't change at all.
When this Html form is submitted, the $_POST array has the keys:
'id', 'title', and 'body'.
There are no inputs named 'event_id', 'event_name', 'event_desc'.
The Html input properties 'name' need to change.

Post not working from text input, with CI

I'm trying to post some information from a from do so some calculations, however for some reason that data isn't posting. I do a var_dump after I get the info, and it still is blank. Any help would be great.
My Controller:
<?php
class Timevalueshow extends Controller{
function index(){
$this->load->view('Timevalueshow_view');
}
function submit(){
$years = $this->input->post('years');
$rate = $this->input->post('rate');
$principle = $this->input->post('principle');
$periods = $this->input->post('periods');
$isCont = $this->input->post('continuous');
var_dump($years);
$params = array(
'years' => $years,
'rate' => $rate,
'principle' => $principle,
'periods' => $periods,
'isCont' => $isCont
);
var_dump($params);
$this->load->library('timevalue',$params);
$this->timevalue->leprint();
}
}
?>
And the view file.
<head>
<title>Time Value of Money</title>
</head>
<body>
<div id="container">
<form method="POST" action="http://localhost:8888/CodeIgniter_1.7.2/index.php/timevalueshow/submit">
<p>Years: </p> <input id="years" type="text" />
<p>Rate: </p> <input type="text" id="rate"/>
<p>Principle: </p> <input type="text" id="principle"/>
<p>Periods: </p> <input type="text" id="periods"/>
<p>Continuous?: </p> <input type="checkbox" id="continuous"/>
<input type="submit" value="Submit"/>
</form>
</div>
</body>
You didn't submitted the form elements' name's only their id's. Edit your form so the elements contain name attributes too.

Categories