How to write csv file on form submit

Project : in this project we will learn how to write csv file by using form submit.

index.php
                            
                              <?php include("create-csv.php"); ?>
                                <!DOCTYPE html>
                                <html>
                                <head>
                                 <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
                                </head>
                                <body>
                                  <div class="container "><br>
                                      <div class="row mt-2">
                                          <div class="col-md-6 offset-3 border p-4" style="background: #f5f6fa;"> 
                                            <div class="w-100 text-center"><?php echo $error; ?><?php echo $msg; ?></div>  
                                             <form method="POST" action="index.php" enctype="multipart/form-data">
                                              <h3 class="rounded bg-primary w-100 p-2 text-white text-center">Submit and Create CSV File</h3>   
                                              <div class="form-group">
                                                <label for="fullname">Fullname</label>
                                                <input type="text" class="form-control" name="fname" id="fullname">
                                              </div>
                                              <div class="form-group">
                                                <label for="mobile">Mobile</label>
                                                <input type="text" class="form-control" name="mobile" id="mobile">
                                              </div>
                                              <div class="form-group">
                                                <label for="email">Email</label>
                                                <input type="text" class="form-control" name="email" id="email">
                                              </div>
                                              <div class="form-group">
                                                <label for="city">City</label>
                                                <input type="text" class="form-control" name="city" id="city">
                                              </div>
                                              <div class="form-group">
                                                <label for="country">Country</label>
                                                <input type="text" class="form-control" name="country" id="country">
                                              </div>
                                              <div class="form-group">
                                                <button type="submit" name="submit" class="btn btn-success">Submit & Save CSV File</button>
                                                <a href="upload-csv.php" class="btn btn-info float-right">Upload CSV</a>
                                              </div>
                                          </form>
                                         </div>
                                      </div>         
                                  </div>
                                </body>
                                </html>
                            
                          

Submit Form

Submit Data Form
PHP

Database and table name

You have to create database and table in phpMyAdmin.

Follow as in image.

PHP>

Create table in databse

Table in database
                              
                                -- Table structure for table `datas`
                                CREATE TABLE `datas` (
                                  `id` int(11) NOT NULL  PRIMARY KEY AUTO_INCREMENT,
                                  `fullname` varchar(100) NOT NULL,
                                  `mobile` varchar(20) NOT NULL,
                                  `email` varchar(100) NOT NULL,
                                  `city` varchar(100) NOT NULL,
                                  `country` varchar(100) NOT NULL,
                                ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
                              
                          

Create database connection

Create connection with database
myproject/dbcon.php
dbcon.php
    
        $dbhost ="localhost";
        $dbuser ="root";
        $dbpswd ="";
        $dbname ="dbdemo";
        $dbcon= mysqli_connect($dbhost,$dbuser,$dbpswd,$dbname);
        if($dbcon){
            //echo "Connected............!!!";
            return $dbcon;
            exit();
        }else{
            die("Connection failed ? ? ?". mysqli_error($dbcon));
        }
        mysqli_close($dbcon);
    

Insert data & Save as CSV File

Process to insert data and save as csv file

create-csv.php
                              
                                  <?php  
                                  include('dbcon.php');
                                  $error=''; $msg='';
                                  if(isset($_POST['submit'])){
                                    $fname= ucwords($_POST['fname']);
                                    $mobile= $_POST['mobile'];
                                    $email= $_POST['email'];
                                    $city= ucfirst($_POST['city']);
                                    $country= ucfirst($_POST['country']);
                                        if(empty($fname) || empty($mobile) || empty($email)||empty($city)||empty($country)){
                                          $error = "<div class='alert alert-danger text-danger'>All the fields are required ?</div>";
                                        }else{
                                          $sql="INSERT INTO datas(fullname,mobile,email,city,country) VALUES('$fname','$mobile','$email','$city','$country')";
                                          $run = mysqli_query($dbcon,$sql);
                                          if($run){
                                            $file_open = fopen('user_data.csv','a');
                                              $save_data = [
                                                'fullname'=> $fname,
                                                'mobile'  => $mobile,
                                                'email'   => $email,
                                                'city'    => $city,
                                                'country' => $country,
                                              ];
                                            fputcsv($file_open, $save_data);
                                            $msg = "<div class='alert alert-success text-success'>The data has been submitted </div>";
                                            $fname =''; $mobile =''; $email =''; $city=''; $country='';
                                            fclose($file_open);
                                          }else{
                                            $error = "<div class='alert alert-danger text-danger'>The data is not submitted and save</div>";     
                                          }
                                        }
                                    }
                                  mysqli_close($dbcon);
                              
                          
Saved CSV File

Check project folder the txt file will be created user_data.CSV filename.

PHP

css folder

You must download and include bootstrap css source files for good interface OR You can add cdn link from bootstrap website.
myproject/css
bootstrap.min.css