Make sure you distinguish between the
specific problems you're asking about and the description of the overall goal. Both need to be included, but when you mix the two it isn't clear what you're asking for. Moreover, the larger the request, the fewer people will look at it and the fewer good responses you'll get.
A design concern: what happens if someone distributes the link they're given after successfully submitting a coupon? Will the link work in perpetuity?
The
mysql extension is outdated and on its way to deprecation. Instead, use PDO, which has many useful improvements, such as
prepared statements and support for the
Traversable interface, so you can loop over results with
foreach. If you need a PDO tutorial, try "
Writing MySQL Scripts with PHP and PDO".
Speaking of prepared statements, the code could use them. The sample is vulnerable to
SQL injection, which is a very serious
security risk. Data shouldn't be interpolated directly into a statement.
Mixing data access with display and logic code results in high
coupling, which generally should be avoided. Better to
separate each concern into distinct modules. See the
MVC and
Three-Tier architectures for some top-level examples of how these can be separated.