Jump to content

unable to store value of radio button into mysql


orbitter

Recommended Posts

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

?>

 

 

Link to comment
Share on other sites

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>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.