PHP-SQL interactive voting script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • caveman
    New Member
    • Feb 2007
    • 6

    PHP-SQL interactive voting script

    Hi i am trying to create an interactive PHP-Mysql voting system, so that users can input their 'favourite colour' for example in a html submit box, the php would then access the database and search the records for the input color, and if it finds it would increase the vote count by 1 in the 'votes' column for that colour, but if it doesnt find it it will add a row to the table with 'votes' value 1.
    Does anyone know how to do this or where to find a tutorial?
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    I wonder if there is any tutorial on this. It is not that difficult to do.

    It is a plain php-mysql solution and it cannot be hard to develop for a php programmer. Since you have already laid out how it should work and you also stated that you are already trying to create this solution, start coding it.

    When you have problems while developing we will help you.

    Ronald :cool:

    Comment

    • caveman
      New Member
      • Feb 2007
      • 6

      #3
      Hi thanks for replying, i managed to find a registration script, which checks or updates the database and have adapted it but there are still a few mistakes in it that i cant iron out. Can you help?
      [php]
      <html>
      <head>
      <title>Untitled </title>
      </head>

      <body>
      <?php
      include 'll/connect.php';
      include 'll/database.php';

      if (isset($_POST['submit'])) {


      if (!get_magic_quo tes_gpc()) {
      $_POST['colour'] = addslashes($_PO ST['colour']);
      }
      $usercheck = $_POST['colour'];
      $check = mysql_query("SE LECT colour FROM favourites WHERE colour = '$usercheck'")
      or die(mysql_error ());
      $check2 = mysql_num_rows( $check);

      //if the colour exists add vote
      if ($check2 != 0)
      mysql_query("UP DATE favourites SET votes = 'votes+1'")
      {
      echo('your vote is counted!!');
      }

      // if it does not exist, add new row and 1 vote
      $insert = "INSERT INTO favourites (colour, votes)
      VALUES ('".$_POST['name']."', '1')";
      $add_member = mysql_query($in sert);
      ?>

      <?php
      }
      else
      {
      ?>
      <form action="<?php echo $_SERVER['PHP_SELF']; ?>"

      method="post">
      <table border="0">
      <tr><td>name: </td><td>
      <input type="text" name="colour" maxlength="60">
      </td></tr>
      <tr><th colspan=2><inpu t type="submit" name="submit"

      value="vote!"></th></tr> </table>
      </form>

      <?php
      }
      ?>

      </body>
      </html>[/php]
      Last edited by ronverdonk; Feb 17 '07, 08:17 PM. Reason: code within tags

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        Please read the Posting Guidelines before you post in this forum!
        Especially the part about enclosing code within tags!

        moderator

        Comment

        • ronverdonk
          Recognized Expert Specialist
          • Jul 2006
          • 4259

          #5
          i managed to find a registration script, which checks or updates the database and have adapted it but there are still a few mistakes in it that i cant iron out
          So.... what are these mistakes?

          Ronald :cool:

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            Hi.

            As Ronald says, it helps to have the error details.

            I did however notice a couple of things in this part

            [PHP]
            //if the colour exists add vote
            if ($check2 != 0)
            mysql_query("UP DATE favourites SET votes = 'votes+1'")
            {
            echo('your vote is counted!!');
            }

            // if it does not exist, add new row and 1 vote
            $insert = "INSERT INTO favourites (colour, votes)
            VALUES ('".$_POST['name']."', '1')";
            $add_member = mysql_query($in sert);
            ?>
            [/PHP]

            First, when you insert or update a number in MySQL, you should leave out the ' chars:
            [php]
            $sql = "update mytable set num = 'num + 1'";
            // should be
            $sql = "update mytable set num = num + 1";
            [/php]

            Second, you didnt finish your update query. It needs to know what rows to update. As it is, it adds 1 to all the rows in you table.
            [php]
            mysql_query("UP DATE favourites SET votes = 'votes+1'")
            // should be
            mysql_query("UP DATE favourites SET votes = votes+1 WHERE colour = '". $_POST['name'] ."'");
            [/php]


            Lastly, you seeme have to left out the else there. I mean as it is, it will insert a new row for the color everytime you run the code, even if it is already in there.

            This is how I see it atm
            [php]
            if(/*color exists */)
            {
            // Add 1 to the count
            }

            // Add the new color
            [/php]

            This is how I think it should be
            [php]
            if(/*color exists */)
            {
            // Add 1 to the count
            }
            else // <---
            {
            // Add the new color
            }

            [/php]

            Comment

            • caveman
              New Member
              • Feb 2007
              • 6

              #7
              Thats brilliant, thanks, the script works now.

              Comment

              Working...