odd problems (showing more results then there is)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

odd problems (showing more results then there is)

Post by psychotomus »

anyone know why?

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?
include 'config.php';
$cat_id = mysql_real_escape_string($_GET['cat']);
$ip = $_SERVER['REMOTE_ADDR'];
if(isset($_POST['Submit']))
{
	$Code = mysql_real_escape_string(strip_tags($_POST['textCode']));
	$Discount = mysql_real_escape_string(strip_tags($_POST['textDiscount']));
	mysql_query("INSERT INTO coupons (cat_id, coupon, discount) VALUES ('$cat_id', '$Code', '$Discount')") or die(mysql_error());
	$msg = "Coupon is now pending approval";
}
$result = mysql_query("SELECT cat FROM cats WHERE id='$cat_id'") or die(mysql_error());
$cat = mysql_fetch_object($result);

echo $msg;
$result = mysql_query("SELECT * FROM coupons WHERE approved='y' AND cat_id='$cat_id' AND featured='y' ORDER by success_rate DESC") or die(mysql_error());
while($coupons = mysql_fetch_object($result))
{
?>
<table width="100%"  border="0">
  <tr>
	<td><p>Code: <?= $coupons->coupon ?><br>
	  Coupon: <?= $coupons->discount ?><br>
	  Stats: <?= $coupons->success_rate ?>% success rate (<?= $coupons->votes ?> votes)<br>
	<a href="<?= $coupons->url ?>">USE COUPON</a></p>    </td>
	<td>
	<?
	$result = mysql_query("SELECT id FROM votes WHERE ip='$ip' AND coupon_id='$coupons->id' AND vote='n'") or die(mysql_error());
	if(mysql_num_rows($result) == 0)
	{
		$result = mysql_query("SELECT id FROM votes WHERE ip='$ip' AND coupon_id='$coupons->id' AND vote='y'") or die(mysql_error());
		echo 'Did this coupon work for you?<br>';
		if(mysql_num_rows($result) == 0)
		{
	?>
	  <a href="vote.php?coupon=<?= $coupons->id ?>&vote=y" target="_blank">Yes</a>
	<?
		}
	?> 
	  | <a href="vote.php?coupon=<?= $coupons->id ?>&vote=n" target="_blank">No </a><br>
	<?
	 }
	 ?>
	  <a href="comments.php?coupon=<?= $coupons->id ?>" target="_blank"><?= $coupons->comments ?> Comments </a><br>
	  <a href="report.php?coupon=<?= $coupons->id ?>">Report Coupon </a></td>
  </tr>
</table>
<?
}
$result = mysql_query("SELECT * FROM coupons  WHERE approved='y' AND cat_id='$cat_id' AND featured='n' ORDER by success_rate DESC") or die(mysql_error());
while($coupons = mysql_fetch_object($result))
{
?>
<table width="100%"  border="0">
  <tr>
	<td><p>Code: <?= $coupons->coupon ?><br>
	  Coupon: <?= $coupons->discount ?><br>
	  Stats: <?= $coupons->success_rate ?>% success rate (<?= $coupons->votes ?> votes)<br>
		<a href="<?= $coupons->url ?>">USE COUPON</a></p>    </td>
	<td>
	<?
	$result = mysql_query("SELECT id FROM votes WHERE ip='$ip' AND coupon_id='$coupons->id' AND vote='n'") or die(mysql_error());
	if(mysql_num_rows($result) == 0)
	{
		$result = mysql_query("SELECT id FROM votes WHERE ip='$ip' AND coupon_id='$coupons->id' AND vote='y'") or die(mysql_error());
		echo 'Did this coupon work for you?<br>';
		if(mysql_num_rows($result) == 0)
		{
	?>
	  <a href="vote.php?coupon=<?= $coupons->id ?>&vote=y" target="_blank">Yes</a>
	<?
		}
	?> 
	  | <a href="vote.php?coupon=<?= $coupons->id ?>&vote=n" target="_blank">No </a><br>
	<?
	 }
	 ?>
	  <a href="comments.php?coupon=<?= $coupons->id ?>" target="_blank"><?= $coupons->comments ?> Comments </a><br>
	  <a href="report.php?coupon=<?= $coupons->id ?>">Report Coupon </a>
	  </td>
  </tr>
</table>
<?
}
?>
<br>
<br>
<form name="form1" method="post" action="">
  Share your <?= $cat->cat ?> Coupon<br>
  <table width="100%"  border="0">
    <tr>
      <td><strong>Code:</strong></td>
      <td><input name="textCode" type="text" size="50"></td>
    </tr>
    <tr>
      <td><strong>Discount:</strong></td>
      <td><input name="textDiscount" type="text" value="" size="50"></td>
    </tr>
    <tr>
      <td></td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><input type="submit" name="Submit" value="Add Coupon"></td>
    </tr>
  </table>
</form>
</body>
</html>
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

You are using the variable $result for three different queries in that loop. Who knows exactly what it is doing?!?
(#10850)
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Post by psychotomus »

ah crap. overlooked it ;\

thanks dude.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

I would recommend separating the code where you are using PHP as a programming language from the code where you are using PHP as a templating language. It will really help you see what is going on and reduce these kinds of errors.
(#10850)
Post Reply