473,396 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Updating a MySQL table value through HREF link

AutumnsDecay
170 100+
Hey everyone.

I have been writing a testimonials backend for a client who wishes to use the feature. How it is supposed to work is like this:

The user writes a testimonial of their experience at the salon into a basic HTML form. When the user clicks send it runs a PHP script inserting it into a MySQL table.

The table has 4 fields: id, msg, name, active

When the testimonial is submitted it obtains a default value of 0 for the 'active' field. This makes the testimonial 'inactive'.

The salon owner wants to login and see all the pending testimonials (where active = 0). She can then review each one individually. If she wants to accept that testimonial she then clicks on the 'Activate' link, which will update the active value for that testimonial ID to 1 instead of 0.

When users come to the website one random testimonial will be pulled where the active value is 1 (meaning its active). Each time the page is loaded it will be a different one, again where the active value is 1.

My question is how to set the active value to 1. I've been trying an array of different codes, both written by me for this piece or by other developers who've freely posted simple solutions. I'm at a loss and it's needed by tomorrow morning.

Here's the code for what I have tried, most recently.

Expand|Select|Wrap|Line Numbers
  1. <?
  2.              if($_SESSION['access'] == 1)
  3.             {
  4.             $db = new DB();
  5.             $rows = $db->getRows();
  6.             $id = $_REQUEST[$row["id"]];
  7.             $str = "SELECT * FROM testimonials WHERE active = 0";
  8.             $db->query($str);
  9.             $rows = $db->getRows();
  10.             foreach($rows as $row)
  11.             print '<div style="border: 1px dotted #fff; padding: 5px; width: 400px; margin-bottom:10px;"><b><u>' . 
  12.             $row["name"] . '</b></u><br />' . $row["msg"] . '<br/><p align="left">Testimonail ID ' . $row["id"] . ' - 
  13.             <a href="testimonialsindex.php?mode=activate"><img src="images/activate.jpg"></a><br/></div>';
  14.             }    
  15.             else
  16.             {
  17.             print 'You are not logged in! Please login to view this page.';
  18.             }
  19.  
  20.         //if logged in, add a javascript file
  21. if($_SESSION['access'] == 1)
  22. {
  23.     if($_REQUEST['mode'] == 'activate')
  24.         {
  25.         $db = new DB();
  26.         $db->query("UPDATE testimonials SET active 1 WHERE id = " . $row["id"]);
  27.  
  28.         }
  29.     }
  30.  
  31.  
  32. ?>
  33.  
I really need help and hope that somebody will able to help me.

Thanks very much!
Feb 15 '09 #1
11 7518
Markus
6,050 Expert 4TB
Are you just trying to update the value in the MySQL table?

If so, check out MySQL UPDATE. Otherwise, could you explain a little more concisely?
Feb 15 '09 #2
AutumnsDecay
170 100+
Well that helped a little bit, Markus. It now updates the table value, however it's only letting it activate from bottom to top.

What I mean is this:

I have the testimonials displayed vertically like so:

-------------------------------------
| Name: Mat |
| this place is great! |
| |
|Testimonial ID 1 |
------------------------------------

-------------------------------------
| Name: Sandy |
| this place is rocks! |
| |
|Testimonial ID 2 |
------------------------------------

-------------------------------------
| Name: Liz |
| this place is cool. |
| |
|Testimonial ID 3 |
------------------------------------


I have to activate ID 3 first, then ID 2, then ID 1. I can't just choose one from the list to activate. Let's say I want to leave ID 3 and ID 2 in pending (active = 0) and activate ID 1, I'd have to activate all three and then unactivate ID 3 and ID 2 from a seperate page.

Why's that happening?
Feb 15 '09 #3
hoopy
88
You have to pass the ID in the URL string..

So something like:

<a href="testimonialsindex.php?mode=activate&id=ID_OF _THE_RECORD">

Then when you do the update only update that ID.
Feb 15 '09 #4
AutumnsDecay
170 100+
Excellent. I had tried that once before but wasn't sure if it would work. Used the '&id=' thing, and had the 'activate' mode use the $_GET to obtain the id from the url.

It works perfectly.

Thanks.
Feb 15 '09 #5
AutumnsDecay
170 100+
Hmm. I have to refresh in order for the testimonial to not be displayed. I've included a 'header' function into my script but when the page is loaded I get the

