I'm simply trying to implement an undo button using PHP and MySQL, for example if a user deletes a post or any general item there will be an option to undo that MySQL query and leave the post as it was. Thanks in advance.
You need a flag column to do thing. For example, the del_flag, if the user delete a record, set this flag to 1, when undo, set it to 0.
For clarification, once a row is deleted from a MySQL database, it can't be "undone". There's no going back once an item has been deleted.
So, to mimic this "undo" functionality, you need to (as already suggested in another answer) create a new field in the database table that keeps track of whether a post has been deleted or not. Let's call this new field Deleted and say it can have only two values -- yes (meaning it has been deleted) and no (meaning it hasn't been deleted).
When a post is deleted, you can mark that Deleted field as yes and "hide" the post from view so that it appears to have been deleted (even though it actually is still present in the database).
Likewise, when a user click on the "undo" button that will switch the Deleted field back to no.
In your PHP code, you'll want to check the value of the Deleted field for each post, and then hide or show the post accordingly.
As with any challenge like this, there are multiple solutions. If the above approach isn't what you're looking for, and you really want to actually delete the post from the database, then you could save the deleted post in another database table first (and keep a reference to it, of course, so that you can restore it if the "undo" button is used), and then delete the post from the "main" database.
When you delete the post just mark it as not 'live'. Then to undo just mark it as 'live' again.
Then in your queries just filter results based on if they are 'live' or not.
Related
I am dealing with alerting whether call/meeting slot is available or not before a user saves a call/meeting. My task mainly deals with the editing part. There are two approaches to edit a specific call.
Select a particular lead in leads page -> go to detail view -> Log Call -> save -> come back to same lead detail view again -> click on edit call in the activities subpanel. Now if you check the URL, you see a url parameter with name "record=abcde-fghi..." which I am using during validations.
Now go to activities menu on the top and in the calendar you see all the held calls. Click on a particular call and then edit it. Now in the edit page, when you check the URL, you don't see "record=abcde-fghi..." url parameter which is useful for me to do validations. How to get the record id in this case?
When I checked the save function, it is accessing this id from $bean. But I don't know how to access this bean before saving the call to database.
I am taking the form parameters- startdate, mins, hours, parent_id, user_asign_id etc and triggering an ajax call based on the record id for first case above, But I am missing record id for second case to validate the call/meeting.
Please help. Thanks in advance!
The id is only being generated after you save the record, of course. And that's why you don't see any id in the url above.
A workaround is to use a different ajax call, depending on whether you have the id or not. You could use '-1' as your id and check the server for this '-1' id.
Hope it helps
I'm currently developing a database/website server interface to facilitate inputting data for a data collection project. There are two types of additions being made to the database: A and B here. Some of the tables in the database that handle these entries are as follows:
dcs_projectname_a
dcs_projectname_b
Each of these have tables for all the required input fields in addition to things like creator, timestamp, etc.
The pages on the website facilitate three different options: add, view, and edit. Each page for each type of entry performs the respective function. That is, the add page adds, view page views, etc.
I am just about done; however, there is a major challenge I haven't really confronted yet: concurrency. There will be multiple users adding content to the database at the same time. Each entry is given its own specific id and there CANNOT be any duplicate id's. That is, the first a entry is A000001, the next is A000002, and so on.
On the add and edit pages, there is a disabled field for the user to view the id for other uses when physically documenting entries.
What I need to figure out is how to implement concurrency management so that when users are concurrently adding a's that they will not be under the same id and row.
The add page automatically generates the id to be used by accessing the database's most recent id and adding one.
My thought was to create a new row in the table every time the add page is opened and give it the calculated id. Then, when information is added it performs a modification to that existing row. This way, if another user opens the add page while another entry is currently being added it will be given the future id, not the same one.
With this method I need a way to delete this entry if the user leaves the add page or closes the browser. Then, I also need other users with open add pages to automatically update their id's to one less when the first user (or any other user less than the most id being used) leaves their add page and cancels the addition.
This is kind of a complicated explanation and if you don't understand let me know and I'll try to answer as best as I can. Any help is much appreciated!
Thanks
There's a number of solutions to your problem, but you seem to have made things harder by having your application generate the record IDs for you.
Instead, you could just be using MySQL's AUTO_INCREMENT functionality to automatically generate/increment the record ID for you (upon insert). MySQL will ensure that there are no duplicates, and you can get rid of the extra database call to retrieve the most recent ID.
I am really new to database management. I installed wamp and have set up a database in phpmyadmin. Now I need to refresh the value of a particular row which contains some data. How do I do this. Are there any good tutorials for doing this available on the net. Can you please point me in the right direction as I don't know what to do now.
It's called "UPDATE".
mysql_query("UPDATE table_name SET field_name = new_value");
If you add a condition to the end, you can decide which record get's updated.. Assuming you have an "id" field (which is unique to each record), you could change the record with ID 50 by appending:
where id = 50
to the query.
I think you are referring to the visual thing more than what is happening on the background. If you hit the button, lets say, update, the page will go to the server and come back with the new, updated results of what you are trying to update. Before digging into using complicated techniques to make this happen nicely, try to understand the page lifecycle. Then start applying javascript(and AJAX) if you want to make this happen nicely. Hope this helps,
I have just started with Zend Framework, so might be a bit of a silly question coming up.
I have a Form with 5 Checkboxes. A User can click on as many checkboxes as needed. This gets entered into the Database. (1:n)
This all works fine. But now I am gotten to the part where a User can Edit the Post. The Checkbox gets shown as well as which once are active. But how do I update this now?
The Rest of the Post Update works fine, collecting the Data and send an update:
$this->getDbTable()->update($data, array('post_id = ?' => $id));
Now I want to update the Checkboxes, the 1:n Relationship. But how would I do that if I for example had 4 Checkboxes active but after the update I want only 3 active? Should I delete all entries first and than do a normal insert or is there a trick to do it?
Hope someone can help. Thanks !
Assuming that the row existing in your database is what you use to indicate that it should be checked, you will need to delete it as the update() function won't do that.
If delete / re-insert is going to be too expensive of an operation, perhaps store the checked values in the session and do an array_diff($previous_selected, $currently_selected) to get the list of items to delete.
One thing to note is that the formCheckbox view helper by default creates a hidden input with the same name as the checkbox with a value of 0.
In a PHP application I'm building, I'd like to have an 'editable' table. The idea is that each row will have an edit button, which, when clicked, will replace certain fields with text fields and select lists and change to a save button. When the user clicks save, the data data should be validated and changed if appropriate.
I'm mainly tackling this as a learning project (I'm aware there's a ton of stuff already out there) and to see if I can get anything 'cool' working. I've created a PHP table-generating class that can take an array of objects as a datasource, and can have columns created based on those class methods.
e.g.
$table = new Table($dataSource);
$table->addColumn('Name', 'getName');
$table->addColumn('Amount Due', array('getOrdersManager', 'getTotalAmountDue')); //First calls getOrdersManager() on each data item and then calls the getTotalAmountDue() on the result
I'd like to try my hand at extending this to be able to the table row and have those changes reflect on the corresponding object in the data source.
I don't really have very much experience with AJAX although it's clearly going to play a very important role in getting this to work correctly.
Any tips on how I should approach such a task?
Edit: I'm not really interesting in looking at Ajax libraries at this point (I do have some experience with jQuery). I'm more interested in learning the basics of Ajax at this point.
my tip is to use jquery(does most of the heavy lifting for you and is easy to learn).
The idea is that each row will have an
edit button, which, when clicked, will
replace certain fields with text
fields and select lists and change to
a save button
http://api.jquery.com/click/
When the user clicks save, the data
data should be validated and changed
if appropriate.
http://api.jquery.com/jQuery.post/
Some things to be aware of/think about:
Are you going to send every field change to the server, or only the whole row (the latter is more resource efficient, but not necessarily as accurate)
How are you going to ensure the data displayed stays accurate even if the update to the server fails for some reason (either a network failure or a DB/validation error)
How will you ensure the user has permission to update the record and that you don't open a security hole by allowing the AJAX responder just to update whatever record it is told to. My approach has been that if a record is shown in the interactive table then the user has the permission to update it, so a cache of record IDs is held in the session when the table is created
Are you going to load options dynamically? If you don't, then a long table can end up containing a lot of HTML because of repetition of the select controls, but again it is more resource efficient not to have a request every time a user clicks into a dropdown. One compromise might be to put the options into a hidden HTML field and load them dynamically into the correct place when a user clicks a dropdown