Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New Coder
    Join Date
    Jun 2010
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    using mysql and php to track user ids for who voted

    hey guys i am am working on a site that allows users to log in and vote for positions. the login portion is completed as is the portion for voting. the problem is now i need to make sure each user id is posted with their vote and they can only vote once. i have two tables set up one for users which contains the id of each user and a data table which contains the positions being voted on. i am crazy new to php and am tryig to learn as i go. i dont know where the code should be inserted and what the exact syntax should be. here is the login portion of the code
    PHP Code:
    <?php
    session_start
    ();
    require_once 
    'classes/Membership.php';
    $membership = new Membership();

    // If the user clicks the "Log Out" link on the index page.
    if(isset($_GET['status']) && $_GET['status'] == 'loggedout') {
        
    $membership->log_User_Out();
    }

    // Did the user enter a password/username and click submit?
    if($_POST && !empty($_POST['username']) && !empty($_POST['pwd'])) {
        
    $response $membership->validate_User($_POST['username'], $_POST['pwd']);
    }
                                                            

    ?>


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>LOGIN to access HCEA voting</title>
    <link rel="stylesheet" type="text/css" href="css/default.css" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js"></script>
    <script type="text/javascript" src="js/main.js"></script>
    </head>

    <body>
    <div id="login">
        <form method="post" action="">
            <h2>Login <small>enter your credentials</small></h2>
            <p>
                <label for="name">Username: </label>
                <input type="text" name="username" />
            </p>
            
            <p>
                <label for="pwd">Password: </label>
                <input type="password" name="pwd" />
            </p>
            
            <p>
                <input type="submit" id="submit" value="Login" name="submit" />
            </p>
        </form>
        <?php if(isset($response)) echo "<h4 class='alert'>" $response "</h4>"?>
    </div><!--end login-->
    </body>
    </html>
    and the process

    PHP Code:
    <?php
     
    //show all $_POST variables the page is receiving.
     
    echo '<pre>Debugging:'print_r($_POST); echo '</pre>';
     
    reset($_POST); //print_r() moves the array pointer to the last value, this resets it to number one.
     //OR,
     
    var_dump($_POST); 
     
     
    mysql_connect("localhost""root""kungfu") or die(mysql_error()); 
     
    mysql_select_db("membership") or die(mysql_error()); 
     
     
     
    $president=($_POST['president']);
     
    $vp=($_POST['vp']);
     
    $tr=($_POST['tr']);
     
    $sect=($_POST['sect']);
     
     if (
    $president=='other') {
        
    $president=($_POST['pres_txt']);
    }
     if (
    $vp=='other') {
        
    $vp=($_POST['vp_txt']);
    }
     if (
    $tr=='other') {
        
    $tr=($_POST['tr_txt']);
    }
     if (
    $sect=='other') {
        
    $sect=($_POST['sect_txt']);
    }

     
    mysql_query ("INSERT INTO `data` VALUES ('$president', '$vp', '$tr', '$sect')"); 
     echo 
    $query;
      Print 
    "Your vote has been successfully cast."
     
    ?>

  • #2
    New Coder
    Join Date
    Jun 2010
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i am thinking i will need a third table called temp which when the user logs in will put their user id in it... but am not certain how to do this or if there is an easier way...

  • #3
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,535
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Well, to keep track of who has voted, add another col to the "data" table in the second code set where you store the userID. If you wanna see if a user has voted, check if they're in the data table.

  • #4
    New Coder
    Join Date
    Jun 2010
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    how do i update the data table with the user id when the user logs on?

  • #5
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,535
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Oh, you wanna know when the user logs on? Each time they log in, or the last time they logged in?

    If every time, you'll need a separate table that keeps track of that (store userID and datestamp) and if last, just add a "lastLoggedIn" col to the user table.

  • #6
    New Coder
    Join Date
    Jun 2010
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    first thanks for helping me. second the plan is to only allow one set of votes per user. the user id next to their vote is to prove that everyone only voted once without showing the usersname that placed the vote. i am doing this site for a small teachers union who vote once or twice a year. i figured it would be easy but i am seeing it is more complicated than i first thought. thanks again for the help.

  • #7
    New Coder
    Join Date
    Jun 2010
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    frrre
    Last edited by rnorr002; 06-25-2010 at 01:11 AM.

  • #8
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,535
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Sorry, missed this post, do you still need help with something?

  • #9
    New Coder
    Join Date
    Jun 2010
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yes actually. i have the login classes and i cant figure out where i would put a statement to save the user name to a table named temp. here is the code
    PHP Code:
    <?php

    require_once 'includes/constants.php';

    class 
    Mysql {
        private 
    $conn;
        
        function 
    __construct() {
            
    $this->conn = new mysqli(DB_SERVERDB_USERDB_PASSWORDDB_NAME) or 
                          die(
    'There was a problem connecting to the database.');
        }
        
        function 
    verify_Username_and_Pass($un$pwd) {
            echo (
    $un);         
            
    $query "SELECT *
                    FROM users
                    WHERE username = ? AND password = ?
                    LIMIT 1"
    ;
                                   
            if(
    $stmt $this->conn->prepare($query)) {
                
    $stmt->bind_param('ss'$un$pwd);
                
    $stmt->execute();
                 
                if(
    $stmt->fetch()) {
                    
    $stmt->close();
                    return 
    true;
                }
            }
        
        }
    }

  • #10
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,535
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Save the username? For what?

    Also, given what you're trying to do, making objects might be needless work, but regardless... Are you saving the last login? What is structure for temp?

  • #11
    New Coder
    Join Date
    Jun 2010
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i volunteered to deign a site that would allow only members to login. once logged in the users would then be able to vote on positions such as president vicepresident ect. on clicking submit this data would be sent to the mysql. all of this works fine. the problem i am now running into is that they want each user to only vote once and each user's id which is stored in the users table needs to be attached to the vote they submit which is stored in the data table. my thought which this might be making it way harder than it needs to be, is to send the users name to a temp table which is cleared when after every vote. the temp table will have a foreign key referencing the id in the users table.

  • #12
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,535
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Well, you said they wanna know who voted for what, right? Just attach the user ID to the vote table and to see if someone voted, check if their userID is there. There's no need for a temp table... in fact, that won't prove anything other then they logged in.

  • #13
    New Coder
    Join Date
    Jun 2010
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok thanks for the input.


  •  

    Posting Permissions

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