gpt4 book ai didi

php - 将输入保存到数据库和类错误 - 构建优惠券系统

转载 作者:行者123 更新时间:2023-11-29 02:59:07 25 4
gpt4 key购买 nike

好的,我正在为我的管理面板构建(尝试)优惠券代码系统,我想要实现的是允许管理员通过设置其coupon_codecoupon_discount ( 1% 至 100%)。当他提交时,它将存储在两个数据库的两个不同表中。

表格优惠券:

  • coupon_id INT(10) NOT NULL AUTO INCREMENT - 主键
  • coupon_code VARCHAR(15) NOT NULL - 唯一
  • 折扣十进制(3,2)
  • 过期 DATETIME -NULL
  • 计数 INT(10) -NULL

我在网上查了很多例子,这可能是我写得不好的类(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 - 新优惠券创建

在管理方面,我完全不知道如何将 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 的未定义索引错误..

如果我在此处 require_once 我的类文件,我将收到非抽象方法 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 点,我努力学习这些已经有几个小时了。

所以我的问题是: “如何以正确的方式将必要的表单数据保存到表中,并将优惠券保存到表中?”

(只编辑和指定了我当前的问题,你能重新打开吗?)

(评论了较长/不必要的部分以澄清)

非常感谢您的宝贵时间!

最佳答案

到目前为止,我设法解决了它。使用 Bootstrap 表。

使用“添加优惠券”按钮:

<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>
  1. data-toggle='modal'data-target='#myModal' 将模态 div 链接到按钮

    <
  2. 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">&times;</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 语句中,“if is 'add coupon' or 'edit coupon')。

我最终解决了这个问题,如果您对此有任何想法,请与我分享,以便我改进。

我希望这些代码对某些人有用!

感谢您的宝贵时间。

关于php - 将输入保存到数据库和类错误 - 构建优惠券系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26644070/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com