SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast Mike Smith's Avatar
    Join Date
    Dec 2007
    Location
    Knoxville, TN
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    adding voting from radial selections to mysql

    Hey everyone,

    I have a question where people can vote one of three answers for a picture and I want to be able to have the database add to the row each time one of the answers is given.

    IE: if the person selects "yes" for the answer, I want the row for answer to show "1" and if someone else says "yes" then it would increase it to "2".

    How would I go about doing this? It sounds easy in my head but I can't get my head wrapped around it when looking at my screen.

    Jst so you can see everything. Here's the entire form section of my index.php file

    PHP Code:
        <form method="post" action="voted.php">
    <?php $query "SELECT id, name, url FROM photos ORDER BY RAND() LIMIT 3"$result mysql_query($query); while($row mysql_fetch_assoc($result)) { ?>
        <input type="hidden" name="Record" value="{$row['id']}">
             <div class="picturewrap">
            <img src="<?php {echo "{$row['url']}"; } ?>" alt="<?php {echo "{$row['name']}"; } ?>" />
            <div class="formwrapper">
                 <input type="radio" name="<?php {echo "{$row['id']}"; } ?>" value="Yes">
            </div>
            <div class="formwrapper">
                 <input type="radio" name="<?php {echo "{$row['id']}"; } ?>" value="Maybe">
            </div>
            <div class="formwrapper">
                 <input type="radio" name="<?php {echo "{$row['id']}"; } ?>" value="No">
            </div>
             </div>
    <? ?>
             <div class="submitform">
            <input type="image" src="images/button_submit.jpg" class="button_submit" value="Submit"><br/ >
             </div>
        </form>
    Thanks,
    Mike
    Last edited by Mike Smith; May 22, 2009 at 10:48.

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,220
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    is this related to your other thread? or is this a brand new problem?
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Enthusiast Mike Smith's Avatar
    Join Date
    Dec 2007
    Location
    Knoxville, TN
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Related to the other thread, but thought that keeping the questing simpler in this thread, it might be easier to follow.

    Here's some code I have:

    PHP Code:
    <input type="radio" name="<?php {echo "{$row['id']}"; } ?>" value="yes">
    That is one of the radio buttons. The row id pulls the picture's id so it can register the value="yes" to it (as they're all in the same row.

    I have yet to figure out how to have the form submission file (voted.php) check these items above and increase the column for "yes" by one number.

    Also, the pictures will be shown randomly numerous times so I need the visitor to be able to vote on the same pic multiple times (although I did keep a record of your code in the previous thread in case I needed it in the future).

  4. #4
    SitePoint Enthusiast Mike Smith's Avatar
    Join Date
    Dec 2007
    Location
    Knoxville, TN
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just so you can see everything. Here's the entire form section of my index.php file

    PHP Code:
        <form method="post" action="voted.php">
    <?php $query "SELECT id, name, url FROM photos ORDER BY RAND() LIMIT 3"$result mysql_query($query); while($row mysql_fetch_assoc($result)) { ?>
        <input type="hidden" name="Record" value="{$row['id']}">
             <div class="picturewrap">
            <img src="<?php {echo "{$row['url']}"; } ?>" alt="<?php {echo "{$row['name']}"; } ?>" />
            <div class="formwrapper">
                 <input type="radio" name="<?php {echo "{$row['id']}"; } ?>" value="Yes">
            </div>
            <div class="formwrapper">
                 <input type="radio" name="<?php {echo "{$row['id']}"; } ?>" value="Maybe">
            </div>
            <div class="formwrapper">
                 <input type="radio" name="<?php {echo "{$row['id']}"; } ?>" value="No">
            </div>
             </div>
    <? ?>
             <div class="submitform">
            <input type="image" src="images/button_submit.jpg" class="button_submit" value="Submit"><br/ >
             </div>
        </form>

  5. #5
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,220
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    won't help me, as i don't do php, but i can move this thread to the php forum for you
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  6. #6
    SitePoint Enthusiast Mike Smith's Avatar
    Join Date
    Dec 2007
    Location
    Knoxville, TN
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks. i appreciate it

  7. #7
    SitePoint Enthusiast Mike Smith's Avatar
    Join Date
    Dec 2007
    Location
    Knoxville, TN
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am now currently at this point in the voted.php file

    PHP Code:
    <?PHP
    $con 
    mysql_connect("localhost","username","password");
    if (!
    $con)
      {
      die(
    'Could not connect: ' mysql_error());
      }

    mysql_select_db("database"$con);

    $sql="INSERT INTO photos (id, spoon, fork, knife)
    VALUES
    ('
    $_POST[id]','$_POST[spoon]','$_POST[fork]','$_POST[knife]')";

    if (!
    mysql_query($sql,$con))
      {
      die(
    'Error: ' mysql_error());
      }
    echo 
    "1 record added";

    ?>
    I need the INSERT to happen the ROW (which is passed in the hidden value on the index.php page) instead of just inserting a new row altogether

  8. #8
    SitePoint Enthusiast Mike Smith's Avatar
    Join Date
    Dec 2007
    Location
    Knoxville, TN
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    UPDATE: This is where I'm at now.

    PHP Code:
    <?PHP
    $con 
    mysql_connect("localhost","user","pass2");
    if (!
    $con)
      {
      die(
    'Could not connect: ' mysql_error());
      }


    mysql_select_db("database"$con);

    $sql="UPDATE photos
    SET yes = yes + 1
    WHERE id = '
    $_POST[id]'";

    if (!
    mysql_query($sql,$con))
      {
      die(
    'Error: ' mysql_error());
      }
    echo 
    "hooray";

    ?>
    my problem now is getting the SET yes = yes + 1 to become an if/else because I have three different options from three different pictures (ie: one picture's voted as "yes" while the other is voted as "no" and the third is voted as "maybe".

    The code above is working if the person selects the "yes" option, but the other two pictures aren't being updated. Any idea on how to fix this?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •