<?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>
Follow as in image.
-- 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;
$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);
Process to insert data and save as csv file
<?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);
Check project folder the txt file will be created user_data.CSV filename.