0

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>
2
Contributors
5
Replies
6
Views
5 Years
Discussion Span
Last Post by broj1
0

First thing: escape double quotes in line 44:

echo "<img src=\"logo.jpg\" width=\"50\" height=\"50\">";

Lines 14 to 18 should go after the if on line 57. When you check whether the form was submited and if it was you can read POST variables and then you will be using the correct value in $stuID.

To avoid possible errors each $row element should be checked for existence before using it (it would be also better to use associative keys in result array). You actualy need only $stuID and you should check it for existence.

if(isset($stuID)) {
   $stuID = trim($_POST['stuID']);

   // do all the database writing here

} else {

   echo 'Error reading form data!';
}

Input element in row 47 has type="int". I do not think such type exists in HTML.

if(isset($row[0])) {
    $row0 = $row[0];
} else {
    $row0 = '';

if(isset($row[1])) {
    $row1 = $row[1];
} else {
    $row1 = '';

if(isset($row[2])) {
    $row2 = $row[2];
} else {
    $row2 = '';

if(isset($row[3])) {
    $row3 = $row[3];
} else {
    $row3 = '';;

//get and print the name, surname, stuID and votes
while($row = mysql_fetch_array($result)) {

    ...

    echo "Name: <input name=\"name\" type=\"text\"  value = \"".$row0."\"  />";
    echo "Surname: <input name=\"surname\" type=\"text\"  value = \"".$row1."\"  />";
    echo "StudID: <input name=\"stuID\" type=\"text\"  value = \"".$row2."\"  />";
    echo "Votes: <input name=\"votes\" type=\"text\"  value = \"".$row3."\"  />";
    echo "<br/>";

    ....
	}
0

i got the picture part to work but the voting still does not work as it should. i do not get where the alterations i am to make have to go.... where do i put in the if statements

if(isset($row[0])) {
$row0 = $row[0];}
else
{ $row0 = '';
if(isset($row[1]))
{ $row1 = $row[1];}
else {
$row1 = '';
if(isset($row[2]))
{ $row2 = $row[2];}
else { $row2 = '';
if(isset($row[3]))
{ $row3 = $row[3];}
else { $row3 = '';

and where do i put this?

if(isset($stuID)) { $stuID = trim($_POST); // do all the database writing here } else { echo 'Error reading form data!';}if(isset($stuID)) {
$stuID = trim($_POST);

// do all the database writing here

} else {

echo 'Error reading form data!';
}

0

I put the proposed changes to your code. It is slightly different from what I said in previous post since I become more familiar with your code. Hope this is what you are after and hopefully I haven't introduced some errors :-).

<!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

    //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))
    {
        // check if the fields in $row contain anything
        if(isset($row[0])) {
            $row0 = $row[0];
        } else {
            $row0 = '';
        }
        
        if(isset($row[1])) {
            $row1 = $row[1];
        } else {
            $row1 = '';
        }
        
        if(isset($row[2])) {
            $row2 = $row[2];
        } else {
            $row2 = '';
        }
        
        if(isset($row[3])) {
            $row3 = $row[3];
        } else {
            $row3 = '';
        }

        // start the form
        echo "<form method=\"post\" action=\"votes.php\">";
        echo "<img src=\"logo.jpg\" width=\"50\"height=\"50\">";
        echo "Name: <input name=\"name\" type=\"text\" value=\"$row0\" />";
        echo "Surname: <input name=\"surname\" type=\"text\"  value=\"$row1\"  />";
        echo "StudID: <input name=\"stuID\" type=\"text\" value=\"$row2\"  />";
        echo "Votes: <input name=\"votes\" type=\"text\" value=\"$row3\"  />";
        echo "<input name=\"Vote\" type=\"submit\" value=\"vote\" />";
        echo "<br/>";
        
        // end the form
        echo "</form>";
    }

    //if vote button is clicked
    if (isset($_POST['Vote']))
    {

        // first check if $stuID exists and is numeric
        // (maybe user has not input the ID), if applicable
        // you can also check if it is a number with is_numeric($_POST['stuID'])
        if(isset($_POST['stuID'])) { 
            
            // deslare a variable $stuID
            $stuID = trim($_POST['stuID']);
            
        } else {
            
            die('Error reading form data!');
        }

        //declare other variables
        $name = trim($_POST['name']);
        $surname = trim($_POST['surname']);
        
        // this is not neccessary here as it is done above
        // $stuID = trim($_POST['stuID']);
        
        $votes = trim($_POST['votes']);

        //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;
    }
?>

</body>

</html>
0

got it and it works fine, now i have to figure out how to make sure that users cannot navigate back to the previous page using the "back" page on their browser... would session help and how would i get it so that the user cannot type the url of a certain page and navigate straight to it skipping all the validating i have used.

0

I think preventing user going back using back button is not a very good idea. Back button is there to help the user navigating and disabling it is like taking a wheel out of a user's car if you know what I mean. I the user goes back to the login page you can tell them that this will log him out but you should still allow him to do it. Just my opinion based on the fact that I have quite a lot of experience with users' browsing habits.

But anyway, yes, you can use session to track valid logged-in users. There are some nice threads on this forum that can show you how.

http://www.daniweb.com/web-development/php/threads/410804
http://www.daniweb.com/web-development/php/threads/349627
...

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.