Delete button for each table row - php

I manage to succesfully read and display data from my database with the following code:
http://pastebin.com/rjZfBWZX
I also generate a delete button for each row of the table :) Clicking the delete button calls "obrisi.php" which is supposed to delete that row but I messed something up :S Here's obrisi.php code:
http://pastebin.com/mrFy1i7S
I'm getting a Parse error: syntax error, unexpected 'FROM' (T_STRING) :S

Let's try and do it with _GET instead of _POST.
In your main code you need to change line 39 (the input) from:
echo "<td>" . " <input type='submit' id= '$id' . ' value='Delete' >" . "</td>";
to:
echo "<td><a href='obrisi.php?id=$id'>Delete</a></td>";
In your obrisi.php change line 3 (the id setup) from:
$id = $_POST['id'];
to:
$id = $_GET['id'];
And finally as a nice addition, redirect back to the main page by adding the following line at the end of the obrisi.php file before the closing of the php tag.
header('location:index.php');
Where index.php the name of the main page you have.

echo "<td>" . " <input type='submit' id= '$id' . ' value='Delete' >" . "</td>";
some simple errors here. this would output (with $id = 1):
<td><input type='submit' id= '1' . ' value='Delete' ></td>
this line should be corrected to
echo '<td><input type="submit" id="' . $id . '" value="Delete" ></td>';
this is also going wrong.
echo "<form action="obrisi.php" method="post">";
should be like:
echo '<form action="obrisi.php" method="post">';
But the main problem is that there is no field id given in the post. The id of a html element is not sent on submit. it is basically to identify that element in the HTML structure.
And when using a submit button you will have to limit the scope of the form to that row and use a hidden input field, or use a link like thanpa suggests
to clarify: if you want to do it with a post (but i would sugget using the $_GET)
while ($row = mysqli_fetch_array($result) )
{
$id = $row['id'];
echo "<tr>";
echo '<form action="obrisi.php" method="post">';
echo "<td>" . $row['Ime'] . "</td>";
echo "<td>" . $row['Prezime'] . "</td>";
echo "<td>" . $row['Grad'] . "</td>";
echo "<td>" . $row['Drzava'] . "</td>";
echo "<td>" . $row['Obavijesti'] . "</td>";
echo "<td>" . $row['Tekst'] . "</td>";
echo "<td>"
echo '<td><input type="hidden" name="id" value="' . $id . '"/><input type="submit" value="Delete" ></td>';
echo "</form>
echo "</tr>";
}
and remove the echo's for the form from start and end of script.

as an additional note here, if this is going to be at some point being used in a live system you need to be checking $id in obrisi.php that it is actually an ID and not something nasty and unexpected like more sql, look up sql injection.

Related

How do I send all the rows in the table?

