How To Delete Row in Database With Delete Button - php

I am trying add a "Delete" button in my application. The button's functionality is to remove the database row when it is clicked. I guess I will need to create something like delete.php and link the button to it. But I have no idea how to do it. Can anyone help?
Below is my code:
<table id="edit_accounts" class="tablesorter">
<thead>
<tr><?php
while($v=mysql_fetch_field($result)) if($v->name!="paid_for_year_date" && $v->name!="approved"){
?><th class="header"><?php echo display_version($v->name);?></th>
<?php
}
?>
<th class="header">Actions</td>
</tr>
</thead>
<?php
while($row=mysql_fetch_assoc($result)){
$row["category"]=$categories[$row["category"]]["category_name"];
$pfydate=$row["paid_for_year_date"];
unset($row["paid_for_year_date"]);
$extra_link="";
if($pfydate==$row["join_date"]){
$extra_link="<br/><a href='mark_as_paid.php?account_id=".$row["account_id"]."&auth_code=".md5("lgotadmin".$row["account_id"])."'>Mark as Paid</a>";
}
if($row["approved"]==0){
$extra_link.="<br/><a href='approve.php?account_id=".$row["account_id"]."&auth_code=".md5("lgotadmin".$row["account_id"])."'>Approve</a>";
}
unset($row["approved"]);
?>
<tr><td><?php echo implode("</td><td>",$row);?></td><td>Edit Account<br>Edit Transactions<br/>Edit Account<?php echo $extra_link;?></td></tr>
<?php
}
?>
</table>

Follow these steps
1) You need to create a file deleteFile.php (or) you can create a single file and do all the operations like insert, update and delete by using if conditions.
2) Then, you should pass the row id or some identifier to the page to identify which row has to be deleted.
3) In that, you should write a query to delete the row by using this identifier.
4) Then you can give an alert msg and redirect into the page.
5) In the case of single file, you need to send an operation type like delete with the id to the page

You didn't mention how do you want to update your page. There are essentially two way to refresh the page.
Refresh the entire page.
In this you will call your delete.php with some parameters. In delete.php you perform the delete operation based on given parameter and then send it back to your code responsible for genering your page. The newly generated page will not have deleted row.
AJAX
If you are using AJAX based call to delete item
This becomes little bit more complex on browser related code.
- You pass id/parameters to delete.php via AJAX call.
- delete.php deletes row and sends status message back to browser.
- You javascript handles the response and performs required DOM manipulation to delete row from table in case of successful operation.

Related

Using PHP hyperlinks to extract data from mysql table

I am currently stuck on how to use $_GET from a hyperlink and send it to another php page for processing.
I have 2 php pages. The first page, we'll call it page1.php, gets user input (customerID) from a HTML page through a textbox and according to that id, goes into the database and finds relative information such as the Customer Name and the Address etc using logical joins.
$id = $_GET['custID']; //This is to obtain the input from the HTML page.
I have that working perfectly.
My next task is to create another php page, we'll call it page2.php, in where i am required to output only the customerID and the customerName, and from there i am required to hyperlink every record in the Customer ID row which references to page1.php and when the user clicks any of those links, it should only show information according to that customerID in a table.
So far i can make each record in the Customer ID row link to page1.php but i am unable to output any results.
<?php
while ($row = mysql_fetch_array($rs)) { ?>
<tr>
<td><?php echo $row["customerName"]?></td>
<td><?php echo "<a href='page1.php?'>{$row["custID"]}</a>"?></td>
</tr>
<?php }
Any help would be much appreciated.
Change the link to following
<?php echo ''.$row["custID"].'';?>

run php on html link click and stay on same page after refresh

