Jump to content

voting page problem


silverglade

Recommended Posts

hi, i have a form that enters a painting and artist's name into the database, then outputs it to the page. its not working. here is what i get back

 

"sorry, you can just vote once.No paintings in the database

welcome to vote your paintings

 

Please enter your vote your favorite painting."

 

any help greatly appreciated. ive looked through the code and database and cant find one error.

 

database sql

 

CREATE TABLE `painting` (
  `id` int(11) NOT NULL auto_increment,
  `painting` varchar(32) NOT NULL,
  `artist` varchar(32) NOT NULL,     
  `vote` varchar(32) NOT NULL,
  `IP` varchar(32) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=19 DEFAULT CHARSET=latin1 AUTO_INCREMENT=19 ;



CREATE TABLE `painting_voters` (
  `id` int(11) NOT NULL auto_increment,
  `ip` varchar(30) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

 

page code

 

<?php
// connect to database include file
//include("connect2.php");

$checkIp	= TRUE; // Set to tru to enable ip checking
$server		= 'localhost';
$user		= 'user';
$password	= 'password';
$database	= 'database';

mysql_connect($server,$user,$password) or die(mysql_error());
mysql_select_db($database) or die(mysql_error());

ini_set('display_errors', 1);
error_reporting(E_ALL);


// escape username and password for use in SQL//person said on board "looks fine" like this
//to prevent sql injections

function post($fieldname = '') {
$data = '';
if($fieldname!='' AND isset($_POST[$fieldname])) $data = $_POST[$fieldname];
return $data;
}

if($_POST) {
$painting	= post('painting');
$artist  = post('artist');



$painting	= strip_tags(mysql_real_escape_string($painting));
$artist 	= strip_tags(mysql_real_escape_string($artist));

}

if(isset($_POST['Submit']) AND isset($_POST['vote']))
{ 

$Check 	= mysql_query("SELECT * FROM voters WHERE  IP='".$_SERVER['REMOTE_ADDR']."'");
$num 	= mysql_num_rows($Check);

if(!$checkIp) $num = 0;

if($num < 1) // counts if the ip is less than 1, if it is more than 1 it means that someone already voted.
{
	echo (" You have voted for  ".$painting. " by ".$artist."<br /><br />");

	$check1 = mysql_query("SELECT vote FROM painting WHERE `painting`='{$painting}' AND `artist`='{$artist}' ");
	if(mysql_num_rows($check1)>0)
	{
		$row = mysql_fetch_array($check1);
		$vote = $row['vote'] + 1;
		mysql_query("UPDATE painting SET vote='$vote' WHERE `painting`='{$painting}' AND `artist`='{$artist}'  ");
	}
	else
	{
		$sql = "INSERT INTO painting (`painting`,`artist`,`vote`) VALUES('{$painting}','{$artist}','1')";
		mysql_query($sql);
	}

	if($checkIp) mysql_query("INSERT INTO painting_voters (ip) VALUES('{$_SERVER['REMOTE_ADDR']}')");
}
else
{
	echo "sorry, you can just vote once.";
}
}
  
if($_POST) {

$query="SELECT * FROM painting ORDER BY vote DESC LIMIT 10";
$result=mysql_query($query); 

if(mysql_num_rows($result) > 0)
{
	echo "Top ten paintings :<br />";
	echo '<table border="1">
			<tr>
				<td>Painting</td>
				<td>Artist</td>


				<td>Vote</td>
			</tr>
		';
	while($row = mysql_fetch_array($result))
	{
		echo '<tr>
				<td>'.$row['painting'].' </td>
				<td>'.$row['artist'].' </td>


				<td>'.$row['vote'].' </td>
			  </tr>
			';
	}
	echo '</table>';
}
else
{
	echo "No paintings in the database";
}
}
   
?>
<!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 http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>painting</title>
</head>

<body>
</p>
<div align="center">welcome to <strong>vote your paintings</strong>  </div>
<p>Please enter your vote for your favorite painting.<span class="style4"></span><br />
</p>
<form id="form1" name="form1" method="post" action="">
  <table border="0">
    <tr>
      <td>Painting</td>
      <td>
        <input name="painting" type="text" id="painting" /></td>
    </tr>
    <tr>
      <td>Artist</td>
      <td>
       <input name="artist" type="text" id="artist" />      </td>
    </tr>
    

    <tr>
      <td> </td>
      <td>
	<input type="submit" name="Submit" value="Submit" />
	<input type="hidden" name="vote" value="TRUE" />	  </td>
    </tr>
  </table>
</form>
<form id="form2" name="form2" method="post" action="">
<table width="666" border="1">
    <tr>
      <td width="42"> </td>
      <td width="608"><div align="center">VIEW THE UPDATED TOP TEN PAINTINGS</div></td>
    </tr>
    <tr>
      <td> </td>
      <td><input type="submit" name="submit" id="submit" value="submit" /></td>
    </tr>
  </table>
  
</form>

</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/191027-voting-page-problem/
Share on other sites

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.