Updating a boolean value in a database when a link is clicked

Discussion in 'PHP' started by TOSCS, Mar 7, 2011.

  1. #1
    I have a page which displays, at random, a field from a table in a MySQL database.

    Next to this field I wish to have a small image icon, which when clicked, 'flags' that record (for the purpose of reporting it to admin). I have a boolean field 'flagged' in the table, default value is 0/FALSE.

    Here is the code used to display the record, I have removed some HTML & PHP that is not relevant
    
    <?php
    include("config.php");
    ?>
    
    /**
    Display the results from the database
    **/
    $q = "SELECT * FROM entries ORDER BY rand() LIMIT 1";
    $r = mysql_query($q);
    
    if(mysql_num_rows($r)>0): //table is non-empty
    	while($row = mysql_fetch_assoc($r)):
    		$net_vote = $row['votes_up'] - $row['votes_down']; // ignore this code for purposes of thread!!
    ?>
    
    <div class='entry'>
    <h1><?php echo $row['title']; ?></h1>
    </div>
    
    PHP:
     
    TOSCS, Mar 7, 2011 IP
  2. awood969

    awood969 Member

    Messages:
    186
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    40
    #2
    If your looking for the SQL code this would be;

    UPDATE (tablename) SET (fieldname)='(true/false)' WHERE (uniqueidentified)=(unique id).

    To clarify the example

    UPDATE posts SET flagged='false' WHERE postID='10'

    Hope this helps

    Andrew
     
    awood969, Mar 7, 2011 IP
  3. TOSCS

    TOSCS Member

    Messages:
    79
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #3
    I'm not sure how I can get it to update the same record that has been pulled up on the page.
     
    TOSCS, Mar 7, 2011 IP
  4. awood969

    awood969 Member

    Messages:
    186
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    40
    #4
    I would need to have some uniqueID to be able to determine what it is. For example you will notice on these forums that there are category ID's, forum ID's, thread ID's and post ID's. If you hover over one of the post numbers on the top right hand side of any any post you will see a URL, it looks something like;

    forums.digitalpoint.com/showthread.php?t=2114175&p=15894210#post15894210

    This calls the showthread.php file with the thread ID of 2114175 and the Post ID of 15894210. The thread ID is then likely to contain data on it's record of the associated forum and category data in order to fill in the breadcrumb you see at the top of the site here;

    Forum --> Design & Development --> Programming --> PHP --> Updating a boolean value in a database when a link is clicked

    So you will need to place some unique ID for any and everything that you would call from the database. I would suggest you Google "Database Normalisation" and try to get your data down to Third Normal Form. This should allow you to design your database correctly and gain access to all the information you need.
     
    awood969, Mar 7, 2011 IP
  5. TOSCS

    TOSCS Member

    Messages:
    79
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #5
    I only have one table
    `id`, `title`, `votes_up`, `votes_down`, `flagged`
     
    TOSCS, Mar 7, 2011 IP
  6. awood969

    awood969 Member

    Messages:
    186
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    40
    #6
    then the ID field would be its unique identifier. Use this to find the correct record. Ensure that this record ID is stored as a PHP variable somewhere.
     
    awood969, Mar 7, 2011 IP
  7. TOSCS

    TOSCS Member

    Messages:
    79
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #7
    i think I have it

    
    $id = $row['id'];
    $report = "UPDATE jcge SET flagged='true' WHERE id='$id'";
    PHP:
    How do I make that query run when the hyperlink is clicked?
     
    TOSCS, Mar 7, 2011 IP
  8. awood969

    awood969 Member

    Messages:
    186
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    40
    #8
    You could either use AJAX (Although I don't know how to do it that way) or use PHP and reload the page with something like a page.php?flaggedpost=66 URI and then check;

     
    awood969, Mar 7, 2011 IP