I have a table of data that I want to allow users to remove rows from by clicking a link in the table. I'm looking to have the page refresh and the row that the user selects removed from the table. This is done based on calling a php function which alters the table's database. What I have so far is
<tr>
<td width = '35%'><?php echo $the_title?></td>
<td width = '20%'><?php echo $val1?></td>
<td width = '10%'><?php echo $val2 ?></td>
<td width = '15%'><a href='?remove_now=<?php echo $row?>' name=''>Remove Now</a></td>
</tr>
<?php
if (isset($_GET['remove_now'])){
$row_to_remove=$_GET['remove_now'];
my_func ($row_to_remove);}
?>
When I click the Remove Now link it changes the page, because I'm already on a hashed page, which I don't want it to do. It also doesn't run my_func.
Any thoughts? Thanks
PHP runs on the server side, where a page update runs on the client (browser). If you want to execute php code, either the page has to be refreshed, or you need to use a javascript ajax call that will actually access the "page" on the server you're looking for, grab its data, and make it available to the current page (that made the ajax call).
If you just want to manipulate the page and don't need any data from the server, just use javascript. Javascript is for dom manipulation, not php.
Well, you cannot do what you are trying to do with php. You need to use javascript to do what you want.
Here are some ways you can accomplish what you want
1) use javascript to manipulate the table that is already rendered on your page
or
2) use php to render the table without the desired row when the page initially loads
if you need to use php as part of the row removal process, you should look into ajax.
based on you comment, try something like this:
<tr>
<td width = '35%'><?php echo $the_title?></td>
<td width = '20%'><?php echo $val1?></td>
<td width = '10%'><?php echo $val2 ?></td>
<?php
$maxnumrows = 10;
for($i=0;$i<$maxnumrows;$i++){
if ( (!isset($_GET['remove_now'])) || ( (isset($_GET['remove_now'])) && ($_GET['remove_now']!=$i) ) ){
?>
<td width = '15%'><a href='?remove_now='<?php echo $i?> name=''>Remove Now</a> </td>
<?php
}
}
?>
</tr>
Closing quote on href attribute is wrong. It should be after closing php tag like this.
<td width = '15%'><a href='?remove_now=<?php echo $row?>' name=''>Remove Now</a></td>
And yes .. javascript and ajax will work as well as just clicking the link and display same page. If you don't want url to be changed put header("Location ....") after my_func call
Cheers
It sounds like you're trying to delete entries from the database, and you're just thinking a little backwards. Here's a simplified piece of logic that may help you understand how to go about this:
// See if they just deleted something
// If so we will delete the data and reload the page
if(isset($_GET['delete_id']))){
// Run the code that deletes the entry from the database
deleteFromDatabase($_GET['delete_id']);
// Now that we've done that just cleanly reload the page
header('Location: '.$_SERVER['PHP_SELF']); // This refers to the current page without the $_GET query string - there must not be any page output before you call this
die(); // Always kill the script after reloading the location header
}
// Here's your normal output (whatever you would normally do for output)
getTableData();
outputTable();
echo '<table>Whatever etc...';
?>

Change row value onclick html table like phpMyAdmin

I'm basically trying to use a simple method of editing table stored inside a mySQL database, but don't want to go through a different editing page, so my theory is :
Show all data inside a HTML table as usual would as followed.
<table class="table table-bordered sortable">
<thead>
<tr>
<th>Marchant</th>
<th>URL</th>
<th>Status</th>
<th>Sold</th>
<th>Deals</th>
<th>Sites</th>
<th>Found</th>
<th>Seen</th>
</tr>
</thead>
<tbody>
<tr>
<td>value</td>
<td></td>
<td></td>
<td>Sold here</td>
<td>Deals here</td>
<td>Sites here</td>
<td>null</td>
<td>null</td>
</tr>
</tbody>
I want to be able to edit the data on the fly, simply by clicking on the value displayed and changing it.
The question is how would i go about by doing this, is it even possible ?
If you're using jQuery check out edit-in-place plugins (jEditable is a good one).
The idea behind this is to set up a click event listener on the table cell, in which you append an editable textarea/form with the contents of the TD.
On change or submit the data is sent trough an ajax request to the server, which updates the DB and eventually returns back sanitized data (you update the TD contents with it).
Yes, its possible.
It's mostly frontend.
Here is example of creating input after you click on a cell. (jQuery)
// once page is loaded
$(document).ready(function() {
// adding an event when the user clicks on <td>
$('td').click(function() {
// create input for editing
var editarea = document.createElement('input');
editarea.setAttribute('type', 'text');
// put current value in it
editarea.setAttribute('value', $(this).html());
// rewrite current value with edit area
$(this).html(editarea);
// set focus to newly created input
$(editarea).focus();
});
});
After this you can add event to newly created input. (for example when user hits Enter)
Then you do AJAX request and send new values to a .php script.
You also need to add id to a newly created element so you know exactly what cell is needed to be changed after data is sent via AJAX.
Also, don't forget to validate data before putting to MySQL.
So if everything went good or not you return value back, and according to the value you write JavaScript code to to remove element from a cell / put edited value or popup a message.
Hope this helps.

