I have three problems i would like help with.
ONE:
Assume a database called "vote" with table "president", the table has four tuples
at the moment. I was hoping to have all the tuples printed on screen but i only
get from the second tuple; the first row is not shown on the page when it loads.
how can i fix that?
TWO:
The table "president" has a field called "votes". on the table there is next to each name
a radio button. now, i was expecting that when a certain radio button is selected and submitted
then the "votes" attribute would be modified by adding one to it but the update seems to only
be taking place on the last tuple of the database. i get why this happens. the last value of
"stuID" to be passed is in the last tuple. how can i get the correct modification of the field
"votes" such that when a user clicks the radio button next to second row in the table, the votes
attribute in that row is increased by one?
THREE:
on the table i have, how would i show a picture so that a picture of what is being selected can be
seen? assume i have a picture called "logo.png" that is in a folder called "images".
I am trying to build a voting application, any tips on how i can improve the code will be appreciated.
the code i have is below:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Vote</title>
</head>
<body>
<?php
//declare variavles
$name = trim($_POST['name']);
$surname = trim($_POST['surname']);
$stuID = trim($_POST['stuID']);
$votes = trim($_POST['votes']);
//make connection to the database
$connect = mysql_connect("localhost", "root","");
if (!$connect)
{
die("database connection failed". mysql_error());
}
//make sure we’re using the right database
$select_db = mysql_select_db("vote");
if (!select_db)
{
die("database selection failed " .mysql_error());
}
$query = "SELECT name, surname, stuID, votes FROM president";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_row($result);
//get and print the name, surname, stuID and votes
while($row = mysql_fetch_array($result))
{
echo "<form method=\"post\" action=\"votes.php\">";
echo "<img src="logo.jpg" width="50"height="50">";
echo "Name: <input name=\"name\" type=\"text\" value = \"".$row[0]."\" />";
echo "Surname: <input name=\"surname\" type=\"text\" value = \"".$row[1]."\" />";
echo "StudID: <input name=\"stuID\" type=\"int\" value = \"".$row[2]."\" />";
echo "Votes: <input name=\"votes\" type=\"text\" value = \"".$row[3]."\" />";
echo "<input name=\"Vote\" type=\"submit\" value=\"vote\" />";
echo "<br/>";
}
//if vote button is clicked
if (isset($_POST['Vote']))
{
//make connection
$connect = mysql_connect("localhost", "root","");
if (!$connect)
{
die("database connection failed". mysql_error());
}
//make sure we’re using the right database
$select_db = mysql_select_db("vote");
if (!select_db)
{
die("database selection failed " .mysql_error());
}
$update = 1;
//echo "value of update is ". $update;
echo "<br/>";
//add one the current value of votes
mysql_query("UPDATE president SET votes = ($votes + $update) WHERE stuID = '$stuID'");
//echo "value of votes is ".$votes;
//echo "<br/>";
//echo "value of candidate is ".$name;
}
echo "</form>";
?>
</body>
</html>