Web Analytics Made Easy -
StatCounter Trouble looping and submitting forms into database - CodingForum

Announcement

Collapse
No announcement yet.

Trouble looping and submitting forms into database

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Trouble looping and submitting forms into database

    I have this voting thing (The medal voting for all who already know) that I'm doing. (taking forever because I'm learning). But, anyway...

    I have a loop that makes Radio Buttons. With "Yes" & "No" buttons on each. For each Thing to vote for...
    (I took out unneeded info)


    PHP:
    --------------------------------------------------------------------------------

    <?php
    $all_votes_sql = "SELECT voter, original, originator, poll_end, member, medal FROM votes WHERE original > '0'";
    $all_votes_result = mysql_query($all_votes_sql);

    if(mysql_error())
    {
    die("There Was An Error Loading The Current Polls List");
    }
    else{
    $num=0;
    while($row = mysql_fetch_array($all_votes_result))
    {
    $num++;

    echo '<td bgcolor="#e1e1e1">';
    echo '<input type="hidden" name="voter_sub_'.$num.'" value="' . $username . '">';
    echo '<input type="hidden" name="member_sub_'.$num.'" value="' . $row[member] . '">';
    echo '<input type="hidden" name="medal_sub_'.$num.'" value="' . $row[medal] . '">';
    echo '<input type="radio" NAME="vote_sub_'.$num.'" VALUE="1" class="textinput">Yes<br />';
    echo '<input type="radio" NAME="vote_sub_'.$num.'" VALUE="0" class="textinput">No</td>';
    $button = true;

    }

    }

    ?>

    --------------------------------------------------------------------------------


    Anyway...
    I tried to loop it with each input type having a different "id number". If you noticed so you can identify each seperate vote (I hope that is the right way). Anyway, the page reloads and I need a script that can Identify each vote and put the stuff needed for each inside the "votes" table.

    This is what I attempted to do...
    Do I need to loop the REQUEST, the MySQL, and the submission and stuff?
    I didn't bother, because I never did it and don't know what to loop.
    [php]
    <?
    if ($REQUEST_METHOD == 'POST') {

    $voter_sub = $_REQUEST['voter_sub'] ;
    $member_sub = $_REQUEST['member_sub'] ;
    $medal_sub = $_REQUEST['medal_sub'] ;
    $vote_sub = $_REQUEST['vote_sub'] ;


    if(isset($voter_sub) and isset($member_sub) and isset($medal_sub) and isset($vote_sub) and empty($the_voter) and empty($voted_member) and empty($voted_medal))
    {
    $check_voter_sql = "SELECT voter, member, medal FROM votes WHERE member='$member_sub' and medal='$medal_sub'";
    $check_voter_sql_queried = mysql_query($check_voter_sql);
    $check_voter_data_array = mysql_fetch_array($check_voter_sql_queried);

    if (mysql_error()) {
    print('There Was An Error Submitting Your Vote');
    }
    else{
    if($check_voter_data_array[voter] == $voter_sub){
    print('You already voted for this one.');
    }
    else
    {
    $create_vote_sql = "INSERT INTO votes (original, voter, member, medal, vote) VALUES('0', '$voter_sub', '$member_sub', '$medal_sub', '$vote_sub')";
    mysql_query($create_vote_sql);
    }
    }
    }
    else
    {
    header( "Location: error.html" );
    }
    }

    }

    ?>

  • #2
    Please enclose php-code inside [ p h p ] and [ / p h p ] tags (without the spaces)

    I also don't understand the logic in your script. What are you selectiing when you build the form ? You don't need all the values you are selecting + you should not be identifying the user based on a posted hidden field. Use a session-variable for that.
    And you could use the medale-id or name as the name for your radiobuttons. Like
    PHP Code:
    while($row mysql_fetch_assoc($all_votes_result)){
      echo 
    '<td bgcolor="#e1e1e1">'$row[medal] . '">';
      echo 
    '<input type="radio" name="'$row['medal'] .'" VALUE="1" class="textinput">Yes<br />';
      echo 
    '<input type="radio" NAME="'$row['medal'] .'" VALUE="0" class="textinput">No</td>';
      
    $button true;

    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

    Comment


    • #3
      So correct me if I'm wrong, but you're having the difficulty on the server side, after the post.

      I think you should use arrays when creating your form:
      PHP Code:
      <?php 
      $all_votes_sql 
      "SELECT voter, original, originator, poll_end, member, medal FROM votes WHERE original > '0'"
      $all_votes_result mysql_query($all_votes_sql); 

      if(
      mysql_error()) 

      die(
      "There Was An Error Loading The Current Polls List"); 

      else{ 
      $num=0
      while(
      $row mysql_fetch_array($all_votes_result)) 

      $num++; 

      echo 
      '<td bgcolor="#e1e1e1">'
      echo 
      '<input type="hidden" name="voter_sub['.$num.]'" value="' $username '">'
      echo 
      '<input type="hidden" name="member_sub['.$num.']" value="' $row[member] . '">'
      echo 
      '<input type="hidden" name="medal_sub['.$num.']" value="' $row[medal] . '">'
      echo 
      '<input type="radio" NAME="vote_sub['.$num.']" VALUE="1" class="textinput">Yes<br />'
      echo 
      '<input type="radio" NAME="vote_sub['.$num.']" VALUE="0" class="textinput">No</td>'
      $button true





      ?>
      So based on your code, this is the modification I've thought to make. It's hard to test it, but hopefully you can debug it and get some ideas from it:
      PHP Code:
      <?php
      if ($REQUEST_METHOD == 'POST') {
       
      extract($_POST);
       foreach(
      $voter_sub as $i => $v) { 
        if(isset(
      $voter_sub[$i]) && isset($member_sub[$i]) && isset($medal_sub[$i]) && isset($vote_sub[$i]) && empty($the_voter) && empty($voted_member) && empty($voted_medal)) {
         
      $check_voter_sql "SELECT voter, member, medal FROM votes WHERE member='$member_sub[$i]' and medal='$medal_sub[$i]'";
         
      $check_voter_sql_queried mysql_query($check_voter_sql);
         
      $check_voter_data_array mysql_fetch_array($check_voter_sql_queried);

         if (
      mysql_error()) { 
          print(
      'There Was An Error Submitting Your Vote'); 
         }
         else{
          if(
      $check_voter_data_array[voter] == $voter_sub[$i]){
           print(
      'You already voted for this one.');
          }
          else {
           
      $create_vote_sql "INSERT INTO votes (original, voter, member, medal, vote) VALUES('0', '$voter_sub[$i]', '$member_sub[$i]', '$medal_sub[$i]', '$vote_sub[$i]')";
           
      mysql_query($create_vote_sql);
          }
         }
        }
        else {
         
      header"Location: error.html" );
        }
       } 
      }
      ?>
      That should do it if your previously submitted code was correct. Now it should just be a debugging issue.

      Let me know if I've made any horrible errors!

      Hope that helps,
      Sadiq.

      Comment


      • #4
        Ok, since I'm so confused I'll show you the script I've been working on and What it does. Well, first of all the user is logged in with a Session to see the script anyway (thankfully). Ok, What this is, is a "Medal Voting" Script for my clan. There is a page before this that submits/creates a new vote. I am using the same MySQL table for Created Votes and Votes towards created votes. It displays all the current "Started" votes for a member to get a medal. If they already voted or the pole is Expired, they will not get the radio buttons. But, the problem is I want to make it so you can add a new vote with the radio buttons. And submit it on the same page.

        Basically... I need to get the info of the user and the "yes" or "No" from the radio buttons into the database with...
        $create_vote_sql = "INSERT INTO votes (original, voter, member, medal, vote) VALUES ('0', '$voter_sub', '$member_sub', '$medal_sub', '$vote_sub')";
        mysql_query($create_vote_sql);
        Then, I'll figure the rest out from there.

        Hope you guys get it. This is the whole page (besides the one that creates a totally new vote)...
        PHP Code:
        <?php 
        session_start
        (); 
        header("Cache-control: private"); //IE 6 Fix 

        if($_SESSION['user']){ ?> 
        <html>
        <head>
        <title>Forgotten Wraith's</title>
        <link rel="stylesheet" href="../css/main.css" type="text/css" />
        <script language="javascript" src="../js/main.js"></script>

        </head>
        <body marginwidth="0" marginheight="0" style="margin:0;">
        <table cellspacing="0" cellpadding="0" border="0" width="100%">
        <tr align="center" valign="top">
        <td align="left" valign="top">

        <?php
        $link 
        mysql_connect ("localhost","punkpuke","**************") or die ("Error: No connection to MySQL server\n"); 
        $database mysql_select_db("forgottenwraiths_com_main") or die ("Error: No Database Connection. \n");

        if (
        $REQUEST_METHOD == 'POST') {

          
        $the_voter $_REQUEST['voter'] ;
          
        $voted_member $_REQUEST['member'] ;
          
        $voted_medal $_REQUEST['medal'] ;


            
        $voter_sub $_REQUEST['voter_sub'] ;
            
        $member_sub $_REQUEST['member_sub'] ;
            
        $medal_sub $_REQUEST['medal_sub'] ;
            
        $vote_sub $_REQUEST['vote_sub'] ;

            if(isset(
        $the_voter) and isset($voted_member) and isset($voted_medal) and empty($voter_sub) and empty($member_sub) and empty($medal_sub) and empty($vote_sub))
            {

            
        $date date("Ymd");
            
            
        $sql_date $date;
            
            
        $pollStart = array(); 
            
        $pollStart[0] = substr($sql_date04); 
            
        $pollStart[1] = substr($sql_date42); 
            
        $pollStart[2] = substr($sql_date62);
            
            
        //second, an array that will hold year, month, and day for the ending date 
            
        $pollEnd = array($pollStart[0], $pollStart[1], $pollStart[2]); 
            
            
        //number of days in the month 
            
        $monthLength date("t"); 
            
            
        //if the 7 days spills over into the next month 
            
        if(($pollStart[2] + 7) > $monthLength
            { 
                   
        //if 7 days spill into the next month and the month is Dec., add a year 
                   
        if($pollStart[1] == 12
                   { 
                           
        $pollEnd[0] = $pollStart[0] + 1
                   } 
                    
                   
        //increment month 
                   
        $pollEnd[1] = $pollStart[1] + 1
                   
        //get ending day (in the next month) 
                   
        $pollEnd[2] = - ($monthLength $pollStart[2]); 
            } 
            
            
        //add leading zeros to month and day if necessary 
            
        if(strlen($pollEnd[1]) < 2
            { 
               
        $pollEnd[1] = "0".$pollEnd[1]; 
            } 
            if(
        strlen($pollEnd[2]) < 2
            { 
                   
        $pollEnd[2] = "0".$pollEnd[2]; 
            } 

            
        $end_date $pollEnd[0].$pollEnd[1].$pollEnd[2];    

            
        $check_voter_sql "SELECT COUNT(*) AS vote_count FROM votes WHERE original='1' and originator='$the_voter'"
            
        $check_voter_sql_queried mysql_query($check_voter_sql);
                
        $check_voter_data mysql_fetch_array($check_voter_sql_queried); 

            if (
        mysql_error()) { 
            print(
        'There Was An Error Submitting Your Vote'); 
            }
            else
            { 
               if(
        $check_voter_data["vote_count"] >= 2
               { 
                  print(
        'You cannot have more than 2 votes you started running at the same time.'); 
               }
               else
               {
                
        $check_voter_sql "SELECT voter, member, medal FROM votes WHERE member='$voted_member' and medal='$voted_medal'";
                
        $check_voter_sql_queried mysql_query($check_voter_sql);
                
        $check_voter_data_array mysql_fetch_array($check_voter_sql_queried);
                if(
        $check_voter_data_array[voter] == $the_voter){
                print(
        'You cannot create a vote just like one you\'ve already started.');
                    }
                else
                {
                    
        $check_voter_sql "SELECT member, medal FROM votes WHERE member='$voted_member' and medal='$voted_medal'";
                    
        $check_voter_sql_queried mysql_query($check_voter_sql);
                    
        $check_voter_data_array mysql_fetch_array($check_voter_sql_queried);
                    if(
        $check_voter_data_array[member] == $voted_member and $check_voter_data_array[medal] == $voted_medal){
                    print(
        'You cannot create a vote just like one that has already been started.');
                    }
                    else
                    {
                    
        $create_vote_sql "INSERT INTO votes (original, originator, date, poll_end, voter, member, medal, vote) VALUES ('1', '$the_voter', '$date', '$end_date', '$the_voter', '$voted_member', '$voted_medal', '1')";
                    
        mysql_query($create_vote_sql);
                    }
                }
               } 
            }
            }
            else
            {
                if(isset(
        $voter_sub) and isset($member_sub) and isset($medal_sub) and isset($vote_sub) and empty($the_voter) and empty($voted_member) and empty($voted_medal))
                {
                
        $check_voter_sql "SELECT voter, member, medal FROM votes WHERE member='$member_sub' and medal='$medal_sub'";
                
        $check_voter_sql_queried mysql_query($check_voter_sql);
                
        $check_voter_data_array mysql_fetch_array($check_voter_sql_queried);

                    if (
        mysql_error()) { 
                    print(
        'There Was An Error Submitting Your Vote'); 
                    }
                    else{
                     if(
        $check_voter_data_array[voter] == $voter_sub){
                     print(
        'You already voted for this one.');
                     }
                     else
                     {
                     
        $create_vote_sql "INSERT INTO votes (original, voter, member, medal, vote) VALUES ('0', '$voter_sub', '$member_sub', '$medal_sub', '$vote_sub')";
                     
        mysql_query($create_vote_sql);
                     }
                    }
                }
                else
                {
                
        header"Location: error.html" );
                }
            }    

        }

        ?>

        <h2 align="center">Current Votes</h2>

        <p>This is the list of current votes</p>

        <form method="post" action="medalvotes.php">
        <table cellpadding="1" cellspacing="5" border="1" width="100%" class="rankblock">

        <?php
        $today 
        date("Ymd");
        $username strtolower($_SESSION['user']);

        $all_votes_sql "SELECT voter, original, originator, poll_end, member, medal FROM votes WHERE original > '0'"
        $all_votes_result mysql_query($all_votes_sql);

        if(
        mysql_error()) 

              die(
        "There Was An Error Loading The Current Polls List"); 
        }
        else{ 
        $num=0;
        while(
        $row mysql_fetch_array($all_votes_result)) 

            
        $num++;
            
        $sql_end_date $row['poll_end'];

            
        $endDate = array(); 
            
        $endDate[0] = substr($sql_end_date04); 
            
        $endDate[1] = substr($sql_end_date42); 
            
        $endDate[2] = substr($sql_end_date62);

              echo 
        "<tr><td bgcolor=\"#e1e1e1\"><p>Should ".$row[member]." receive The ".$row[medal]."?<br />Started By: ".$row["originator"].". Poll Ends: "$endDate[1] ."-"$endDate[2] ."-"$endDate[0] .".</p></td>";

                if(
        $row['poll_end'] <= $today){
                echo 
        '<td bgcolor="#e1e1e1">This pole has Officially Ended.</td>';
                }
                else{
                if(
        $row['voter'] == $username){
                echo 
        '<td bgcolor="#e1e1e1">You have already voted for this one.</td>';
                }
                else{
                echo 
        '<td bgcolor="#e1e1e1">';
                echo 
        '<input type="hidden" name="voter_sub_'.$num.'" value="' $username '">';
                echo 
        '<input type="hidden" name="member_sub_'.$num.'" value="' $row[member] . '">';
                echo 
        '<input type="hidden" name="medal_sub_'.$num.'" value="' $row[medal] . '">';
                echo 
        '<input type="radio" NAME="vote_sub_'.$num.'" VALUE="1" class="textinput">Yes<br />';
                echo 
        '<input type="radio" NAME="vote_sub_'.$num.'" VALUE="0" class="textinput">No</td>';
                
        $button true;
                }
                }
                
              echo 
        "</tr>"
        }



        ?>


        </table>

        <?php
        if($button == true){
        ?>
        <p align="center">
        <table>
        <tr>
        <td>
        <input type="submit" value="Submit" class="submit">
        </td>
        <td>
        <input type="reset" class="submit">
        </td>
        </table>
        </p>
        <?php
        }
        ?>
        </form>

        </td>
        </tr>
        </table>
        </body>
        </html>
        <?php

        else {   
        include(
        '../login/denied.php');

        ?>

        Comment


        • #5
          I'm not reading all that code...

          raf and I posted some feedback and some code already. I don't see what is wrong with our posts.

          Please advise what the problem is and why our solutions don't solve it. Then, if necessary, post the relevant sections (or tell us where to look now that you've posted everything) so that we can help you with those areas.

          If it is merely accessing all those fields from your form, then take a second look at my post because it does what you're looking for. If you require something else, just say so.

          Sadiq.

          Comment


          • #6
            ummm... sorry I was a bit spacy yesterday. I'll try it and see if I works when I get some time.

            Comment


            • #7
              I have trouble with that. Because I have some other stuff I request....

              PHP Code:
              <?
              if ($REQUEST_METHOD == 'POST') {

                
              $the_voter $_REQUEST['voter'] ;
                
              $voted_member $_REQUEST['member'] ;
                
              $voted_medal $_REQUEST['medal'] ;

              ?>
              It was giving a "cannot modify headers" thing. I couldn't figure.
              I tried putting extract post at the top part. Then, it said Foreach Loop command error.

              Then, I did something like this...
              PHP Code:
              <?php
              extract
              ($_POST);
              foreach(
              $voter_sub as $i => $v


              if(isset(
              $voter_sub[$i]) && isset($member_sub[$i]) && isset($medal_sub[$i]) && isset($vote_sub[$i]) and empty($the_voter) and empty($voted_member) and empty($voted_medal))
              {
              $check_voter_sql "SELECT voter, member, medal FROM votes WHERE member='$member_sub[$i]' and medal='$medal_sub[$i]'";
              $check_voter_sql_queried mysql_query($check_voter_sql);
              $check_voter_data_array mysql_fetch_array($check_voter_sql_queried);

              if (
              mysql_error()) { 
              print(
              'There Was An Error Submitting Your Vote'); 
              }
              else{
              if(
              $check_voter_data_array[voter] == $voter_sub[$i]){
              print(
              'You already voted for this one.');
              }
              else
              {
              $create_vote_sql "INSERT INTO votes (original, voter, member, medal, vote) VALUES('0', '$voter_sub[$i]', '$member_sub[$i]', '$medal_sub[$i]', '$vote_sub[$i]')";
              mysql_query($create_vote_sql);
              }
              }
              }
              else
              {
              header"Location: error.html" );
              }
              }
              ?>
              If you need to look at my script for reference.
              The First part checks if you are making a new poll or voting towards a vote. That's the other form values on a different page.

              First part checks for Post with code to make a new poll

              Second part checks for Post votes towards a current poll (working on this with you guys)
              Third part displays a table and I hope I got that settled on creating the radio buttons with the arrays correctly.

              It out putted something like this for each form...
              <input type="hidden" name="voter_sub_[4]" value="punkpuke">

              Comment


              • #8
                Sorry I just ate a lotta Chinese food for lunch... kinda sleepy here.

                I'm not sure that I see a question anywhere...

                Sadiq.

                Comment


                • #9
                  I have trouble with that. Because I have some other stuff I request....



                  PHP:--------------------------------------------------------------------------------

                  <?
                  if ($REQUEST_METHOD == 'POST') {

                  $the_voter = $_REQUEST['voter'] ;
                  $voted_member = $_REQUEST['member'] ;
                  $voted_medal = $_REQUEST['medal'] ;

                  ?>

                  --------------------------------------------------------------------------------


                  It was giving a "cannot modify headers" thing. I couldn't figure.
                  I tried putting extract post at the top part. Then, it said Foreach Loop command error?

                  Then, I did something like this...

                  PHP:--------------------------------------------------------------------------------

                  <?php
                  extract($_POST);
                  foreach($voter_sub as $i => $v)
                  {

                  if(isset($voter_sub[$i]) && isset($member_sub[$i]) && isset($medal_sub[$i]) && isset($vote_sub[$i]) and empty($the_voter) and empty($voted_member) and empty($voted_medal))
                  {
                  $check_voter_sql = "SELECT voter, member, medal FROM votes WHERE member='$member_sub[$i]' and medal='$medal_sub[$i]'";
                  $check_voter_sql_queried = mysql_query($check_voter_sql);
                  $check_voter_data_array = mysql_fetch_array($check_voter_sql_queried);

                  if (mysql_error()) {
                  print('There Was An Error Submitting Your Vote');
                  }
                  else{
                  if($check_voter_data_array[voter] == $voter_sub[$i]){
                  print('You already voted for this one.');
                  }
                  else
                  {
                  $create_vote_sql = "INSERT INTO votes (original, voter, member, medal, vote) VALUES('0', '$voter_sub[$i]', '$member_sub[$i]', '$medal_sub[$i]', '$vote_sub[$i]')";
                  mysql_query($create_vote_sql);
                  }
                  }
                  }
                  else
                  {
                  header( "Location: error.html" );
                  }
                  }
                  ?>

                  --------------------------------------------------------------------------------


                  If you need to look at my script for reference.
                  The First part checks if you are making a new poll or voting towards a vote. That's the other form values on a different page.

                  First part checks for Post with code to make a new poll

                  Second part checks for Post votes towards a current poll (working on this with you guys)
                  Third part displays a table and I hope I got that settled on creating the radio buttons with the arrays correctly?

                  It out putted something like this for each form...
                  <input type="hidden" name="voter_sub_[4]" value="punkpuke">

                  Comment

                  Working...
                  X