orbitter Posted February 26, 2008 Share Posted February 26, 2008 hi there, i am trying to input the value of a radio button when it is clicked and submitted, from a html form to a mysql database. this is a poll script, so when a user clicks for example the radio button for John smith and submits it, a value of 1 should appear on the table created on mysql and so on. any help with where i am going wrong in my script would be helpful. thank you this is the html form vote.html: <html> <head> <title>polling</title> <head> <h1>pop pol</h1> <p>who will you vpte for in the election</p> <form method="post" action="show_poll.php"> <input type="radio" name="vote" vote="John Smith">John Smith</ br> <input type="radio" name="vote" vote="Mary Jones">Mary Jones</ br> <input type="radio" name="vote" vote="Fred Bloggs">Fred Bloggs</ br> <input type="submit" value="Show results"> </form> </body> here is the php script show_poll.php: <?php //get vote from form $vote=$_POST['vote']; //log into database if (!$db_conn = mysql_connect("localhost", "root", "fadiyounes746")) { echo 'could not connect to db<br />'; exit; } mysql_select_db("poll"); if (!empty($vote)) //if they filled the form out, add their vote { $vote = addlashes($vote); $query = "update poll_results set num_votes = num_votes + 1 where candidate = '$vote'"; if(!($result = mysql_query($query, $db_conn))) { echo 'could not connect to database<br />'; exit; } } // get current results of poll, regardless of whether they voted $query = 'select * from poll_results'; if(!($result = mysql_query($query, $db_conn))) { echo 'could not connect to dataBase<br />'; exit; } $num_candidates = mysql_num_rows($result); //calculte total number of votes so far $total_votes=0; while ($row = mysql_fetch_object ($result)) { $total_votes += $row->num_votes; } mysql_data_seek($result, 0); //reset the result pointer ?> Quote Link to comment Share on other sites More sharing options...
rhodesa Posted February 26, 2008 Share Posted February 26, 2008 In your inputs, you have vote= instead of value= change this <input type="radio" name="vote" vote="John Smith">John Smith</ br> to this <input type="radio" name="vote" value="John Smith">John Smith</ br> Quote Link to comment Share on other sites More sharing options...
orbitter Posted February 26, 2008 Author Share Posted February 26, 2008 hi there thanks for your reply, i changed it to value but still wont store the value into the mysql database, any ideas how this can be solved? thank you for your help Quote Link to comment Share on other sites More sharing options...
orbitter Posted February 26, 2008 Author Share Posted February 26, 2008 so the html file was changed to the below but still does not store the value of the radio button into mysql: <html> <head> <title>polling</title> <head> <h1>pop pol</h1> <p>who will you vpte for in the election</p> <form method="post" action="show_poll.php"> <input type="radio" name="vote" value="John Smith">John Smith</ br> <input type="radio" name="vote" value="Mary Jones">Mary Jones</ br> <input type="radio" name="vote" value="Fred Bloggs">Fred Bloggs</ br> <input type="submit" value="Show results"> </form> </body> Quote Link to comment 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.