【发布时间】:2018-02-15 00:55:11
【问题描述】:
所以我目前正在用 PHP 制作这个小网店应用程序。
目前正在构建一个允许生成优惠券代码的功能。 这本身就很好用。虽然我不确定如何生成多个代码并将其插入数据库。
试图对此进行自我教育。 所以伸出援助之手当然会很棒!
我的代码在这里:
<?php
$page = "Gift Cards";
require_once 'header.php';
if(isset($_POST['createnewCard']))
{
$plan = $_POST['plan'];
if(empty($plan))
{
$notify = error('Plan input was empty!');
}
if(empty($notify))
{
/// Generate Gift Code
$code = substr(base_convert(sha1(uniqid(mt_rand())), 16, 36), 0, 10);
/// Input to database
$SQLinsert = $odb -> prepare("INSERT INTO `giftcards` VALUES(NULL, :code, :planID, 0, 0, UNIX_TIMESTAMP())");
$SQLinsert -> execute(array(':code' => $code, ':planID' => $plan));
$notify = success('New Giftcard has been generated. New code is: '.$code.'');
}
}
?>
【问题讨论】:
-
创建一个类并调用构造函数
-
您想只创建特定数量的代码吗?您可以循环执行此操作。我肯定至少会将其抽象为某种辅助函数或辅助类。