Link that makes a change in a database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Zero20two
Forum Newbie
Posts: 9
Joined: Wed Feb 13, 2008 11:51 am

Link that makes a change in a database

Post by Zero20two »

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!
User avatar
Peter Anselmo
Forum Commoner
Posts: 58
Joined: Wed Feb 27, 2008 7:22 pm

Re: Link that makes a change in a database

Post by Peter Anselmo »

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.
Zero20two
Forum Newbie
Posts: 9
Joined: Wed Feb 13, 2008 11:51 am

Re: Link that makes a change in a database

Post by Zero20two »

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! :lol:

Matt
User avatar
Peter Anselmo
Forum Commoner
Posts: 58
Joined: Wed Feb 27, 2008 7:22 pm

Re: Link that makes a change in a database

Post by Peter Anselmo »

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).

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>';
?>
Then you can access it in your new page under the _GET scope. This is identical to creating a form with method="get".

Code: Select all

<?php
if( isset( $_GET['name'] ) ){
 
   // code to update database here
 
}?>
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.
Post Reply