I am new to php.
I have a page with a list of names. When a name is clicked it will need to make a change on that persons record in MySQL and then return the user to the same page to click the next name as they need. How should I go about doing this?
Example:
George Bush - When clicked it needs to set a MySQL field to "TRUE".
Bill Clinton - Same thing...
I will eventually need another page that auto refreshes to display the changes that are made to the MySQL table.
Thanks!
Link that makes a change in a database
Moderator: General Moderators
- Peter Anselmo
- Forum Commoner
- Posts: 58
- Joined: Wed Feb 27, 2008 7:22 pm
Re: Link that makes a change in a database
You could just make the names go to the page that you're on. Then you put the update code at the top of the current page, and run it if a name has been clicked. Once it updates, it will load the page like normal, and a user can select another name.
Alternately, if you'd rather go to a different page, $_SERVER['HTTP_REFERER'] holds the url of the page you were just on, you can use it to send the user back.
Alternately, if you'd rather go to a different page, $_SERVER['HTTP_REFERER'] holds the url of the page you were just on, you can use it to send the user back.
Re: Link that makes a change in a database
That's my question really. What is the code to make a database change when a link is clicked? I know how to do it from a form, just not a link.
Are you talking about having the results display on the same page as the clickable names? That probably won't work for my situation.
Thanks for the reply!
Matt
Are you talking about having the results display on the same page as the clickable names? That probably won't work for my situation.
Thanks for the reply!
Matt
- Peter Anselmo
- Forum Commoner
- Posts: 58
- Joined: Wed Feb 27, 2008 7:22 pm
Re: Link that makes a change in a database
If you want to pass information via a link (such as a name to update), you can pass it as a query parameter (Designated by the "?" and "=" symbols).
Then you can access it in your new page under the _GET scope. This is identical to creating a form with method="get".
However, BE EXTREMELY CAUTIOUS WITH THIS. It is generally bad practice to have a link update a database, because things like crawlers can click on links, and fire any code associated with it. Also, some applications will pre-load the pages from all the links on your current page to speed up the browsing experience. Thus, it would trip the code on the new page for EACH link. Using a form is much better practice.
Code: Select all
<?php
echo '<a href="nameupdate.php?name=' . urlencode("George Bush") . '">George Bush</a><br />';
echo '<a href="nameupdate.php?name=' . urlencode("Bill Clinton") . '">Bill Clinton</a>';
?>Code: Select all
<?php
if( isset( $_GET['name'] ) ){
// code to update database here
}?>