i have one html table with all the rows value from a specific database table.
I created a html table column to eliminate a specific row from the database, inside this column exists a checkbox to check and eliminate multiple table rows from the database.
But i'm having some problems, i can only delete the last row, example: Lets imagine that you check the second row, and you click at submit button and nothing happens.
But if you check the last inserted row, and you click submit, it deletes the row from the database.
I just can delete the last row, i can't delete multiple rows by checking a specific number of checkboxes.
I did this:
I'm using the MVC structure:
View:
<form method='POST'>
<?php
echo "<td><input style='width: 50px;' class='delete' value='delete' type='submit' name='del[]' /></td>";
foreach($editjoarray as $key){
echo "
<table>
<tr>
<td> ".$key['id']."</td>
<td><input type='checkbox' name='delete[]' value=".$key['id']." /></td>
</tr>
</table>
</form>";
}
Model:
function fname(){
if(isset($_POST['del']) && !empty($_POST['delete'])) {
{
$checkbox = $_POST['delete'];
$countCheck = count($_POST['delete']);
for($i=0;$i<$countCheck;$i++) {
$del_id = $checkbox[$i];
echo $del_id;
$sql = "DELETE FROM mytable WHERE id=$del_id";
$exe = mysql_query($sql) or die(mysql_error());
}
}
}
printFunction();
}
You are closing the form tag inside the loop so there will always be only one delete checkbox (as post values). You need to move the closing tag of the form </form> outside the loop so that all the checkboxes lies in the same form.
<form method='POST'>
<?php foreach($editjoarray as $key){
/*
delete checkboxes
input fields
*/
} ?>
</form>
Related
The following code is in tag_show.php.I have to show all the data from the database and I am trying to edit the database from php code so i form the edit button at the end of all row in html table.
echo"<br><br><br><br>
<table border='6' style= 'background-color: #FFFFE0; color: #761a9b; margin: 2 auto;'>
<thead>
<tr>
<th>tag_title</th>
<th>description</th>
<th>show_in_welcome</th>
<th>status</th>
</tr>
</thread>
<tbody>";
while($row = mysqli_fetch_assoc($get_detail))
{
$_SESSION['tag_title'] =$row['tag_title'];
$_SESSION['description'] =$row['description'];
$_SESSION['show_in_welcome'] =$row['show_in_welcome'];
$_SESSION['status']=$row['status'];
echo
"<tr>
<td>{$_SESSION['tag_title']}</td>
<td>{$_SESSION['description']}</td>
<td>{$_SESSION['show_in_welcome']}</td>
<td>{$row['status']}</td>
<td><form action='edit.php' method='POST'><input type='hidden' name='tag_title' value='".$row["tag_title"]."'/><input type='submit' name='submit-btn' value='edit' /></form></td>
</tr>\n";
}
and the edit.php is
<?php
session_start();
echo"<form action='tag_show.php' method='post'>
<br><br><br>
Tag Title<br><input name='tag_title' type='text' value='{$_SESSION['tag_title']}'><br><br>
Description <br><input name='description' type='text' value='{$_SESSION['description']}'><br><br>
Show in welcome<br><input name='show_in_welcome' type='text' value='{$_SESSION['show_in_welcome']}'><br><br>
Status<br><input name='status' type='text' value='{$_SESSION['status']}'> <br><br>
<input name='submit1' type='submit' value='Update'>
</form>";
?>
but the $_SESSION returns the last row value in HTML table. But i want the receptive row value when i click on edit button.
You don't need a session variable to pass the value to edit.php file.
Hope there is a unique key to identify the record from database. I assume it as recordid. Pass this value to edit.php as a query string
tag_show.php
$output = '';
while($row = mysqli_fetch_assoc($get_detail)){
$output '<tr>
<td>'.$row['tag_title'].'</td>
<td>'.$row['description'].'</td>
<td>'.$row['show_in_welcome'].'</td>
<td>'.$row['status'].'</td>
<td>edit</td>
</tr>';
}
In edit.php, you can get the recordid from query string and retrieve the data from database to populate the form
<?php
if($_GET['editid']!=''){
//get record corresponding to the 'editid' from database
//I assume the record is for and store in a variable $editdata
?>
<form action="tag_show.php" method="post">
<br><br><br>
Tag Title<br><input name="tag_title" type="text" value="<?=$editdata['tag_title']?>"><br><br>
Description <br><input name="description" type="text" value="<?=$editdata['description']?>"><br><br>
Show in welcome<br><input name="show_in_welcome" type="text" value="<?=$editdata['show_in_welcome']?>"><br><br>
Status<br><input name="status" type="text" value="<?=$editdata['status']?>"> <br><br>
<input name="submit1" type="submit" value="Update">
</form>
<?php
}
?>
Looks like you are never updating the session data with the information received from the form. You will need something like this somewhere in your code.
$_SESSION['tag_title'] = $_POST['tag_title'];
$_SESSION['description'] = $_POST['description'];
$_SESSION['show_in_welcome'] = $_POST['show_in_welcome'];
$_SESSION['status']= $_POST['status'];
A few things to consider.
Make sure your db info does not overwrite the values after user submits form.
Always sanitize/validate user input before using it (unlike the above code, since it's just an example).
here session make no sense. each time loop runs previous value replaced therefore you are getting last value. Instead of you can use the concept of query strings. Instead of Printing Form print a anchor tag
<a href='file.php?id='.$row["tag_title"]>Edit</a>
Being your tag_title unique.
if(isset($_GET['id))
{
$id = $_['id']
// Your code based on id
}
Now using id run mysql query and select desired data and display in form
Certainly your code will return the last row as the session array overwrites the previous row's value after every iteration.
Make the session array as multi dimensional like $_SESSION[$id]["tag_title"] and then in your edit.php refer to the session array using the id value.
$_SESSION[$id] = array('tag_title' => $row['tag_title'], 'description' => $row['description'], 'show_in_welcome' => $row['show_in_welcome'], 'status' => $row['status'])
Then increment the $id after each iteration.
I have randomly generated questions and decided to put those in a temporary table. Now, it is already in my html table:
echo "<form action=# method=post>";
echo "<table>";
do{
echo "<tr>";
echo "<td>"."<input type=text value='$rows[question]' name=question>"."</td>;
echo "<td>"."<input type=text value='$rows[correct]' name=correct>"."</td>;
}while($rows=mysqli_fetch_array($rs));
echo "</table>";
echo "<input type=submit name=insert>";
echo "</form>";
?>
</body>
</html>
What I want to happen is, when I hit the insert button name="insert", data from that table, [from row 1 till the last row] will be inserted to my database "tbl_randomq". Is it possible to insert multiple row data simultaneously to database with just 1 click.
I've tried to use while loop, but it only inserts repeated(10times) data coming from the last row. Help with this please :-)
do{
?>
<tr>
<td><input type=text value='<?=$row["question"]?>' name='question[]'></td>
<td><input type=text value='<?=$row["correct"]?>' name='correct[]'></td>
</tr>
<?php
}while($row = mysqli_fetch_array($rs));
Then to save this :
for ($i=0; $i<count($_POST['question']); $i++){
$question = addslashes($_POST['question'][$i]);
$correct = addslashes($_POST['correct'][$i]);
mysqli_query("
insert into tbl_ramdomq (question, correct)
values ('$question','$correct')
");
}
Replace code formatting with your own as you wish. Though for myself I prefer to use short php tags inside html rather than echo because of syntax coloring and better clarity.
I'm relatively new to php and mysql. What I'm trying to do in this section of code is update 2 columns of information in a database based on the inputs of 2 text boxes. Whenever I try and update the values in the database they update to 0. I placed echo statements after I declared the $ variables and the values of both variables was the same as what I had typed into the boxes. But when I run the sql_query the new values in the database are 0 rather than the values of the 2 $ variables. Any help would be much appreciated! Thanks in advance!
<?php
$result = mysql_query("SELECT * from place_order ORDER BY item_name;");
echo "<form action='' method='POST'>Select an item:<select name='selection'><option>Select...</option>";
while ($row = mysql_fetch_assoc($result))
{
$item_name = $row["item_name"];
echo "<option>$item_name</option>";
}
echo "</select>
<input type='submit' value='Select Item' style='float:right;'/>";
$selection = $_POST['selection'];
echo "<br><br>Type the updated information into the text fields<br><br>";
echo "
<table width='300'>
<tr>
<td align='left'>Item Cost(€): </td>
<td align='left'><input type='text' name='cost'></td>
</tr>
<tr>
<td align='left'>Item Quantity: </td>
<td align='left'><input type='text' name='quantity'></td>
</tr>
<tr>
<td align='left'></td>
<td align='left'><input type='submit' name='button' value='Submit'></td>
</tr>
</table>
";
$cost = $_POST['cost'];
$quantity = $_POST['quantity'];
$selection = $_POST['selection'];
$sql = "UPDATE place_order SET item_cost='$cost', quantity='$quantity' WHERE item_name='$selection' ORDER BY item_name;;";
$query_update = mysql_query($sql);
if($query_update)
{
echo "Table updated! Click View Stock in the menu to view the updated table";
}
echo"</form>";
?>
There are two forms in this script:
one with the select box
another with the input boxes
each of these forms post individually.
Let's have some kind of state transition table:
forms are showing first time initially, all values are either empty
or default.
you select an item name and submit the first form.
option list is posted and $_POST['select'] has a value which then
is properly populated in the second form
the script runs til the end and immediately inserts an empty row to
your database with the selected item name
you make your inputs in the second form and submit it
this has two input variables: cost and quantity which get posted
due to the fact that the option list is in the other form, it will
not be posted and $select is empty at that state
the update statement will run but with no result since there is no
empty item_name in the db - useless update.
simple solution: make just one form out of that and you're done (except error handling which is missing entirely ...)
second solution: if only $_POST['select'] is set, put the value of $select in a hidden field and exit the script after echo-ing the second form.
In my MySQL database, the user can update values in the database via a result table by clicking on an UPDATE button next to the field that the user wants to update the value of (see below).
car colour update field
1 Mercedes blue UPDATE BUTTON
2 VW grey UPDATE BUTTON
3 Opel red UPDATE BUTTON
4 Alfa Romeo white UPDATE BUTTON
5 Renault pink UPDATE BUTTON
The table is produced by a "foreach" loop. Each time a new row is written in the table the UPDATE BUTTON is part of a FORM ACTION statement with a variable (i.e. $New_Update_File_Name) for the file name after ACTION. That variable is filled with a unique filename in every run of the loop. In my verification echo statement $New_Update_File_Name appears exactly as I want it in every run of the loop as shown by the output of my echo statements:
cars[Mercedes] = blue
New_Update_File_Name = Update_blue-SESSION.php
cars[VW] = grey
New_Update_File_Name = Update_grey-SESSION.php
cars[Opel] = red
New_Update_File_Name = Update_red-SESSION.php
cars[Alfa Romeo] = white
New_Update_File_Name = Update_white-SESSION.php
cars[Renault] = pink
New_Update_File_Name = Update_pink-SESSION.php
However, if I click on any of the UPLOAD BUTTONs they all have the same value as the first UPLOAD BUTTON: Update_blue-SESSION.php, rather than what I want, i.e. each UPLOAD BUTTON pointing to the correct New_Update_File_Name for that row. I cannot find out why this is not happening. Any help greatly appreciated. My code appears below.
<?php
// BUILD THE CAR TABLE WITH UPDATE BUTTON
$cars = array("Mercedes"=>"blue","VW"=>"grey","Opel"=>"red","Alfa Romeo"=>"white","Renault"=>"pink");
$counter = 1;
echo "<table>
<tr>
<th></th>
<th>car</th>
<th>colour</th>
<th>update field</th>";
foreach ($cars as $field => $value)
{
$Update_File_Name = 'Update_-SESSION.php';
$New_Update_File_Name = substr_replace($Update_File_Name,$cars[$field],-12, 0);
echo "cars[$field] = ".$cars[$field]."<br />";
echo "New_Update_File_Name = ".$New_Update_File_Name."<br />";
echo "<tr class='alt'>
<td>$counter</td>
<td>$field</td>
<td>$value</td>
<td><form action='$New_Update_File_Name' method='post'>
<input type='submit' value='update'></td>
</tr>";
$counter++;
}
echo "</table>";
?>
For starters, calling a different file for each action is a really bad idea. What do you do when you want to update code that is the same in each?
You should instead, have them all send to the same one, and send one extra variable ( such as color ) to determine what you want to do.
Form action = "...update.php".
By the way, the reason you're having problems is because you have a lot of invalid HTML.
You are not closing your form fields, and therefore the first call is set to them all.
<td>
<form action='$New_Update_File_Name' method='post'>
<input type="hidden" value="<?php echo $value; ?>" name="color" />
<input type='submit' name="submit" value='update'>
</form>
</td>
Note: I added a hidden input. In your update.php file, you can do
if($_POST['color'] == "blue") {
.... do stuff
}
Because you have opened a form in loop with new action but havn't closed the form inside the loop therefore only first form action will be treated create a single form for each new action try this
foreach ($cars as $field => $value)
{
$Update_File_Name = 'Update_-SESSION.php';
$New_Update_File_Name = substr_replace($Update_File_Name,$cars[$field],-12, 0);
echo "cars[$field] = ".$cars[$field]."<br />";
echo "New_Update_File_Name = ".$New_Update_File_Name."<br />";
echo "<tr class='alt'>
<td>$counter</td>
<td>$field</td>
<td>$value</td>
<td><form action='$New_Update_File_Name' method='post'>
<input type='submit' value='update'></form></td>
</tr>";
$counter++;
}
I have a page which displays my database information. Through each loop, there is a checkbox printed that is to delete the associated entry if checked and submitted. The checkbox names are 'delete[]' and there is the hidden value which contains the row id is named 'id[]'.
This is the relevant part of my form:
<tr><td valign='top'>
<label class='amarillo med' style='color:#C00;'>Delete Section </label>
<input type='checkbox' name='delete[]' />
<input type='hidden' name='id[]' value='" . $row['about_id'] . "' />
</td></tr>
This is my php and query
$deleteCount = count($_POST['delete']);
for ($x = 0; $x < $deleteCount; $x++) {
if(isset($_POST['delete'][$x])) {
$sql = 'DELETE FROM about WHERE about_id = \''.$_POST['id'][$x].'\'';
$result = mysql_query($sql);
}
}
This is what happens.
If three rows are returned and I want to delete only the third, I check the third box and click submit. This deletes the first row that was returned. Again, if three rows are returned and I want to delete the first and third, I submit and the first two rows are deleted.
What it looks like is happening is for every checkbox that is checked, that many rows are deleted starting with the first. Any advice would be greatly appreciated.
The best and much easier way is to use
<input type="checkbox" name="delete[]" value="<?=$row['about_id']?>"/>
and then
foreach($_REQUEST['delete'] as $delID)
{
...
}
Use the following code
<tr><td valign='top'>
<label class='amarillo med' style='color:#C00;'>Delete Section </label>
<input type='checkbox' name='delete[$row['id']]' />
<input type='hidden' name='id[$row['id']]' value='" . $row['about_id'] . "' />
</td></tr>
where $row['id'] is the id of the row