delete a particular row from session array

I am a beginner in php.
I want to know how a particular row in a php session array can be deleted(not from the database, but from the page only).
I have a table namely, issues having columns issue_id & issue_descrp.
The page is displayed with a table in which each row contains issue and its corresponding id. Each row contain a delete button too. What I want is to delete the corresponding row from the page when I click the button.
This is my php code:
<?php
foreach($_SESSION['meeting_issues'] as $meeting_issues)
{
$query="select issue_id,issue from issues where issue_id='$meeting_issues'";
$result=$_SESSION['connection']->query($query) or die (mysql_error());
while($row = $result->fetch_assoc())
{?>
<?php $issue_id=$row['issue_id']; ?>
<tr><td><?php echo $row['issue_id']; ?></td><td><?php echo $row['issue']; ?></td><td><input type="button" name="<?php echo $row['issue_id']; ?>" id="button" value="Remove"/></td>
</tr>
<?php
}
}
?>
Hope my question is clear. Please help me. Thanks in advance.
use unset to delete array elements such as those in $_SESSION
http://php.net/manual/en/function.unset.php
do not delete the whole session this way, use this instead
http://php.net/manual/en/function.session-unset.php
To remove a row on the page itself, you will need Javascript or jQuery. jQuery is advised because of all the possibilities it gives and it is easier to use than normal Javascript.
jQuery:
$("#button").parents("tr:closest").remove();
Javascript:
document.getElementById('button').parentNode.parentNode.parentNode.removeChild(document.getElementById('button').parentNode.parentNode);
As you can see, jQuery is alot faster and more easy to type.
You are using an ID for the buttons, but the ID is always the same. I recommend using classes for this, because an ID should be unique on a page.
jQuery website

Php inside a javascript function?

I have a feature on my users inbox that allows users to check/uncheck messages in their inbox that they want to make favourite.
I'm currently testing what happens when a user checks the box (clicks on the image and causes it to go from greyed out to colour meaning the box is checked).
Anyway as you can see from the code below when the box ischecked this url is suppose to be loaded: http://mysite.com/messages/favourite_checked
The message_id of the row the user has checked the box on is suppose to be added onto the end of the url this then loads my controller "messages" and method "favourite_checked" which then passes a variable that grabs the message_id from the url, stores it in a variable then sends it the my model and it is used in a mysql query.
Basically I update the favourites column of my messages table and set it to = 1 where the message_id from url matches the one in the messages table in my database. So yea, where the match is found the "favourite" column in that row is updated to 1. 1 = favourite 0 = not favourite.
Any I just thought I would make it clear what was happening..
My problem is nothing happens when I check the box, nothing is updated so I feel I must be doing something wrong where I try to add the id to the url in the javascript function.
I've tried $(post) also.. nothing happens then also.
Maybe someone can spot it because I really don't know what the problem is.
<script type="text/javascript">
// favourite check box
$('input.favourite:checkbox').simpleImageCheck({
image: '<?php echo base_url()?>images/messages/check.png',
imageChecked: '<?php echo base_url()?>images/messages/unchecked.png',
afterCheck: function(isChecked) {
if (isChecked) {
//query to db from php to update favourite number to 1
$.get('http://mysite.com/messages/favourite_checked'+'<?php foreach ($query as $row): ?><?php $row['id']; ?><?php endforeach; ?>');
}
// else (!isChecked)
// {
// //query to db from php to update favourite number to 0
// $.get('http://mysite.com/messages/favourite_unchecked');
// }
}
});
</script>
I think your basic problem is some confusion about when the PHP is running vs the javascript.
The PHP you put on the page is server side, it will load first, then the javascript will run client-side.
This part here:
$.get('http://mysite.com/messages/favourite_checked'+'<?php foreach ($query as $row): ?><?php $row['id']; ?><?php endforeach; ?>');
Seems like you are wanting this to be dynamic based on what you checked, but I don't see how that url is going to show specifically what you are looking for.
About the PHP:
I think you want to replace this:
<?php $row['id']; ?> // does nothing
with this:
<?php echo $row['id']; ?> // echo's the id
Although I´m not sure that that will work as the loop you have there will generate a strange url, just adding all id's...
About the javascript:
I´m not familiar with the simpleImageCheck() function you are calling, but does it have an onClick or onChange event handler? Otherwise I don´t see your code being run at all.

Categories