Jump to content

cant find the error in my code.


silverglade

Recommended Posts

hi i cant for the life of me find the error in this code that updates a database and shows output. please help. thanks.

 

here is the sql for the table

 

CREATE TABLE `usa` (
  `id` int(11) NOT NULL auto_increment,
   
  `city` varchar(32) NOT NULL,
  `state` 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 `usa_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 ;

 

here is the code for the page

 

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

$checkIp	= TRUE; // Set to tru to enable ip checking
$server		= ' ';
$user		= ' ';
$password	= ' ';
$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) {
$city	= post('city');
$state  = post('state');



$city	= strip_tags(mysql_real_escape_string($city));
$state 	= strip_tags(mysql_real_escape_string($state));

}

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

$Check 	= mysql_query("SELECT * FROM usa_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  ".$city. " in ".$state."<br /><br />");

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

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

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

if(mysql_num_rows($result) > 0)
{
	echo "Top ten places to live :<br />";
	echo '<table border="1">
			<tr>
				<td>City</td>
				<td>State</td>


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


				<td>'.$row['vote'].' </td>
			  </tr>
			';
	}
	echo '</table>';
}
else
{
	echo "No places 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>place to live in the USA</title>
</head>

<body>
</p>
<div align="center">welcome to <strong>vote your favorite places to live in the USA</strong>  </div>
<p>Please enter your vote for your favorite place to live in the USA.<span class="style4"></span><br />
</p>
<form id="form1" name="form1" method="post" action="">
  <table border="0">
    <tr>
      <td width="31">City</td>
      <td width="334">
      <input name="city" type="text" id="city" /></td>
    </tr>
    <tr>
      <td>State</td>
      <td>
       <input name="state" type="text" id="state" /> 
      </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 PLACES TO LIVE</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/191042-cant-find-the-error-in-my-code/
Share on other sites

nevermind i have a new problem i fixed that one. here is the problem. its a voting script. and here is the error i get when i press submit, i looked over my code and the table code like a million times and cant see the error, please help if you can . thanks. derek

 

You have voted for David by Michelangelo


Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /hermes/bosweb/web173/b1739/sl.brendansite1/public_html/TOPTENBOX/sculpture/sculpture.php on line 51
No sculptures in the database 

 

 

and here is my database code

 

CREATE TABLE `sculpture` (
  `id` int(11) NOT NULL auto_increment,
  `sculpt` 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 `sculpture_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 ;

 

and here is my page code

 

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

$checkIp	= TRUE; // Set to tru to enable ip checking
$server		= ' ';
$user		= ' ';
$password	= ' ';
$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) {
$sculpt	= post('sculpt');
$artist  = post('artist');



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

}

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

$Check 	= mysql_query("SELECT * FROM sculpture_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  ".$sculpt. " by ".$artist."<br /><br />");

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

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

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

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


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


				<td>'.$row['vote'].' </td>
			  </tr>
			';
	}
	echo '</table>';
}
else
{
	echo "No sculptures 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>Sculpture</title>
</head>

<body>
</p>
<div align="center">welcome to <strong>vote your sculpture</strong>  </div>
<p>Please enter your vote for your favorite sculpture.<span class="style4"></span><br />
</p>
<form id="form1" name="form1" method="post" action="">
  <table border="0">
    <tr>
      <td>Sculpture</td>
      <td>
        <input name="sculpt" type="text" id="sculpt" /></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 SCULPTURES</div></td>
    </tr>
    <tr>
      <td> </td>
      <td><input type="submit" name="submit" id="submit" value="submit" /></td>
    </tr>
  </table>
  
</form>

</body>
</html>

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.