【问题标题】:PHP Generate more than one string and insert into databasePHP 生成多个字符串并插入数据库
【发布时间】: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.'');
        }   
    }
?>

【问题讨论】:

  • 创建一个类并调用构造函数
  • 您想只创建特定数量的代码吗?您可以循环执行此操作。我肯定至少会将其抽象为某种辅助函数或辅助类。

标签: php mysql pdo


【解决方案1】:

您可以使用 for 循环:

for ($x=0; $x<5; $x++) {
     /// 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.'');
}

循环执行 5 次 ($x&lt;5),每次都运行 for 循环中的代码。这将每次生成一个新的礼物代码并将其添加到数据库中。

希望对你有帮助

【讨论】:

    【解决方案2】:

    起初我认为你的范围界定有问题。请参阅变量 $notify。

    if(empty($notify))
    {
        /// Generate Gift Code
        $code = substr(base_convert(sha1(uniqid(mt_rand())), 16, 36), 0, 10);
    
        ...
    }   
    

    我同意 Aaron 的观点,我也会这样做。

    【讨论】:

      【解决方案3】:

      这是一个抽象出来的例子。您需要为您的情况清理它,但它应该生成 5 个代码。

      <?php
      
          $page = "Gift Cards";
          require_once 'header.php'; 
      
          function generateCode() {
              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.'');
                  }   
              }
          }
      
      
          $numOfCodes = 5;
      
          for($i = 0; $i < $numOfCodes; ++$i) {
              generateCode();
          }
      ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-10-04
        • 2011-08-02
        • 1970-01-01
        • 1970-01-01
        • 2018-06-20
        • 2022-01-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多