Home » how to insert date in mysql using php?

how to insert date in mysql using php?

For Insert date Into MySQL in ‘dd-mm-yyyy’ format Using PHP first of all I have to make a simple table in data base like bellow example for InsertDate current date in mysql using php.

Insert date Into MySQL Using PHP

how to InsertDate date in mysql using php?
Here i am using main three file for InsertDate data in MySQL Example:

  • database.php:firs of all you can database For connecting & testing data base
  • InsertDate.php:and then for fetching the values from the members
  • process.php:A PHP server side file that process the get data request
Read Also:  How to change date format in PHP?

Also Read: How to convert date format in PHP?

CREATE TABLE : members

CREATE TABLE `members` (
	`userid` int(8) NOT NULL,
	`member_name` varchar(55) NOT NULL,
	`profile_name` varchar(55) NOT NULL,
	`birth_date` varchar(55) NOT NULL,
	`email` varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

database.php

<?php
$servername='localhost';
$username='root';
$password='';
$dbname = "crud";
$conn=mysqli_connect($servername,$username,$password,"$dbname");
if(!$conn){
   die('Could not Connect My Sql:' .mysql_error());
}
?>

Also Read: Simple PHP Convert Date Format Examples

Alternative Code in (PDO)

<?php
$servername='localhost';
$username='root';
$password='';
$dbname = "crud";
try {
    $member_name = $_POST['member_name'];
    $profile_name = $_POST['profile_name'];
    $birth_date = $_POST['birth_date'];
    $email = $_POST['email'];
    date_default_timezone_set("Asia/Calcutta");
    $insertdate = date("Y-m-d H:i:s");
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    /* set the PDO error mode to exception */
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
   $sql = "INSERT INTO members (member_name,profile_name,birth_date,email,datetime)
    VALUES ('$member_name', '$profile_name','$birth_date','$email','$insertdate')";
    $conn->exec($sql);
    echo "New record created successfully";
    }
catch(PDOException $e)
    {

    	echo $sql . "<br>" . $e->getMessage();
    }

$conn = null;
?>

InsertDate.php

<!DOCTYPE html>
<html>
  <body>
  <h3>how to InsertDate in mysql using php form</h3>
	<form method="post" action="process.php">
		First name:<br>
		<input type="text" name="member_name">
		<br>
		Last name:<br>
		<input type="text" name="profile_name">
		<br>
		Borth Date:<br>
		<input type="text" name="birth_date">
		<br>
		Email Id:<br>
		<input type="email" name="email">
		<br><br>
		<input type="submit" name="save" value="submit">
	</form>
  </body>
</html>

Also Read: How to change date format in PHP?

Read Also:  Laravel 5/6/7 Carbon date format Example

process.php

<?php
include_once 'database.php';
if(isset($_POST['save']))
{	 
	 $member_name = $_POST['member_name'];
	 $profile_name = $_POST['profile_name'];
	 $birth_date = $_POST['birth_date'];
	 $email = $_POST['email'];
	 $sql = "INSERT INTO members (member_name,profile_name,birth_date,email)
	 VALUES ('$member_name','$profile_name','$birth_date','$email')";
	 if (mysqli_query($conn, $sql)) {
		echo "New record created successfully !";
	 } else {
		echo "Error: " . $sql . "
" . mysqli_error($conn);
	 }
	 mysqli_close($conn);
}
?>


Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  reactjs open link in new tab Example Tutorial
  2. Read Also:  PHP Check If mail() function is enable
  3. Read Also:  Base64 Encode Decode string using Javascript
  4. Read Also:  PHP Laravel 6 Change Date Format example
  5. Read Also:  Laravel Check Duplicate Records before insert
  6. Read Also:  Laravel Free charts and graphs for HTML5, JQuery and Javascript
  7. Read Also:  Time-Range-Picker jquery time range picker example - jQueryUI TimeRangePicker
  8. Read Also:  how to skip a line in python | python skip lines starting with #
  9. Read Also:  UI Grid Filtering using Angular Example
  10. Read Also:  Laravel 6 change date format example Tutorial

Leave a Comment

Your email address will not be published. Required fields are marked *