好的,我正在为我的管理面板构建(尝试)优惠券代码系统,我想要实现的是允许管理员通过设置 coupon_code 和手动创建优惠券coupon_discount(1%至100%)。当他提交时,它将被存储在两个不同的dbs表中。
表优惠券:
我在网上检查了很多例子,这可能是我写得很差的课程:
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 - 新优惠券制作
在管理方面,我完全迷失了如何将coupon_code和coupon_discount传递给数据库......如何利用我在课堂上写的功能...这就是我所做的:
<?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();
}
?>
我收到了coupon_code和coupon_discount的未定义索引错误。
如果我在这里要求我的班级文件,我得到非抽象方法ProductDiscount :: create()必须包含正文错误。
<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>
这是我的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");
?>
现在凌晨4点,我正在努力学习这些课程。
所以我的问题是: &#34;如何以正确的方式将必要的表单数据保存到表中并将优惠券保存到表中?&#34; < /强>
(仅编辑并指定我当前的问题,您可以重新开启吗?)
(评论了长/不必要的部分以澄清)
非常感谢您的时间!
答案 0 :(得分:1)
好的,到目前为止,我设法解决了这个问题。使用bootstrap表。
使用&#34;添加优惠券&#34;按钮:
<button data-toggle='modal' data-target='#myModal' type='button' class='btn btn-primary' style="margin: 20px;" name='add-btn' id='add-btn'>Add Coupon</button>
data-toggle='modal'
和data-target='#myModal'
将模态div链接到按钮
SELECT语句获取优惠券表并将数据拉入引导表。
<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>
这是javascript
<script>
// Function added to each edit button. Sets the ID to the button "data-id" value.
// Finds the parent (td) of the button, then goes to each sibling with the correct class name
// and changes the modal input value to the inner text of the TD
var editfunction = function() {
$('#couponId').val($(this).attr('data-id'));
var parent = $(this).parent();
$('#couponCode').val($(parent).siblings('.coupon_code').text());
$('#couponDiscount').val($(parent).siblings('.coupon_discount').text());
$('#couponExpire').val($(parent).siblings('.coupon_expire').text());
};
// Called on the ADD button only
// Sets the ID to 0 (to INSERT the record on the form submit)
// Sets the other inputs to blank values
function addNewCoupon()
{
$('#couponId').val('0');
$('#couponCode').val('');
$('#couponDiscount').val('');
$('#couponExpire').val('');
}
// When the page has loaded...
// Attach the edit function call to the click event of each edit button
// Attach the add new coupon to the click event of the add button
$(document).ready(function() {
$('button[name="submit-btn"]').click(editfunction);
$('#add-btn').click(function() { addNewCoupon(); });
});
</script>
然后在createcoupon.php上我这样做了:
// Check connection
$coupon_id = intval($_POST['coupon_id']);
$coupon_code = $_POST['coupon_code'];
$coupon_discount = $_POST['coupon_discount'];
在使用mysqli连接之后我插入/更新了(在if else语句中,&#34;如果是&#39;添加优惠券&#39;或者编辑优惠券&#39;)他们。
我最终解决了这个问题,如果您有任何想法,请与我分享,以便我可以改进。
我希望这些代码对某人有用!
感谢您的时间。