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')");
?>

= is for assignment, == for comparison.

= 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')");
?>

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)

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.