rnorr002 Posted June 15, 2010 Share Posted June 15, 2010 hey i am pretty new to PHP. I am working on creating a secure login voting site for a school system that will allow teachers to login with a user name i will provide and cast votes for different positions. I have gotten the secure login portion of the site done but i cant seem to figure out how to update the sql with the data selected by the user and how to ensure each person gets to vote only once for the positions listed. I am new to PHP like i said but know Javascript, HTML, and SQL. any help or pointers would be greatly appreciated thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/204897-creating-a-voting-site/ Share on other sites More sharing options...
ngreenwood6 Posted June 15, 2010 Share Posted June 15, 2010 Thats actually pretty simple. I assume you are creating your users in the database with a unique id. What you can do is when the form that you create for the voting is posted you can then insert the user that is logged in user id and vote into a new table storing there user_id and there selection. Then when you display the form if the users id is in the new table you will just display something saying that they have already voted. Quote Link to comment https://forums.phpfreaks.com/topic/204897-creating-a-voting-site/#findComment-1072684 Share on other sites More sharing options...
Soldier Jane Posted June 15, 2010 Share Posted June 15, 2010 This site is great for beginners:  http://www.w3schools.com/PHP/php_mysql_intro.asp  An easy way to ensure that each person can only vote once is by adding a has_voted property to your users. However, if you intend to do multiple polls/surveys then perhaps it would be better to log user names or IDs along the voting data for each candidate/answer and check whether they have already voted before logging the vote or even displaying the form. Just like ngreenwood6 said really. Quote Link to comment https://forums.phpfreaks.com/topic/204897-creating-a-voting-site/#findComment-1072685 Share on other sites More sharing options...
rnorr002 Posted June 17, 2010 Author Share Posted June 17, 2010 i am working on getting a basic knowledge of php here is the code i have for sending the data from the voting site to mysql. the problem here is that i am not getting any actual data from the radio buttons. when i select other and fill in the text i get it in the database but not the radio selections. any pointers in the right direction would be great thanks. the radio buttons are in four groups with an other option with text type in. Â <? $president=$_POST['Selection1']; $vp=$_POST['RadioGroup2']; $tr=$_POST['RadioGroup3']; $sect=$_POST['RadioGroup1']; mysql_connect("website", "username", "passwrod") or die(mysql_error()); mysql_select_db("membership") or die(mysql_error()); mysql_query("INSERT INTO `data` VALUES ('$president', '$vp', '$tr', '$sect')"); Print "Your information has been successfully added to the database."; ?> Â Quote Link to comment https://forums.phpfreaks.com/topic/204897-creating-a-voting-site/#findComment-1073226 Share on other sites More sharing options...
jcbones Posted June 17, 2010 Share Posted June 17, 2010 I recently helped on a project that is exactly what you are trying to do. A school voting site. Here are the things that you need to consider.  1. You need to have the option of dividing up the classes into groups. ie. 8th grade, 9th grade. This is because some will use it for individual grades. 2. You need to make it flexible, so that schools can also use it for other things such as: voting for banners, shirts, uni-forms, etc. 3. You should make it where you can upload images (of the subject, or person) as well as fill in a short description/biography. 4. You should make the voting tied to the user's/ students ID, so they can only vote once per election/poll. 5. You may want to give the user an option of several different layouts. 6. You may want to give the user the option of showing the results after a vote, or waiting until the voting time limit is over.  There is much more, but this was all things that the customers came back and asked to be added in.  As for your code, there are a couple of things you could add to it for debugging purposes. <?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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/204897-creating-a-voting-site/#findComment-1073233 Share on other sites More sharing options...
rnorr002 Posted June 20, 2010 Author Share Posted June 20, 2010 still cant seem to get the data to pass from the radio buttons to the actual mysql. it populates the database with an empty file. here is the html along with the php. Â <form id="form1" name="form1" method="post" action="process.php"> Â <p>President</p> Â <p> Â Â <label> Â Â Â <input type="radio" name="president" value="field11" id="Selection1_0" /> Me</label> Â Â <br /> Â Â <label> Â Â Â <input type="radio" name="president" value="field12" id="Selection1_1" /> You</label> Â Â <br /> Â Â <label> Â Â Â <input type="radio" name="president" value="field13" id="Selection1_2" /> Other </label> Â Â <label> Â Â <input type="text" name="president" id="Selection1_3" value="" size="25" /> Â </p> Â <p> Â Â </label> Â </p> <p>VicePresident</p> Â <p> Â Â <label> Â Â <input type="radio" name="vp" value="field21" id="RadioGroup2_0" /> John</label> Â Â <br /> Â Â <label> Â Â <input type="radio" name="vp" value="field22" id="RadioGroup2_1" /> Marty</label> Â Â <br /> Â Â <label> Â Â <input type="radio" name="vp" value="field23" id="RadioGroup2_2" /> Other</label> Â Â <input type="text" name="vp" id="RadioGroup2-3" value="" size="25" /> Â Â </label> Â </p> Â <p>Â </p> <p>Treasurer</p> Â <p> Â Â <label> Â Â <input type="radio" name="tr" value="field31" id="RadioGroup3_0" /> Bob</label> Â Â <br /> Â Â <label> Â Â <input type="radio" name="tr" value="field32" id="RadioGroup3_1" /> Rob</label> Â Â <br /> Â Â <label> Â Â <input type="radio" name="tr" value="field33" id="RadioGroup3_2" /> Other</label> Â Â <input type="text" name="tr" id="RadioGroup3_3" value="" size="25" /> Â Â </label> Â </p> <p>Sectretary</p> Â <p> Â Â <label> Â Â <input type="radio" name="sect" value="field41" id="RadioGroup1_0" /> Richard</label> Â Â <br /> Â Â <label> Â Â <input type="radio" name="sect" value="field42" id="RadioGroup1_1" /> Bella</label> Â Â <br /> Â Â <label> Â Â <input type="radio" name="sect" value="field43" id="RadioGroup1_2" /> Other</label> Â Â <label> Â Â <input type="text" name="sect" id="RadioGroup1_3" value="" size="25" /> Â Â </label> Â </p> Â <p> Â Â <label> Â Â <input type="reset" name="reset" id="reset" value="Reset" /> Â Â <input type="submit" name="Submit" id="Submit" value="Submit" /> Â Â </label> Â Â <br /> Â </p> </form> Â Â <?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); Â $president=$_POST['field11']; $vp=$_POST['field21']; $tr=$_POST['field31']; $sect=$_POST['field41']; mysql_connect("localhost", "root", "kungfu") or die(mysql_error()); mysql_select_db("membership") or die(mysql_error()); mysql_query("INSERT INTO `data` VALUES ('$president', '$vp', '$tr', '$sect')"); Print "Your information has been successfully added to the database."; ?> Â i know the php is not complete and i am missing something i just dont know what. Â Â Quote Link to comment https://forums.phpfreaks.com/topic/204897-creating-a-voting-site/#findComment-1074510 Share on other sites More sharing options...
rnorr002 Posted June 20, 2010 Author Share Posted June 20, 2010 sorry this is the correct php file. Â <?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); Â $president=($_POST['president']); $vp=($_POST['vp']); $tr=($_POST['tr']); $sect=($_POST['sect']); mysql_connect("localhost", "root", "kungfu") or die(mysql_error()); mysql_select_db("membership") or die(mysql_error()); mysql_query("INSERT INTO `data` VALUES ('$president', '$vp', '$tr', '$sect')"); Print "Your information has been successfully added to the database."; ?> Quote Link to comment https://forums.phpfreaks.com/topic/204897-creating-a-voting-site/#findComment-1074516 Share on other sites More sharing options...
rnorr002 Posted June 20, 2010 Author Share Posted June 20, 2010 :'( no helpful input? Quote Link to comment https://forums.phpfreaks.com/topic/204897-creating-a-voting-site/#findComment-1074678 Share on other sites More sharing options...
rnorr002 Posted June 21, 2010 Author Share Posted June 21, 2010 ok so i figured it out with some help on a separate site but figured i would post the answer anyways so if anyone else has an issue like this they can figure it out. the problem was the text boxes. they were named the same as the radio button and this caused an override of the radio buttons. the fix was to rename the text boxes and access them using an if statement. Quote Link to comment https://forums.phpfreaks.com/topic/204897-creating-a-voting-site/#findComment-1075278 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.