Php 将输入保存到数据库&;课程错误-建立优惠券系统
好的,我正在为我的管理员面板构建(尝试)一个优惠券代码系统,我想实现的是允许管理员通过设置其优惠券代码和优惠券折扣(1%到100%)手动创建优惠券。当他提交时,它将存储在两个数据库的两个不同表中Php 将输入保存到数据库&;课程错误-建立优惠券系统,php,mysql,Php,Mysql,好的,我正在为我的管理员面板构建(尝试)一个优惠券代码系统,我想实现的是允许管理员通过设置其优惠券代码和优惠券折扣(1%到100%)手动创建优惠券。当他提交时,它将存储在两个数据库的两个不同表中 class ProductDiscount { static public function validate($coupon_code); private $_coupon_code; // string private $_coupon_discount; // integer?? pr
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
表优惠券:
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
- 优惠券id INT(10)非空自动递增-主键
- 优惠券代码VARCHAR(15)不为空-唯一
- 折扣小数(3,2)
- 过期日期时间-空
- 计数INT(10)-空
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
coups.PHP-新优惠券创建
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
在管理方面,我完全不知道如何将优惠券代码和优惠券折扣传递到数据库…如何利用我在课堂上编写的函数…这就是我所做的:
<?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();
}
?>
我发现优惠券代码和优惠券折扣都有未定义的索引错误。
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
<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>
如果我在这里需要一次类文件,我得到的是非抽象方法ProductDiscount::create()必须包含正文错误。
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
<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>
优惠码
优惠券折扣
这是我的CreateCouncil.php:
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
<?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");
?>
现在是凌晨4点,我一直在努力学习这些
因此,我的问题是:“如何以正确的方式将必要的表单数据保存到表中,并将优惠券保存到表中?”
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
(仅编辑并指定了我当前的问题,是否可以重新打开?)
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
(评论了要澄清的长/不必要部分)
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
非常感谢您抽出时间 好的,到目前为止,我设法解决了这个问题。使用引导表
<div style="padding: 20px;">
<?php
$con = new mysqli("------","--------","--------","-----");
$query = "SELECT * FROM coupons WHERE expire > NOW() OR expire IS NULL OR expire = '0000-00-00 00:00:00'";
if ($result = $con->query($query))
{
echo "<table border='1' class='table table-hover' data-toggle='table'>
<tr>
<th>Coupon Code</th>
<th>Amount</th>
<th>Expiry</th>
<th></th>
</tr>";
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row['coupon_code'] . "</td>";
echo "<td>" . $row['coupon_discount'] . "</td>";
echo "<td>" . $row['expire'] . "</td>";
echo "<td><button data-toggle='modal' data-target='#myModal' type='button' class='btn btn-primary' name='submit-btn' data-id='" . $row["coupon_id"] ."'>Edit</button></td>";
echo "</tr>";
}
echo "</table>";
}
mysqli_close($con);
?>
使用“添加优惠券”按钮:
这些是模态div和表单,用于发布输入并将其保存到表中
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h4 class="modal-title" id="myModalLabel">Coupon Information</h4>
</div>
<div class="modal-footer">
<form action='createcoupon.php' method='POST'>
<input type='hidden' name='coupon_id' id='couponId' value='0'/>
<input type="text" class="form-control" style="margin-bottom:10px;" name="coupon_code" id="couponCode" placeholder="e.g SAVE10">
<input type="number" class="form-control" style="margin-bottom:10px;" name="coupon_discount" id="couponDiscount" placeholder="e.g 10 for 10%">
<input type="text" class="form-control" style="margin-bottom:10px;" name="coupon_expire" id="couponExpire" placeholder="e.g 01/01/2015">
<input class="btn btn-primary" type='submit' name='submit-btn' value='Save' />
</form>
</div>
</div>
</div>
</div>
</div>
在mysqli连接之后,我插入/更新了它们(在if-else语句中,“if-is‘添加优惠券’或‘编辑优惠券’)
我最终解决了这个问题,如果你对此有任何想法,请与我分享,以便我能够改进
我希望这些代码能对某些人有用
谢谢您的时间。旁注:不要对变量使用连字符
$coupon code
,请使用下划线$coupon\u code
,否则它会出错。@Fred ii-谢谢!我将我的问题编辑得更精确一些。一下子问所有问题是个坏主意……欢迎您加入Ekin。关于“如何检查是否有相同代码的优惠券”,一种方法是使用mysqli_num_rows()
,但是您可能不需要担心,因为您已经将两个列设置为唯一的,但是如果您真的希望检查它是否存在,您可以使用这种方法。折扣???
-这可能是整数
或十进制
或双精度
,这取决于您以后要查询的percentages可以使用,但您可以在以后使用PHP进行计算。count???默认为NULL(无限制)
你可以使用int
来做这件事,如果这是用于某种计数器的话。好的@Fred ii-明白了,先生!关于计算最后的$amount
,我有$unitCost=6995;
这样,它在运费之后直接进入Stripe。所以我需要做一些类似的事情:例如,对于10%的折扣代码:$折扣额=$amount-$amount*0.10
但是在这种情况下,如何将优惠券折扣作为浮动存储在数据库中?