PHP - Generate Coupon Code using PDO Database and coupon code send to Gmail

PHP - Generate Coupon Code using PDO Database and coupon code  send to Gmail 




                              In this tutorial we will create Generate Coupon Code using PHP and PDO database . PHP is a server-side scripting language designed  for web development. Using PHP, you can let your user directly interact with the script and easily to learned its syntax and easy to develop use knowledge too. PHP is a  user friendly environment. So let's now do it .


 
 
                     

code started before install:


First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for Xampp Server . And this is the link for the Jquery (Jquery is a light weight script) using in this tutorial and Finally, add Bootstrap cdn or package for Layout design Bootstrap .


create Empty files extension .php :-

1. Connect.php

2. Registration.php
 

Create Database :-

 
Create Database I name it registercp.


 Table structure for table `registercp`


database






CREATE TABLE `registercp` (
  `id` int(20) NOT NULL,
  `name` varchar(30) NOT NULL,
  `gender` varchar(20) NOT NULL,
  `email` varchar(70) NOT NULL,
  `city` varchar(20) NOT NULL,
  `type` varchar(30) NOT NULL,
  `phone` varchar(20) NOT NULL,
  `couponcode` varchar(30) NOT NULL,
  `status` varchar(20) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;



1. Creating the database connection:-


<?php
$db_user="root";
$db_pass="";
$db_name="registercp";
$db= new PDO('mysql:host=localhost;dbname='.$db_name.';charset=utf8',$db_user,$db_pass);
$db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);

?>



Coupon code:-

function coupon($l){
$coupon = "Dailyaspirants".substr(str_shuffle(str_repeat('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ',$l-2)),0,$l-2);

return $coupon;
}
$couponcode= coupon(10);



2. Registration.php

dailyaspirants.com



<?php 
session_start();
 include_once('connect.php'); 

?>
<!DOCTYPE html>
<html>
 <head>
 <style>  
.error {color: #FF0001;}  
</style>  
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> 
    <title>Registration Page</title>
      <!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
   <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
    <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
     <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"></script>
     <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
  </head>
  <body>
  <div>
  
<?php
 $db_user="root";
$db_pass="";
$db_name="registercp";
$db= new PDO('mysql:host=localhost;dbname='.$db_name.';charset=utf8',$db_user,$db_pass);

  $errors = array();
   if(isset($_POST['create']))
{
$name = $_POST['name'];
$gender= $_POST['gender'];
$email = $_POST['email'];
        $city =$_POST['city'];
$type =$_POST['type'];
$phone =$_POST['phone'];
$mailtoo='info@dailyaspirants.com';
function coupon($l){
$coupon = "Dailyaspirants".substr(str_shuffle(str_repeat('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ',$l-2)),0,$l-2);

return $coupon;
}
$couponcode= coupon(10);

//mail codes

ini_set('display_errors',1);
error_reporting(E_ALL);
$from="info@".$_SERVER['SERVER_NAME'];
$headers="MIME-VERSION:1.0\r\n"
  ."Content-Type:text/plain; Charset=utf-8\r\n"
  ."Content-Transfer-Encoding: 8bit\r\n"
  ."From:=?UTF-8?B?".base64_encode($name)."?>=<$from>\r\n"
  ."X-Mailer:PHP/".phpversion();
 $to="$email";
$subject="Roi Designs and Construction Send Coupon Code";
$message="Coupon Code:$couponcode.";
$headers="From:".$from;
mail($to,$subject,$message,$headers);

//database

$sql="INSERT INTO registercp(name,gender,email,city,type,phone,couponcode,status) VALUES (?,?,?,?,?,?,?,?)";
$stmtinsert=$db->prepare($sql);
$result=$stmtinsert->execute([$name,$gender,$email,$city,$type,$phone,$couponcode,"1"]);
if($result)
{
   $errors[]= "successfully saved.";
   header("refresh: 1; url = http://www.google.com/"); 
}
else
{
$errors[]="There Were errors While saving the data.!";
}
}

?> 

</div>
   <div class="container">
   <div class="row col-md-6 col-md-offset-3">
        <div class="panel panel-primary">
          <div class="panel-heading text-center">
  <h1>Registration Form</h1>
      </div>
  <div class="panel-body">
  <form action="" method="post">
  <div class="messages">
<?php if($errors) {
foreach ($errors as $key ) {
echo '<div class="alert alert-warning"  class="close" role="alert" data-dismiss="alert" aria-label="close"  >
<i class="glyphicon glyphicon-exclamation-sign"></i>
'.$key.'</div>';
}
} ?>
</div>
<hr class="mb-3">
  <label for="Name"><b>Name</b></label>
  <input class="form-control" id="name" type="text" name="name" required><br/>
  
  <label for="email"><b>email</b></label>
  <input class="form-control" id="email" type="text" name="email" required><br/>
  
  <label for="gender"><b>Gender</b></label>
  <div>
                  <label for="male" class="radio-inline"
                    ><input
                      type="radio"
                      name="gender"
                      value="m"
                      id="male"
                    />&nbsp;&nbsp;Male</label
                  >
                  <label for="female" class="radio-inline"
                    ><input
                      type="radio"
                      name="gender"
                      value="f"
                      id="female"
                    />&nbsp;&nbsp;Female</label
                  >
                  <label for="others" class="radio-inline"
                    ><input
                      type="radio"
                      name="gender"
                      value="o"
                      id="others"
                    />&nbsp;&nbsp;Others</label
                  >
  </div><br/>
  
  <label for="city"><b>City</b></label>
  <input class="form-control" type="text" id="city" name="city" required><br/>
  
  <label for="type"><b>Type of Services</b></label>
         <select name = "type" class="form-control">
  <option value="none" selected disabled hidden> 
                            Select an Option 
                     </option> 
                     <option value = "Construction">Construction</option>
                     <option value = "Design Work">Design Work</option>
                     <option value = "Supervision">Supervision</option>
                     
                  </select required>  
  <br/>
  <label for="phone"><b>Phone</b></label>
  <input class="form-control" type="text" id="phone" name="phone" required><br/>
  <hr class="mb-3">
  <div class="col-lg-12 text-center">
  <input type="submit" name="create"  value="submit" class="btn btn-lg btn-success">
  </div>
  </form>
  </div>
  <div class="panel-footer text-right">
            <small>&copy; Roi Design & Construction</small>
          </div>
  </div>
  </div>
  </div>
   </body>
</html>
<script>
if ( window.history.replaceState ) {
  window.history.replaceState( null, null, window.location.href );
}
</script>

dailyaspirants.com



Output:-

dailyaspirants.com




In this step we get the coupon code  and Automatically send to Gmail Account.
That's all, this is PHP - Generate Coupon Code using PDO Database and coupon code  send to Gmail . You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.
Previous Post Next Post