rghayes Posted May 6, 2013 Share Posted May 6, 2013 I am designing a voting webpage where in users can choose multiple values and all are submitted to a mysql database. I am using checkboxes. I can get one value to go in great, but its the mutiple value thing that has me pulling my hair out. Can someone PLEASE help a beginner out?? Quote Link to comment https://forums.phpfreaks.com/topic/277706-checkboxes/ Share on other sites More sharing options...
Barand Posted May 6, 2013 Share Posted May 6, 2013 put [] at end of checkbox names so they are posted as an array <input type="checkbox" name="myvote[]" value="1" /> <input type="checkbox" name="myvote[]" value="2" /> <input type="checkbox" name="myvote[]" value="3" /> Quote Link to comment https://forums.phpfreaks.com/topic/277706-checkboxes/#findComment-1428623 Share on other sites More sharing options...
rghayes Posted May 6, 2013 Author Share Posted May 6, 2013 Trying now, but still not working I am using the an array as shown above and then $vote=$_REQUEST['myvote']; to request what has been checked. Is there a better way? Quote Link to comment https://forums.phpfreaks.com/topic/277706-checkboxes/#findComment-1428628 Share on other sites More sharing options...
Barand Posted May 6, 2013 Share Posted May 6, 2013 $_REQUEST['myvote'] will be an array if any are checked. foreach ($_REQUEST['myvote'] as $vote) echo $vote; Quote Link to comment https://forums.phpfreaks.com/topic/277706-checkboxes/#findComment-1428636 Share on other sites More sharing options...
rghayes Posted May 6, 2013 Author Share Posted May 6, 2013 That works but it shows all the checked values shoved together likethis. It still only inputs the last on chosen into the db. Quote Link to comment https://forums.phpfreaks.com/topic/277706-checkboxes/#findComment-1428638 Share on other sites More sharing options...
farahZ Posted May 6, 2013 Share Posted May 6, 2013 Add , <br>; aftr the echo part to view choices on lines And then run an array sending value by value to the db If u want all values to be sent as a string, concatenate them into one string separated by , and then send the stribg itself Quote Link to comment https://forums.phpfreaks.com/topic/277706-checkboxes/#findComment-1428671 Share on other sites More sharing options...
Barand Posted May 6, 2013 Share Posted May 6, 2013 You have told us nothing about your tables or how you plan to store the votes so what more can we tell you? Quote Link to comment https://forums.phpfreaks.com/topic/277706-checkboxes/#findComment-1428676 Share on other sites More sharing options...
jazzman1 Posted May 6, 2013 Share Posted May 6, 2013 (edited) Well, as Barand said you have to tell us about your database structure, so I wrote a simple example how to insert multiple radio values to database. In this example I'm going to use Barand's example. You can take a look that thread too - http://forums.phpfreaks.com/topic/267013-looping-multiple-rows-into-database/ <?php if(!empty($_POST['Submit'])) { $votes = $_POST['myvote']; // build a query string ..... $sql = "INSERT INTO `tbl_name`(`votes`) VALUES"; foreach ($votes as $vote) { $sql .= "(".$vote; // trim the last comma from a query string in case you have multiple values //$sql = rtrim($sql, ','); $sql .= "),"; } // trim the last comma from a query string $sql = rtrim($sql, ','); //echo '<pre>' . print_r($sql, true) . '</pre>'; } ?> <form method="post"> <input type="checkbox" name="myvote[]" value="1" /> <input type="checkbox" name="myvote[]" value="2" /> <input type="checkbox" name="myvote[]" value="3" /> <input type="submit" name="Submit" value="Go!" /> </form> Results: INSERT INTO `tbl_name`(`votes`) VALUES(1),(3) Edited May 6, 2013 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/277706-checkboxes/#findComment-1428679 Share on other sites More sharing options...
rghayes Posted May 6, 2013 Author Share Posted May 6, 2013 OK, my database just has entries for each choice represented by each check box and I need to keep up with how many times an item is voted on. Quote Link to comment https://forums.phpfreaks.com/topic/277706-checkboxes/#findComment-1428684 Share on other sites More sharing options...
jazzman1 Posted May 6, 2013 Share Posted May 6, 2013 OK, my database just has entries for each choice represented by each check box and I need to keep up with how many times an item is voted on. Well, I think it's time to show us what you've done so far Quote Link to comment https://forums.phpfreaks.com/topic/277706-checkboxes/#findComment-1428688 Share on other sites More sharing options...
rghayes Posted May 6, 2013 Author Share Posted May 6, 2013 Ok here is my PHP. I think my biggest problem now is I need to update the count in the db not really add anything <?php $vote=$_POST['vote']; $tstring = implode(',' , $vote); echo $tstring; $con = mysql_connect("localhost","root","YES"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("poll", $con); //insert to the table $sql="update poll_results set num_votes = num_votes + 1 where candidate = '$tstring'"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "CheckBox value succesfully added"; mysql_close($con) ?> Quote Link to comment https://forums.phpfreaks.com/topic/277706-checkboxes/#findComment-1428707 Share on other sites More sharing options...
jazzman1 Posted May 6, 2013 Share Posted May 6, 2013 (edited) Try, $vote = $_POST['vote']; $tstring = implode(',', $vote); $sql="update `poll_results` set `num_votes` = `num_votes` + 1 where candidate IN ('".$tstring."')"; Edited May 6, 2013 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/277706-checkboxes/#findComment-1428713 Share on other sites More sharing options...
rghayes Posted May 6, 2013 Author Share Posted May 6, 2013 Didn't work, sends nothing to the db when multiple entries are chosen. works fine if only one is chose. Quote Link to comment https://forums.phpfreaks.com/topic/277706-checkboxes/#findComment-1428719 Share on other sites More sharing options...
Barand Posted May 6, 2013 Share Posted May 6, 2013 You have told us nothing about your tables or how you plan to store the votes so what more can we tell you? Quote Link to comment https://forums.phpfreaks.com/topic/277706-checkboxes/#findComment-1428720 Share on other sites More sharing options...
rghayes Posted May 6, 2013 Author Share Posted May 6, 2013 Only one table with 2 columns. First column is the name of what is being voted for and the second column keeps up with the number of votes. This is what I think is giving me such a hard time, all I need to do is update the number of votes in the db not really add data. Quote Link to comment https://forums.phpfreaks.com/topic/277706-checkboxes/#findComment-1428721 Share on other sites More sharing options...
jazzman1 Posted May 7, 2013 Share Posted May 7, 2013 (edited) So, my fault. Try this and give me back the result of the echo, because I'm not using my machine: $vote = $_POST['myvote']; $tstring = implode(',',array_map('mysql_real_escape_string', $vote)); $sql="update `poll_results` set `num_votes` = `num_votes` + 1 where candidate IN ($tstring)"; echo $sql; Edited May 7, 2013 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/277706-checkboxes/#findComment-1428724 Share on other sites More sharing options...
rghayes Posted May 7, 2013 Author Share Posted May 7, 2013 (edited) This is what was returned on submit: Notice: Undefined index: myvote in C:\xampp\htdocs\belltion\show_poll.php on line 2Warning: array_map(): Argument #2 should be an array in C:\xampp\htdocs\belltion\show_poll.php on line 4Warning: implode(): Invalid arguments passed in C:\xampp\htdocs\belltion\show_poll.php on line 4update `poll_results` set `num_votes` = `num_votes` + 1 where candidate IN () Edited May 7, 2013 by rghayes Quote Link to comment https://forums.phpfreaks.com/topic/277706-checkboxes/#findComment-1428725 Share on other sites More sharing options...
jazzman1 Posted May 7, 2013 Share Posted May 7, 2013 Ah.....change the array to $vote = $_POST['vote']; Quote Link to comment https://forums.phpfreaks.com/topic/277706-checkboxes/#findComment-1428726 Share on other sites More sharing options...
Barand Posted May 7, 2013 Share Posted May 7, 2013 Only one table with 2 columns. First column is the name of what is being voted for and the second column keeps up with the number of votes. This is what I think is giving me such a hard time, all I need to do is update the number of votes in the db not really add data. A lot easier to store userid | vote where vote is the checkbox value. If they vote for 3 items, write 3 records (one for each) All you have to do later is query the db to get the counts SELECT vote, COUNT(*) as total FROM votes GROUP BY vote ORDER BY total DESC Quote Link to comment https://forums.phpfreaks.com/topic/277706-checkboxes/#findComment-1428728 Share on other sites More sharing options...
rghayes Posted May 7, 2013 Author Share Posted May 7, 2013 OK now it gave me this: update `poll_results` set `num_votes` = `num_votes` + 1 where candidate IN (TomJo,AlJac,AmyThom,ArthurBo,BillyMid,KarenMil) Quote Link to comment https://forums.phpfreaks.com/topic/277706-checkboxes/#findComment-1428729 Share on other sites More sharing options...
jazzman1 Posted May 7, 2013 Share Posted May 7, 2013 (edited) The correct sql string should be - update `poll_results` set `num_votes` = `num_votes` + 1 where candidate IN ('TomJo','AlJac','AmyThom','ArthurBo','BillyMid','KarenMil'); I'm not at home right now, I am going to create later if I have a time Edited May 7, 2013 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/277706-checkboxes/#findComment-1428731 Share on other sites More sharing options...
Barand Posted May 7, 2013 Share Posted May 7, 2013 How do you stop someone repeatedly submitting the same vote? if you store userid | vote with unique key on both fields it makes it impossible Quote Link to comment https://forums.phpfreaks.com/topic/277706-checkboxes/#findComment-1428732 Share on other sites More sharing options...
jazzman1 Posted May 7, 2013 Share Posted May 7, 2013 Barand is right! Going back to the query above and the implode function try this, not tested but.... I think is going to be correct: $vote = $_POST['vote']; $tstring = "'" . implode("', '", array_map('mysql_real_escape_string', $vote)) . "'"; $sql="UPDATE `poll_results` SET `num_votes` = `num_votes` + 1 WHERE `candidate` IN ($tstring)"; echo $sql; Quote Link to comment https://forums.phpfreaks.com/topic/277706-checkboxes/#findComment-1428741 Share on other sites More sharing options...
rghayes Posted May 8, 2013 Author Share Posted May 8, 2013 I still am still struggling with this but please check out my updated code. After some serious research I found this but I keep getting Fail to add test_value. if (!$db_conn = new mysqli('localhost', 'root', 'YES', 'charity_db')) { echo 'Could not connect to db<br />'; exit; } if(isset($_POST["vote"])) {$vote = $_POST["vote"];} else {$vote=array();} for ($i="0"; $i<count($vote); $i++) { if(!is_numeric($vote[$i])) {$vote[$i]="";} if(empty($vote[$i])) {unset($vote[$i]);}} $vote = implode ("<>", $vote); $vote = "<>".$vote.""; $sql = "INSERT INTO data_tbl (`id`, `choice`) VALUES (NULL, '$vote')"; $res=mysql_query($sql) or die ("Fail to add test_value"); $sql = "select id, choice from data_tbl limit 1"; $res=mysql_query($sql) or die ("Fail to get test"); $test = explode('<>',$pres[1]); for ($i="0"; $i<count($vote); $i++) { if(empty($vote[$i])) {unset($vote[$i]);} } ?> Quote Link to comment https://forums.phpfreaks.com/topic/277706-checkboxes/#findComment-1429175 Share on other sites More sharing options...
jazzman1 Posted May 8, 2013 Share Posted May 8, 2013 What was happened with my code above? Instead die("some text here") use die(mysql_error()) and let's see the error messages. Quote Link to comment https://forums.phpfreaks.com/topic/277706-checkboxes/#findComment-1429178 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.