i want to update data using radio button for each row of my data.
Example:
My table
id | name | age | number | account | note | place | radio
after i click the radio button (Mencurigakan/Tidak Mencurigakan), the radio will be updated. I tried using this code and i cannot read my id column and i only can update one row
Controller:
public function updateradio($id) {
$data=$this->welcome_model;
$data->radio=$this->input->post("radio$id");
$data->update_bb($id);
}
Model:
public function update_bb($id) {
$id = /*i need something here*/
$data = array('radio' => $this->input->post("radio$id"));
$this->db->where('id', $id);
$this->db->update('info', $data);
}
View:
<?php
foreach ($results as $row) {
echo '<tr>';
echo '<td>' . $row->id . '</td>';
echo '<td>' . $row->name . '</td>';
echo '<td>' . $row->age . '</td>';
echo '<td>' . $row->number . '</td>';
echo '<td>' . "Rp " . $row->account . '</td>';
echo '<td>' . $row->note . '</td>';
echo '<td>' . $row->place . '</td>';
?>
<form action="<?php echo base_url(); ?>welcome/updateradio" method="post">
<div class="radio">
<label>
<input type="radio" name="radio<?php echo $row->id ?>" value="3"/>
Mencurigakan
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="radio<?php echo $row->id ?>" value="2"/>
Tidak Mencurigakan
</label>
</div>
</td>
<?php
echo '</tr>';
}
?>
<button type="submit" class="btn btn-group">Update</button>
</form>
Any help would be appreciated!
Related
I am trying to make a table where data from a database is shown and by pressing on the update button the (not the 'lidnummer one')cells on the row should change to a form input field with the data from that row as the initial input values.
I've copied the idea from something I found and changed it to fit all the data I want to show, but I can't get the row to change upon pressing the 'update' button.
echo '<tr>';
echo '<td>Lidnummer</td>';
echo '<td>Voornaam</td>';
echo '<td>Achternaam</td>';
echo '<td>Huisnummer</td>';
echo '<td>Adres</td>';
echo '<td>E-mailadres(sen)</td>';
echo '<td>Telefoonnummer(s)</td>';
echo '<td>Update</td>';
echo '<td>Delete</td>';
echo '<tr>';
for ($j = 0 ; $j < $num_members ; ++$j)
{
$row = $select_result->fetch_array(MYSQLI_ASSOC);
echo '<tr>';
if(isset($_GET['lidnummer']) && $row['lidnummer'] == $_GET['lidnummer'])
{
echo '<form action="includes/update_ledenlijst.php" method="POST">';
echo '<td>' . $row["lidnummer"] . '</td>';
echo '<td><input type="text" name="voornaam" value="' . $row['voornaam'] . '"></td>';
echo '<td><input type="text" name="naam" value="' . $row['naam'] . '"></td>';
echo '<td><input type="text" name="huisnummer" value="' . $row['huisnummer'] . '"></td>';
echo '<td><select id="adres" name="adres">';
include 'includes/select_postcodes.php';
echo '</select>';
echo "<td>";
echo '<textarea>';
toon_contactgegevens("telefoonnummers", $row, $conn, "telefoonnummer", "form_data");
echo '</textarea>';
echo '</td>';
echo '<td>';
echo '<textarea>';
toon_contactgegevens('emails', $row, $conn, 'email', 'form_data');
echo '</textarea>';
echo '</td>';
echo '<td><button type="submit" class="buttons">Save</button></td>';
echo '<td>----</td>';
echo '</form>';
} else {
echo '<td>' . htmlspecialchars($row["lidnummer"]) . "</td>";
echo '<td>' . htmlspecialchars($row["voornaam"]) . "</td>";
echo '<td>' . htmlspecialchars($row["naam"]) . "</td>";
echo '<td>' . htmlspecialchars($row["huisnummer"]) . "</td>";
echo '<td>' . htmlspecialchars($row["adres"]) . ", " . htmlspecialchars($row["postcode"]) . " " . htmlspecialchars($row["woonplaats"]) . '</td>';
echo '<td>';
toon_contactgegevens('emails', $row, $conn, 'email', "table_data");
echo '</td>';
echo '<td>';
toon_contactgegevens("telefoonnummers", $row, $conn, "telefoonnummer", "table_data");
echo '</td>';
echo '<td><a class="buttons" href="home_ledenlijst.php?id='. $row["lidnummer"] . '" role="button">Update</a></td>';
}
echo "<td><a class='buttons' href='includes/delete_ledenlijst.php?id='" . $row['lidnummer'] . "' role='button'>Delete</a></td>";
echo "</tr>";
}
This will probably be something I have overlooked and is really easy.. But I am new to this so I am still learning :)
You're passing a value called id to the server:
href="home_ledenlijst.php?id='. $row["lidnummer"] . '"
But looking for a value called lidnummer:
$_GET['lidnummer']
How you identify the values needs to be the same. Try looking for the id value:
$_GET['id']
You are checking for $_GET['lidnummer'] but you are setting id='. $row["lidnummer"]
Change
id='. $row["lidnummer"]
to
lidnummer='. $row["lidnummer"]
Cheers! :)
=C=
How do I set names for each radio button in the following PHP code using counter variable, or any other alternative?
<?php
$id=0; //counter variable
$sql ="select roll_number,student_name,gender from student_table";
$query=mysqli_query($dbcon, $sql) or die(mysqli_error($dbcon));
while($post=mysqli_fetch_assoc($query)){
echo '<tr>';
echo '<td>'. $post['roll_number'] . '</td>';
echo '<td>'. $post['student_name'] . '</td>';
echo '<td>'. $post['gender'] . '</td>';
echo '<td width=250>';
echo '<input type="radio" name="rad'.$id'" value="P" />Present';
echo '<input type="radio" name="rad'.$id'" value="A" />Absent';
echo '<button type="submit" name="save" value="Save" class="btn btn-success btn-sm">Save</button>';
++$id;
echo '</td>';
echo '</tr>';
}
?>
echo '<input type="radio" name="rad[' . $post['roll_number'] . ']" value="P" />Present';
echo '<input type="radio" name="rad[' . $post['roll_number'] . ']" value="A" />Absent';
On the receiving end you may do like this:
$answers = $_POST['rad'];
foreach($answers as $roll_number=>$answer){
//do something with the $roll_number and $answer...
}
Updated the code, changed the $post to $row, concatenated the strings, fixed the <button> and so on. There is no form-declaration anywhere - if you have only one form, this will save / fetch every item in the form - depending on your needs, you might wanna declare a form per student, with a unique identifier.
<?php
$query = mysqli_query($dbcon, 'SELECT roll_number,student_name,gender FROM student_table') or die(mysqli_error($dbcon));
while ($row = mysqli_fetch_assoc($query)) {
echo '<tr>
<td>'.$row['roll_number'].'</td>
<td>'.$row['student_name'].'</td>
<td>'.$row['gender'].'</td>
<td width="250">
<label><input type="radio" name="rad['.$row['roll_number'].']" value="P">Present</label>
<label><input type="radio" name="rad['.$row['roll_number'].']" value="A">Absent</label>
<input type="submit" name="save" value="Save" class="btn btn-success btn-sm">
</td>
</tr>';
}
?>
I created a table, inside a foreach loop displaying in the table all the records. Then I included a SQL update with the select and update one array value. When I select different value, the loop updates all the records in the table and NOT only one selected to be updated. I was all day struggling with it, please help me.
If foreach is a wrong way, I would highly appreciate your suggestions of how to display a table that would display records and permit updates for each array.
<?php
foreach ($stmt as $key => $row)
{
$newcustomerid = htmlentities($row['customer_id']);
echo '<tr><td>' . htmlentities($row['customer_company']) . '</td>';
echo '<td> id:' . $newcustomerid . 'key: ' . $key .'</td>';
echo '<td>' . htmlentities($row['customer_email']) . '</td>';
echo '<td>' . htmlentities($row['customer_phone']) . '</td>';
echo '<td>' . htmlentities($row['customer_country']) . '</td>';
echo '<td>' . htmlentities($row['customer_city']) . '</td>';
echo '<td>' . htmlentities($row['customer_segment']) . '</td>';
echo '<td>' . htmlentities($row['customer_updated']) . '</td>';
$customer_status = htmlentities($row['customer_status']);
if(isset($_POST['Submitbb'])){ //check if form was submitted
$input = $_POST['Submitbb']; //get input text
$stmtUpdateStatus = $conn->prepare("UPDATE user_customers SET `customer_status` = :customer_status WHERE `customer_id` = :customer_id");
$stmtUpdateStatus->execute(array(':customer_status' => $input, ':customer_id' => $row['customer_id']));
}
echo '<td>
<form action="" method="post">
<select name="Submitbb" onchange="this.form.submit();">
<option> - ' . $customer_status . ' - </option>
<option>Susisiekti</option>
<option>Priminimas 1</option>
<option>Priminimas 2</option>
<option>Paskambinti</option>
<option>Netinkamas klientas</option>
</select>
</form>
</td>';
echo '<td>' . 'Išsaugoti' . '</td></tr>';
}
?>
Thank you.
You could use a hidden input to capture the row in the database you wish to update
echo '<td>
<form action="" method="post">
<input name="customer_id" type="hidden" value="'.$newcustomerid.'"/> <!-- hidden -->
<select name="Submitbb" onchange="this.form.submit();">
<option> - ' . $customer_status . ' - </option>
<option>Susisiekti</option>
<option>Priminimas 1</option>
<option>Priminimas 2</option>
<option>Paskambinti</option>
<option>Netinkamas klientas</option>
</select>
</form>
</td>';
Then compare it when the form is posted
if(isset($_POST['Submitbb'])){ //check if form was submitted
$input = $_POST['Submitbb']; //get input text
$customer_id = $_POST['customer_id']; // get the posted customer ID
if($row['customer_id'] == $customer_id){
$stmtUpdateStatus = $conn->prepare("UPDATE user_customers SET `customer_status` = :customer_status WHERE `customer_id` = :customer_id");
$stmtUpdateStatus->execute(array(':customer_status' => $input, ':customer_id' => $customer_id));
}
}
This is my code to display code out of my database:
<?php
while($row = mysqli_fetch_array($myData)){
echo '<form action="details.php?ID='.$row['ID'].'" method="post">';
echo '<tr>';
echo '<input type="hidden" name="ID" value="'.$row['ID'].'">';
echo '<td>' . $row['ID'] . '</td>';
echo '<td>' . '<input name="title" value="' . $row['Title'] . '">' .'</td>' ;
echo '<td>' . '<input size=85 name="detail" value="' . $row['Detail'] . '" cols="85" rows="2">'.'</td>';
echo '<td>' . $row['eventDate'] . '</td>';
echo '<td>' . $row['dateAdded'] . '</td>';
echo '<td>' . '<input type="submit" name="update" value="update" class="btn btn-default"> ' . '</td>';
echo '<td>' . '<input type="submit" name="delete" value="delete" class="btn btn-default"> ' . '</td>';
echo '</tr>';
echo '</form>';
}
?>
When I try to change the Inputs to <textarea>'s it stops showing the data from the database but only the text field. When I check Page Source it shows the data. How to format the fields like this:
echo '<td>' . '<textarea name="title" value="' . $row['Title'] . '">'. '</textarea>' .'</td>' ;
echo '<td>' . '<textarea size=85 name="detail" value="' . $row['Detail'] . '" cols="85" rows="2">'. '</textarea>' .'</td>';
textareas don't have a value attribute. Place the value between the opening and closing tags instead:
echo '<td><textarea name="title">'. $row['Title'] . '</textarea></td>' ;
echo '<td><textarea size=85 name="detail" cols="85" rows="2">'. $row['Detail'] .'</textarea></td>';
Try to insert the text between textarea tags instead of adding value attribute
echo '<td>' . '<textarea name="title">'. $row['Title'] .'</textarea>' .'</td>' ;
The general format for a text are field is <textarea name="name" cols="width" rows="height" wrap="type"> </textarea>
So there shouldn't be a value attribute. Something like this:
echo '<td>' . '<textarea name="title">'. '</textarea>' .'</td>' ;
How do i pass id to delete record in this code?
<form action="index.php">
<?php
mysql_connect('localhost', 'root', '');
mysql_select_db('user');
$query = mysql_query("Select * from tbluser");
echo "<center>";
echo '<table style="border:solid 2px black;">';
while(($row = mysql_fetch_array($query)) != NULL) {
echo '<tr>';
echo '<td>' . $row['UserName'] . '</td>';
echo '<td>' . $row['Password'] . '</td>';
echo '<td>' . $row['EmailAddress'] . '</td>';
echo '<td>' . $row['Address'] . '</td>';
echo '<td>' . $row['Address'] . '</td>';
echo '<td><input type = "Submit" value = "Delete" name = "btnDel" /></td>';
echo '</tr>';
}
echo '</table>';
echo "</center>";
?>
</form>
The above code is in index.php and it is submitting it to itself.
Without needing javascript, seperate GET urls etc, just plain old HTML & the original POST: just add the ID to the name of the button:
<input type="submit" value="Delete" name="btnDel[<?php echo $id;?>]">
And in receiving code:
if(isset($_POST['btnDel']) && is_array($_POST['btnDel'])){
foreach($_POST['btnDel'] as $id_to_delete => $useless_value){
//delete item with $id_to_delete
}
}
Here's how I would do it:
Change
echo '<td><input type = "Submit" value = "Delete" name = "btnDel" /></td>';
To
echo '<td><input type = "button" value = "Delete" onclick = "btnDel(' . $row['id'] . ')" /></td>';
Add the following field to the form (outside of the while loop of course):
<input type="hidden" name="id" id="userid" />
Define the following javascript function:
function btnDel(id) {
if (confirm("Really delete id=" + id + "?")) {
document.getElementById('userid').value = id;
document.forms[0].submit();
}
}
Then you can retrieve that value using $_GET['id'] or $_POST['id'] depending on your form's method.
EDIT: Here's a working demo, and its source
Use this to submit the id as a part of form.
<input type="hidden" id="id" name="id" value="<? echo $row['id']; ?>" />
or you can send values in URL to do the same thing
An example:
Delete
A full working sample
<?
mysql_connect('localhost', 'root', '');
mysql_select_db('user');
switch($_GET['action']) {
case "delete":
$query = "DELETE FROM tbluser WHERE id='".$_GET['id']."'"
$result = mysql_query($query);
break;
//well other actions
}
$query = mysql_query("Select * from tbluser");
echo "<center>";
echo '<table style="border:solid 2px black;">';
while(($row = mysql_fetch_array($query)) != NULL) {
echo '<tr>';
echo '<td>' . $row['UserName'] . '</td>';
echo '<td>' . $row['Password'] . '</td>';
echo '<td>' . $row['EmailAddress'] . '</td>';
echo '<td>' . $row['Address'] . '</td>';
echo '<td>' . $row['Address'] . '</td>';
echo '<td>Delete</td>';
echo '</tr>';
}
echo '</table>';
echo "</center>";
?>
You could also send it in the url at action="index.php" given that you move it below while(($row = mysql_fetch_array($query)) != NULL) { like this
echo "<form action='index.php?var=" . $row['id'] . "'>";
You would then get the variable using $_GET['var']
If the id needs to stay hidden (on the page and in the link) a hidden input element and submitting the form using method="post" like previously suggested would be the better way to go.
From the table:
echo '<input type="hidden" name="user_id" value="'.$row['id'].'" />';
echo '<td><input type = "Submit" value = "Delete" name = "btnDel" /></td>';
This will be sent to the server as a parameter named user_id. Ideally, you should be using the POST method in the form.
This assumes that the user id is named, well id in the table.
BTW, the != NULL is unnecessary.
It's sufficient to write:
while($row = mysql_fetch_array($query)) {
NULL evaluates to FALSE in a boolean context.
Update:
As mentioned in comments to another answer, there is an issue with your approach. With multiple rows, you won't be able to distinguish between user ids.
One solution would be use multiple forms.
Another option would be to have the name of the submit button include the id. You'd then parse this out of the name of the $_POST array keys.