Warning: Cannot modify header information - headers already sent by (output started at...

error. Is there anyway I can add a refresh or header into my script? Here's what it looks like now, by the way:

Expand|Select|Wrap|Line Numbers
  1.  <?
  2.              if($_SESSION['access'] == 1)
  3.             {
  4.             $db = new DB();
  5.             $rows = $db->getRows();
  6.             $str = "SELECT * FROM testimonials WHERE active = 0";
  7.             $db->query($str);
  8.             $rows = $db->getRows();
  9.             foreach($rows as $row)
  10.             print '<div style="border: 1px dotted #fff; padding: 5px; width: 400px; margin-bottom:10px;"><b><u>' . 
  11.             $row["name"] . '</b></u><br />' . $row["msg"] . '<br/><p align="left">Testimonail ID ' . $row["id"] . ' - 
  12.             <a href="testimonialsindex.php?mode=activate&id='. $row["id"] .'"><img src="images/activate.jpg"></a><br/></div>';
  13.             }    
  14.             else
  15.             {
  16.             print 'You are not logged in! Please login to view this page.';
  17.             }
  18.  
  19. ?>
  20.  
  21. <?        
  22. if($_SESSION['access'] == 1)
  23.     {
  24.     if($_REQUEST['mode'] == 'activate')
  25.         {
  26.         $db = new DB();
  27.         $id = $_GET['id'];
  28.         $db->query("UPDATE testimonials SET active='1' WHERE id =" .$id);
  29.         header ('Location:testimonialsindex.php');
  30.         }
  31.     }
  32. ?>
  33.  
That's how the the testimonials are printed, and then the lower half is how they're activated. I would think that if an 'IF' statement were present it would be okay to have the header in there to automatically 'refresh' the page. However, it won't.

Suggestions?
Feb 15 '09 #6
Markus
6,050 Expert 4TB
You're updating the records after you have displayed them. Do it the other way around. No need for a header refresh.
Feb 15 '09 #7
hoopy
88
You cant send a header request having already sent data, You would do better to have your update before the display so using your code something like:

Expand|Select|Wrap|Line Numbers
  1. <?
  2. if($_SESSION['access'] == 1)
  3. {
  4. if($_REQUEST['mode'] == 'activate')
  5.   {
  6.   $db = new DB();
  7.   $id = $_GET['id'];
  8.   $db->query("UPDATE testimonials SET active='1' WHERE id =" .$id);
  9.   header ('Location:testimonialsindex.php');
  10.   }
  11. }
  12.  
  13. if($_SESSION['access'] == 1)
  14. {
  15.   $db = new DB();
  16.   $rows = $db->getRows();
  17.   $str = "SELECT * FROM testimonials WHERE active = 0";
  18.   $db->query($str);
  19.   $rows = $db->getRows();
  20.   foreach($rows as $row)
  21.   print '<div style="border: 1px dotted #fff; padding: 5px; width: 400px; margin-bottom:10px;"><b><u>' . 
  22.   $row["name"] . '</b></u><br />' . $row["msg"] . '<br/><p align="left">Testimonail ID ' . $row["id"] . ' - 
  23.   <a href="testimonialsindex.php?mode=activate&id='. $row["id"] .'"><img src="images/activate.jpg"></a><br/></div>';
  24.   }    
  25.   else
  26.   {
  27.   print 'You are not logged in! Please login to view this page.';
  28. }
  29. ?>
Feb 15 '09 #8
Markus
6,050 Expert 4TB
... deja vú
Feb 15 '09 #9
AutumnsDecay
170 100+
I'm now getting this error:

Warning: Cannot modify header information - headers already sent by (output started at /home/hairstre/public_html/testimonialsindex.php:20) in /home/hairstre/public_html/testimonialsindex.php on line 42

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/hairstre/public_html/lib/db.php on line 53

Warning: mysql_free_result(): supplied argument is not a valid MySQL result resource in /home/hairstre/public_html/lib/db.php on line 57

I switched up my code with the code that hoopy posted. It did refresh correctly, however with those errors above the testimonials.

Any ideas?
Feb 15 '09 #10
hoopy
88
I dont understand why you are calling $rows = $db->getRows(); before even running a query. Can you just post all your code as this refers to line 42.
Feb 15 '09 #11
AutumnsDecay
170 100+
I didn't realize I had that in there.

Removed it and the header function. Works great.

Thanks guys.
Feb 15 '09 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Mark Pacey | last post by:
Hi, I have a web page that gets info from a mySQL database for various items, and slaps that info, along with pics onto the page. ( in a shopping site stylee) The pics have an href link, and I...
0
by: Ron Hocking | last post by:
When I link a MySQL table containing a TIME column to Microsoft Access the value does not display. If I edit the value it updates correctly in mysql but if I refresh the Access datasheet view the...
0
by: beary | last post by:
I am using php5 with mysql and also using excel 2003 running on winxp. Anyway, I am currently opening my excel.xls file, then saving as csv file, then closing, then copying it over to the web...
11
by: kennthompson | last post by:
Trouble passing mysql table name in php. If I use an existing table name already defined everything works fine as the following script illustrates. <?php function fms_get_info() { $result =...
1
by: Franky | last post by:
I want to use a query that will update a table if a specific entry exists, or inserts the data if it doesn't. The code below shows what i have so far (taken from a previous post and modified) ...
4
by: mramsay | last post by:
Hi, I'm having a real problem creating a dynamic hyperlink for my website. I want to pull the field name from mysql table. Field name is description. I would like this to be a hyperlink on my...
10
by: help4me | last post by:
I am having trouble updating a table. The logic seems so simple but I just can’t get it to work. The table is named test. The column names are passcode, name, address, city, state, zip and email....
1
osward
by: osward | last post by:
Hi all, I got code over the net for paging mysql table, it provides Prev pages Next link at the bottom of the table. However, I have a pretty large table to display (average over 400+ rows). Even...
2
pradeepjain
by: pradeepjain | last post by:
hii ppl, I have a small prob.i need to feed some things to DB table manually . say a date which will be common to around 10 id's . So i there a way to update all these ID's together like ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.