Okay, I'm building (trying) a coupon code system for my admin panel, what I want to achieve is allow admin to manually create coupons by setting its coupon_code and coupon_discount (1% TO 100%). When he will submit, it will be stored in two different tables of two dbs.
TABLE coupons:
- coupon_id INT(10) NOT NULL AUTO INCREMENT - Primary key
- coupon_code VARCHAR(15) NOT NULL - unique
- discount DECIMAL(3,2)
- expire DATETIME -NULL
- count INT(10) -NULL
I checked lots of examples on the net and this is my probably poorly written class:
class ProductDiscount {
static public function validate($coupon_code);
private $_coupon_code; // string
private $_coupon_discount; // integer??
private $_expire; // null cause unlimited
private $_count; // null
private function __construct(); //for this line I got an error
public function getCouponCode(); // return string
public function getCouponDiscount(); // return
public function getCount(); // returns null unlimited
public function getExpireDate();// null
public function useDiscount(); // apply this discount now
public function useAllDiscount(); // invalidate this discount for future use
COUPONS.PHP - new coupon creation
On the admin side, I am totally lost about how to pass the coupon_code and coupon_discount to the database...How to utilize the functions that I wrote in the class...This is what I did:
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST') {
$coupon_code = $_POST['coupon_code'];
$coupon_discount = $_POST['coupon_discount'];
//insert into db for admin
$connect = new mysqli("localhost","-----","-------","--------");
$stmt = $connect->prepare("INSERT INTO `coupons` (`coupon_code`, `coupon_discount`) VALUES (?,?)");
$stmt->bind_param('si', $coupon_code, $coupon_discount);
$stmt->execute();
$connect->close();
}
?>
I'm getting undefined index errors for both the coupon_code and coupon_discount..
If I require_once my class file here, I'm getting Non-abstract method ProductDiscount::create() must contain body error.
<form class="form-horizontal" role="form" action="createcoupon.php">
<div class="form-group">
<label class="col-sm-2 control-label">Coupon Code</label>
<div class="col-sm-2">
<input type="text" class="form-control" name="coupon_code" id="couponCode" placeholder="e.g SAVE10">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Coupon Discount</label>
<div class="col-sm-2">
<input type="number" class="form-control" name="coupon_discount" id="couponDiscount" placeholder="e.g 10 for 10%">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-3">
<input type="submit" style="background-color:#7575DD; padding:0 !important; color:white;" name="create_coupon" value="Create Coupon" class="btn btn-default"/>
</div>
</div>
</form>
Here is my createcoupon.php:
<?php
//Connect to the 1st db
$coupon_code = $_POST['coupon-code'];
$coupon_discount = $_POST['coupon-discount'];
//the prepared stmt to insert into db1
//Connect to the 2nd tb
$coupon_code = $_POST['coupon-code'];
$coupon_discount = $_POST['coupon-discount'];
//prepared stmt to insert into db2
header("location: http://example.com/admin/coupons");
?>
It's 4am and been hours that I'm trying to learn these.
So my question is: "How can I save the necessary form data to the table in the correct way and save the coupon into the table?"
(edited and specified my current problem only, can you reopen?)
(commented the long/unnecessary parts to clarify)
Thank you so much for your time!