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

?>

 

 

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>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.