You are going to send the full line value using form tag. However, if you send a value, only the value in the last row is sent. How should it be modified?
in this table code
echo "<form action='../verification/medical_bills_check.php' method='post'>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td> <input type='hidden' name='id' value ='" . $row['id'] . " '>" . $row['id'] . "</td>";
echo "<td> <input type='hidden' name='storename' value ='" . $row['storename'] . " '>" . $row['storename'] . "</td>";
echo "<td> <input type='hidden' name='corporate_num' value ='" . $row['corporate_num'] . " '>" . $row['corporate_num'] . "</td>";
echo "<td> <input type='hidden' name='store_mbrno' value ='" . $row['store_mbrno'] . " '>" . $row['store_mbrno'] . "</td>";
echo "<td> <input type='hidden' name='store_mbrno_van' value ='" . $row['store_mbrno_van'] . " '>" . $row['store_mbrno_van'] . "</td>";
echo "<td ><input type='hidden' name='past_cash_receipts' value ='" . $row['past_cash_receipts'] . " '>" . $row['past_cash_receipts'] . "</td>";
echo "<td> <input type='hidden' name='past_card' value ='" . $row['past_card'] . " '>" . $row['past_card'] . "</td>";
echo "<td> <input type='hidden' name='pg_select' value ='" . $row['pg_select'] . " '>" . $row['pg_select'] . "</td>";
echo "<td> <input type='hidden' name='program_select' value ='" . $row['program_select'] . " '>" . $row['program_select'] . "</td>";
echo "<td> <input type='hidden' name='authority' value ='" . $row['authority'] . " '>" . $row['authority'] . "</td>";
echo "<td> <input type='hidden' name='apikey' value ='" . $row['apikey'] . " '>" . $row['apikey'] . "</td>";
echo "<td > <input type='submit'> </td>";
echo "</tr>";
}
echo "</form>";
and receive page code
$_SESSION["corporate_num"] = $_POST["corporate_num"];
$_SESSION["store_mbrno"] = $_POST["store_mbrno"];
$_SESSION["store_mbrno_van"] = $_POST["store_mbrno_van"];
$_SESSION["program_select"] = $_POST["program_select"];
$_SESSION["authority"] = $_POST["authority"];
$_SESSION["apikey"] =$_POST["apikey"];
I want result
id | corporate_num | store_mbrno | submit
1 1 1 submit_button
2 2 2 submit_button
3 3 3 submit_button
4 4 4 submit_button
When I submit id 1 rows.
Received Page receive value id:1 / corporate_num:1/ store_mbrno:1
When I submit id 2 rows.
Received Page receive value id:2 / corporate_num:2/ store_mbrno:2
If I understood your question correctly, the issue is that you have a lot of input fields with the same name within one form. What happens when you click one of the buttons, is that all the data in the whole form gets sent. And, because every row has fields with the same name, the values get overwritten by the next row. Therefore only the data of the last row is sent.
If you only want to send the data of one row, you need to have the data in a separate form. So, one form for each row. Since, technically, you can't have a form element directly inside a table or tr element, I suggest putting the form element and all hidden inputs in the last table cell.
Furthermore, since the contents of the hidden inputs can be modified (you can't trust your visitors not to use the inspector), you should not trust the information that is sent. Instead, if possible, only send the id, and fetch the other information anew from the database.

How to delete a specific row MySQL

I am trying to get my PHP script to delete a specific row from a table. Every row has an unique ID. However, my code deletes always the first row, while for instance I want it to delete the row where I clicked the button.
$task_id = $row_list['task_id'];
if (!empty($_GET['delete'])) {
mysql_query("DELETE FROM to_do.list WHERE list.task_id=$task_id");
header('Location: http://localhost/to_do/list_view.php');
};
Table + trigger for script:
do { ?>
<?php echo "<table><tr>";
echo "<td>" . $row_list['task_id'] . "</td>";
echo "<td>" . $row_list['task_info'] . "</td>";
echo "<td>" . "<form action=script.php method=get name=delete>
<input type=hidden name=delete value=$task_id>
<input type=submit value=Delete></form>" . "</td>"; ?>
<?php } while ($row_list = mysql_fetch_assoc($list));
echo "</table>" ?>
I am not sure how I can make it work. Thanks in advance!
Maybe
if (isset($_GET['submit'])) {
mysql_query("DELETE FROM to_do.list WHERE list.task_id = $_GET['delete']");
header('Location: http://localhost/to_do/list_view.php');
};
and
echo "<td>" . "<form action=script.php method=get>
<input type=hidden name=delete value=$task_id>
<input type=submit name=submit value=Delete></form>" . "</td>"; ?>

Pass variable in php to next page from a table

I'm creating a table and when the user clicks on the button I want it to open a php file called player_profile, where that page will call up more information on that particular player.
When it comes to passing variable through pages I'm aware that something along these lines will work.
//On page 1
$_SESSION['varname'] = $var_value;
//On page 2
$var_value = $_SESSION['varname'];
Yet when I add it to the button the php file wont load. Below is my code without passing a variable that works.
<?php
$result = mysql_query("SELECT *, CONCAT(FirstName,' ', LastName) AS Name FROM Player WHERE TeamID = '$TeamID' ORDER BY LastName ASC");
echo "<table id='customers' border='1'>
<tr>
<th>PlayerID</th>
<th>Name</th>
<th>Position</th>
<th>Tries</th>
<th>Tackles</th>
<th>Turnovers</th>
<th> Info </th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row["PlayerID"] . "</td>";
echo "<td>" . $row["Name"] . "</td>";
echo "<td>" . $row['Position'] . "</td>";
echo "<td>" . $row['Tries'] . "</td>";
echo "<td>" . $row['Tackles'] . "</td>";
echo "<td>" . $row['Turnovers'] . "</td>";
echo "<td><form action=player_profile.php>
<input name=id type=hidden value='".$row['PlayerID']."';>
<input type=submit name=submit value=info>
</form></td>";
echo "</tr>";
echo "</tr>";
}
echo "</table>";
?>
The code above works perfectly at displaying a table and taking me to the player_profile page. But if change the button to the code below the page so that I can pass a variable it doesn't load
echo "<td><form action=player_profile.php>
<input name=id type=hidden value='".$row['PlayerID']."';>
$_SESSION['varname'] = ".$row['PlayerID'].";
<input type=submit name=submit value=info>
</form></td>";
echo "</tr>";
Don't use the session to pass parameters. It breaks the way users expect a page works.
Use $_GET parameters:
John
Then in player_profile.php do:
$id = isset($_GET["id"]) ? $_GET["id"] : false;
if ($id === false) {
exit("missing input");
}
// etc
The problem with using a session here is that if someone opens two different player profiles what can he expect to see? What if he refreshes one of them?
A more advanced and involved way of parameter passing uses URL Rewriting to get nice looking URLs like:
John
You have code trying to set a session inside your echo. You want it separate:
echo "<td><form action=player_profile.php>
<input name=id type=hidden value='".$row['PlayerID']."'>
<input type=submit name=submit value=info>
</form></td>";
echo "</tr>";
If you want to access the PlayerID variable on the player_profile.php you just use:
$var_value = $_GET['id'];
You could also put it into a session var at that point, but I don't know why you would need that.

