Im trying without success to update a row in a table. I mean, when i click on "update" button it goes to another page and i got the previous "values" so i can edit them and finally click on "save".
But it is not working in my program :/..When i click on "edit" button it goes to another page and i only have the previous values of a single field (Field subject). AND when i click on save it does not update anything, it creates a new row.
Here is my code:
The index file, here i put the button "edit".
<a href='".site_url('Home/editar')."/$record->id'>
<button type='button' class='btn btn-primary'>EDIT</button></a>
My controller file, with the function "editar" and "saveupdate":
public function editar($id){
$data['carreras'] = $this->Crudmodel->get_carreras();
$data['record']=$this->Crudmodel->get_id_row($id);
$this->load->view('editar',$data);
}
public function saveupdate(){
$id=$this->input->post("txtid");
$txtcarr=$this->input->post("txtcarr");
$txtmat=$this->input->post("txtmat");
$txtdesc=$this->input->post("txtdesc");
$txtcarga=$this->input->post("txtcarga");
$this->Crudmodel->saveup($txtcarr,$txtmat,$txtdesc,$txtcarga);
$this->db->where("id", $id);
redirect('Home/index');
}
The Crudmodel with the function "saveup"
public function saveup($txtcarr, $txtmat, $txtdesc, $txtcarga){
$data=array(
'carrera_id'=>$txtcarr,
'nombre'=>$txtmat,
'descripcion'=>$txtdesc,
'carga_horaria'=>$txtcarga
);
$this->db->update('materias', $data);
}
Here is the get_id_row from crudmodel:
Function
Finally, the "editar" file which contains all the fields.
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<h2 align="center">UPDATE SUBJECTS</h2>
<form method="post" action='<?php echo site_url('Home/saveupdate'); ?>'>
<tr>
<td><input type="text" hidden name="txtid" value="<?php echo $record->id ?>"/></td>
</tr>
<tr>
<td>
<select name="txtcarr">
<?php foreach($carreras as $item):?>
<option value="<?php echo $item->id;?>"><?php echo $item->nombre;?></option>
<?php endforeach;?>
</select>
</td>
</tr>
<tr>
<td>Subject : </td>
<td><input type="text" name="txtmat" value="<?php echo $record->nombre ?>"/></td>
</tr>
<tr>
<td>Description : </td>
<td><textarea name="txtdesc" value="<?php echo $record->descripcion ?>"></textarea></td>
</tr>
<tr>
<td>Hours : </td>
<td><input type="text" name="txtcarga"/></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Save"/></td>
</tr>
<table class="table table-hover" align="center" border="0" cellspacing="0" cellpadding="0" width="300">
</table>
</div>
</div>
</div>
Do not understand what should i do :S
I think Rushabh thinks you should pass your $id with your data to the model like this:
$this->Crudmodel->saveup($txtcarr,$txtmat,$txtdesc,$txtcarga, $id);
and in your model you can:
$this->db->where("id", $id);
$this->db->update('materias', $data);
and don't forget to remove your where condition in the controller;
Edit:
your get_id_row() function needs to be:
public function get_id_row($id){
$query = $this->db->get_where('your_table_name', array('id' => $id));
return $query->row();
}
Related
I´m passing a value of a radio button in a page with a form and I want to get the value of the selected option in another php file, but I can´t get that value.
First page:
<form class="itemSelection" action="../php/itemSelection1Save.php"><br><br>
<h1>Recommended Item(s)</h1>
<table class="itemTableRecom">
<thead>
<tr>
<th>Selected Item Type</th>
<th>Recommended Item Type</th>
</tr>
</thead>
<tbody>
<?php
foreach ($_SESSION['itemsInfo'] as $row) {
?>
<tr>
<td><input type="radio" name="selectedItem" id="selectedItem" required="" value="<?php echo $row[0] ?>"></td>
<td><?php
echo row[0]</td>
</tr>
<?php
}
?>
</tbody>
</table><br>
<input type="submit" value="Next"/>
</form>
Second file:
$_SESSION['selectedItem1'] = filter_input(INPUT_POST, 'selectedItem');
filter_input(INPUT_POST, 'selectedItem')
returns nothing
You form is with method get (the default method if not specified):
<form class="itemSelection" action="../php/itemSelection1Save.php">
insert method='post':
<form class="itemSelection" action="../php/itemSelection1Save.php" method='post'>
Make the following changes in HTML part , add method="post"
<form class="itemSelection" action="../php/itemSelection1Save.php" method="post">
Make the following change in PHP part (optional):
Also add filter_option in filter_input. filter the ID or name of the filter to use. Default is FILTER_DEFAULT, which results in no filtering.
filter_input(INPUT_POST, 'selectedItem',filter_option)
You Need To Add The Form Method you can try my code i simply code for you hopefully it will work!
<form class="itemSelection" method="post" action="../php/itemSelection1Save.php">
<h1>Recommended Item(s)</h1>
<table class="itemTableRecom">
<thead>
<tr>
<th>Selected Item Type</th>
<th>Recommended Item Type</th>
</tr>
</thead>
<tbody>
<?php
foreach ($_SESSION['itemsInfo'] as $row) {
?>
<tr>
<td><input type="radio" name="selectedItem" value="<?php echo $row[0]; ?>"></td>
<td><?php echo $row[0]; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
<input type="submit" name="submit" value="Next">
</form>
im trying to input data to database with PDO,i make a form for users to input,so forminput -> inputprocess.
But i cant input coz when i press submit its going to proses_input.php and show blank page.
I try to write the code in 1 file but when i press submit it show the input form again.
Here is my form_input.php
<form method="POST" action="proses_input.php">
<table>
<tr>
<td>Nama</td>
<td> : </td>
<td>
<input type="text" name="nama" required>
</td>
</tr>
<tr>
<td>No. Pegawai</td>
<td> : </td>
<td>
<input type="text" name="no_pegawai" required>
</td>
</tr>
<tr>
<td>Bagian</td>
<td> : </td>
<td>
<input type="text" name="bagian">
</td>
</tr>
<tr>
<td>Jabatan</td>
<td> : </td>
<td>
<input type="text" name="jabatan">
</td>
</tr>
<tr>
<td></td>
<td></td>
<td>
<button type="submit" value="submit">Input</button>
</td>
</tr>
</table>
</form>
and here is my proses_input.php
<?php
include_once "connect.php";
if (isset($_POST["submit"])) {
$no_pegawai = $_POST['no_pegawai'];
$nama = $_POST['nama'];
$bagian = $_POST['bagian'];
$jabatan = $_POST['jabatan'];
try { //KONDISI
$sql = "INSERT INTO phonebook (no_pegawai, nama, bagian, jabatan)
VALUES (:no_pegawai, :nama, :bagian, :jabatan)";
$input = $conn->prepare($sql);
$input->bindparam(':no_pegawai', $no_pegawai);
$input->bindparam(':nama', $nama);
$input->bindparam(':bagian', $bagian);
$input->bindparam(':jabatan', $jabatan);
$input->execute();
header('location: admin.php');
} catch (PDOException $e) {
echo $e->getMessage();
}
}
?>
IDK why but everytime i input data to my form,its always stuck on proses_input.php with blank page.i already try to put it in one php file but still not works. Pls help me,thanks
As mentioned above you haven't named your button, therefore it won't match the condition isset($_POST["submit"]) because there is no field named "submit" in your $_POST array. Also worth noting that you should not rely on users input. I saw that you are using parameterized query which is nice but don't forget to escape these values when displaying them on the UI to avoid XSS vulnerabilities.
I have a table, which is fetching data from database correctly, Now I want to add a checkbox in front of each row in the table and submit button above the table. When I check on some checkboxes, I want to delete those rows, I was trying this code to get the ids of the selected rows, but I get nothing when I press the submit, Can you please help me out?
My View file:
<?= form_open('user/set_archive'); ?>
<input type="submit" value="Submit">
<table>
<thead>
<td>Action</td>
<td>From</td>
<td>Subject</td>
<td>Date</td>
</thead>
<tbody>
<?php foreach ($emails as $email) : ?>
<tr class="read">
<td>
<input type="checkbox" name='archiveval[]' id="ch<?= $email['id']; ?>" value="<?= $email['id']; ?>" >
<label for="ch<?= $email['id']; ?>"></label>
</td>
<td>....</td>
<td>....</td>
<td>....</td>
</tr>
<?php endforeach; ?>
<?= form_close(); ?>
</tbody>
</table>
My Controller user/set_archive
public function set_archive()
{
$data1 = $this->input->post('archiveval');
echo "<pre>";
print_r($data1);
}
I believe your assignment to id is incorrect. You can get ids of the rows selected like this
<input type="checkbox" name='archiveval[]' id="archiveval[]" value="<?= $email['id']; ?>" >
Give it a shot
I am making an editing form.
There are 2 forms, one form called edit1.php and another form called edit.php. This second one should get the id generated from the edit1.php and use it to generate further queries. The following is the code for the 2 files:
edit1.php:
<table>
<tr>
<td align="center"><h4><b>EDIT DATA</b></h4></td>
</tr>
<tr>
<td>
<table border="3">
<form action="edit.php" method="get">
<?php
$vendors = "SELECT `vendor`.`v_id`,`vendor`.`v_name` FROM `stfood`.`vendor`";
$result = mysqli_query($con, $vendors);
while ($row = mysqli_fetch_array($result)){
echo ("<tr><td>'".$row["v_id"]."'</td>");
echo ("<td>'".$row["v_name"]."'</td>");
echo ("<td>Edit</td></tr>");
}
?>
</form>
</table>
edit.php:
<?php
require 'init.php';
require 'functions.php';
?>
<html>
<head>
<title>Form Edit Data</title>
</head>
<body>
<table border=1>
<tr>
<td align=center>Form Edit vendor Data</td>
</tr>
<tr>
<td>
<table>
<?php
echo $_REQUEST['v_id'];
$vendor = "SELECT `vendor`.`v_name`, `vendor`.`v_email`,`vendor`.`v_desc`,`vendor`.`cont_id` FROM `stfood`.`vendor` WHERE `vendor`.`v_id` = '".$v_id."'";
$vendor_result = mysqli_query($con, $vendor);
$row = mysqli_fetch_array($vendor_result);
?>
<form method="post" action="edit_data.php">
<input type="hidden" name="id" value="<?php echo $row["v_id"];?>">
<tr>
<td>Name</td>
<td>
<input type="text" name="name" size="20" value="<?php echo $row["v_name"]; ?>">
</td>
</tr>
<tr>
<td>Email</td>
<td>
<input type="text" name="email" size="50" value="<?php echo $row["v_email"];?>">
</td>
</tr>
<tr>
<td>Vendor Description</td>
<td>
<input type="text" name="description" size="100" value="<?php echo $row["v_desc"];?>">
</td>
</tr>
<tr>
<td align="right">
<input type="submit"
name="submit value" value="Edit">
</td>
</tr>
</form>
</table>
</td>
</tr>
</table>
</body>
</html>
When I run the code, the first form displays all the relevant data, and when I click on the edit link the page gets redirected and I can see the v_id passed in the URL, but nothing comes into the edit.php file. When I do a var_dump($row['v_id']); I get NULL.
Why is v_id not set in edit.php?
Since your hyperlink looks like this:
Edit
Your edit.php should use
echo $_GET['id'];
// or something like this
$received_id = $_GET['id'];
// do validation whether you received a good id or not and then move on
...
...
<input type="hidden" name="id" value="<?php echo $received_id;?>">
on your edit.php page you have:
Edit
now on your edit1.php you should first have a isset() and then as follow:
if(isset($_GET['id']){
$id = $_GET['id'];
//carry on blah3 ...
In the edit1.php you have not used form elements. The value $row['v_Id'] should be the value of form element input like this inside form
<input type='hidden' name='v_I'd value ='<?php echo $row['v_id'] ?>'>
Try using $_GET['id'] instead of $_REQUEST['v_id'] on edit.php
See example #1 here
Also, have you defined the $v_id before using it in the query?
Like this.
I am using session_start() function in config.php file and this file is included in all files. in my add_page.php page, i am submitting a form and saving a message(in submit action) in the session, and then re-directing to index.php page(using header('location:...')). but after re-directing, i am not getting the message from the session. the other session data exist like-username, logged_in, etc. any idea???
Code bellow(add_news.php). after submitting form, it re-directs to news.php. config.php file has session_start() function at the top and it is included in every page:
<?php
include('../include/config.php');
//submit edit data
if (isset($_POST['submit_edit'])) {
$news_head = stripslashes(trim($_POST['news_head']));
$news_details = $_POST['news_details'];
$news_special = ($_POST['news_special']=='')?'0':'1';
$news_status = ($_POST['news_status']=='')?'0':'1';
$data = array('news_head'=>$news_head,
'news_details'=>$news_details,
'news_special'=>$news_special,
'news_status'=>$news_status);
$clause = "news_id = '".$_POST['news_id']."'";
$response = update_data('news', $data, $clause);
if($response)
$_SESSION['success_msg'] = "News updated successfully.";
}
//submit edit data end
?>
<form action="add_news.php" method="post" id="add_news_form">
<input type="hidden" name="news_id" value="<?php echo $news_id ?>" />
<table width="100%" border="0" cellspacing="4" cellpadding="4">
<tr>
<td>News Heading</td>
<td><input type="text" name="news_head" id="news_head" class="required" value="<?php echo $news_head ?>" size="100" /></td>
</tr>
<tr>
<td>News Details</td>
<td><textarea name="news_details" id="news_details" class="editor required" rows="7" cols="60"><?php echo trim(stripslashes($news_details)); ?></textarea></td>
</tr>
<tr>
<td>Special News</td>
<td><input type="checkbox" name="news_special" id="news_special" value="1" <?php if($news_special=='1') echo 'checked="checked"'?> />
[ Checked means special ]</td>
</tr>
<tr>
<td>Status Status</td>
<td><input type="checkbox" name="news_status" id="news_status" value="1" <?php if($news_status=='1') echo 'checked="checked"'?> />
[ Checked means published ]</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="Submit" <?php if($_REQUEST['news_id']!="" && $_REQUEST['action']=='edit') echo 'name="submit_edit"'; else echo 'name="submit"'; ?> /></td>
</tr>
</table>
</form>
Make sure the call to session_start() occurs before any calls to save a message in the session. session_start() has to be called on every page request that needs the session, and has to occur at the very beginning of the page, even before the submit action occurs.
Edit
See http://bugs.php.net/bug.php?id=14636 for more information and some suggestions to try.