Page 1 of 1

Link that makes a change in a database

Posted: Thu Feb 28, 2008 7:26 pm
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!

Re: Link that makes a change in a database

Posted: Thu Feb 28, 2008 7:31 pm
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.

Re: Link that makes a change in a database

Posted: Fri Feb 29, 2008 7:10 am
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

Re: Link that makes a change in a database

Posted: Fri Feb 29, 2008 9:23 am
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.