PHP Form Submit and anchor back where it was

I have on my page a dataTable with tons of rows. Each row has an id, name, surname and an action column. In the action column there is a textarea where you can add a comment and a button that submits that comment. When I submit the comment I want the page to position itself where it was, but I cannot figure it out how. Any ideas?
Here is the snippet of the code:
$result = mysql_query($query, $connection);
while ($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<form method='post' action='saveComment.php#position_".$row['id']."'>";
echo "<td hidden><input hidden name='id' readonly value=" . $row['id'] . " /></td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['surname'] . "</td>";
echo "<td><input type='submit' name='submit' value='Comment'/></td>";
echo "<a id='position_".$row['id']."'></a>";
echo "</tr>";
}
I tried passing the id of the current row to the script that saves the comment and it does pass the value, after which it should position it back where it was, but it doesn't seem to work :/
use Ajax to submit the form, and on success:
window.location = 'saveComment.php#position_<? echo $row['id']; ?>';
I found out how it works with anchors! The problem was that I didn't pass the id in the saveComment.php script when redirecting back to the original page.
Code in the page where the form is:
if ($_POST['submit']){
$id=$_POST["id"];
header('Refresh: 0.5; URL=originalPage.php#position_' . $id);
//...rest of code goes here...
}
That's it :)
You need to use
<a name='position_".$row['id']."'></a>
instead of
<a id='position_".$row['id']."'></a>

retrieve mysql data

I have this code for listing mysql records and putting it into a table according to the address inputted. My problem is, how to do just the same but this time, making use of textboxes to project the contents of the record corresponding to the same data inputted.
I'm just a beginner with no talent. Maybe you could give me some idea on how to do it.
mysql_select_db("hospital", $con);
$result = mysql_query("SELECT * FROM t2 WHERE ADDRESS='{$_POST["address"]}'");
echo "<table border='1'>
<tr>
<th>HospNum</th>
<th>RoomNum</th>
<th>LastName</th>
<th>FirstName</th>
<th>MidName</th>
<th>Address</th>
<th>TelNum</th>
<th>Nurse</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['HOSPNUM'] . "</td>";
echo "<td>" . $row['ROOMNUM'] . "</td>";
echo "<td>" . $row['LASTNAME'] . "</td>";
echo "<td>" . $row['FIRSTNAME'] . "</td>";
echo "<td>" . $row['MIDNAME'] . "</td>";
echo "<td>" . $row['ADDRESS'] . "</td>";
echo "<td>" . $row['TELNUM'] . "</td>";
echo "<td>" . $row['NURSE'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
As a suggestion, I do not think displaying the values inside of a textbox is the best idea. With that being said, you achieve your results by performing the following
while($row = mysql_fetch_array($result)) {
echo "<input type=\"text\" value='" . $row['HOSPNUM'] . "'><br />";
echo "<input type=\"text\" value='" . $row['ROOMNUM'] . "'><br />";
....
}
You would need to escape the " inside of the text boxes by using PHP's escape special character \
You need to search from your present tables and use AJAX to notify the user.
I would suggest you look into a framework in PHP which would help you a LOT since you are just starting your projects. List of frameworks can be found at http://www.phpframeworks.com/
As far as i could understand your question, you want to retrieve data from mysql based on the input of the text boxes. This is how you can go about it:
<form action="yourpage.php">
<input type="text" name="text1">
<input type="text" name="text2">
<input type="text" name="text3">
</form>
Now you can retrieve data from mysql using where clause.
select * from table where text1 = 'text1' and text2 = 'text2' and text3 = 'text3'
Note: You need to change the names in form and query as per your requirement.

Categories