0

I work for a company that does online coupons. We'd like to restrict the printing of coupons to only those people who have not printed them before. I'm doing this by inserting couponid numbers and ip addresses to a Print Tracking table.

My plan is upon using the print button, a php script checks the MySQL db for records containing the exact couponid and ip address. When it finds a match it echoes "You have already used this coupon, please choose another"

If no match is found, a new record is created with the couponid and ip address and the user is granted access to the coupon page. Now that the user's ip address along with the couponid is stored, they should not be able to return from the same IP address and print the same coupon.

I am new to IF and ELSE statements. Thanks in advance.

Here is my code:

<?php
// connect to db 
mysql_connect("xxxxxx", "xxxxxx", "xxxxxx") or die(mysql_error()) ; 
mysql_select_db("xxxxxx") or die(mysql_error()) ; 

// grab user ip
$ip = $_SERVER['REMOTE_ADDR'];
echo $ip;

$result = mysql_query("SELECT coupid, ip FROM print_tracking");

while($row = mysql_fetch_array($result))
  {
  echo $row['coupid'] . " " . $row['ip'];
  echo "<br />";
  }
  
if ( $row['coupid'] = $row_rcdCoupons['coupid'] AND $row['ip'] = "$ip" )
echo "You have already printed this coupon, please choose another.";

else 
echo "Thank You.";

mysql_query("INSERT INTO print_tracking (coupid, ip)
VALUES ('$row_rcdCoupons[coupid]', '$ip')");
?>
2
Contributors
4
Replies
5
Views
5 Years
Discussion Span
Last Post by strongpot
0

= is for assignment, == for comparison.

That certainly helped me along, thank you!

The ip and couponid are being inserted even when they already exist in the db. How do I avoid this?

<?php
// connect to db 
mysql_connect("", "", "") or die(mysql_error()) ; 
mysql_select_db("") or die(mysql_error()) ; 

// grab user ip
$ip = $_SERVER['REMOTE_ADDR'];
echo $ip;

$result = mysql_query("SELECT coupid, ip FROM print_tracking");

while($row = mysql_fetch_array($result))
  {
  echo $row['coupid'] . " " . $row['ip'];
  echo "<br />";
  }
  
if ( $row['coupid'] == $row_rcdCoupons['coupid'] AND $row['ip'] == "$ip" )
echo "You have already printed this coupon, please choose another.";

else 
echo "Thank You.";

mysql_query("INSERT INTO print_tracking (coupid, ip)
VALUES ('$row_rcdCoupons[coupid]', '$ip')");
?>

Edited by strongpot: n/a

0

Add on line 11:

$found = false;

between 15-16:

if ( $row['coupid'] == $row_rcdCoupons['coupid'] AND $row['ip'] == "$ip" )
  $found = true;

on line 23:

if (! $found)
0

Add on line 11:

$found = false;

between 15-16:

if ( $row['coupid'] == $row_rcdCoupons['coupid'] AND $row['ip'] == "$ip" )
  $found = true;

on line 23:

if (! $found)

Much appreciated sir. Working great.

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.