Site icon T4Tutorials.com

How to Insert date value in database using PHP?

Insert date in database using PHP (MySQLi Procedural)

<?php
$servername = "localhost";
$username = "root";
$password = "";
$Database = "T4TutorialsDotCom";

// Create connection
$connection = mysqli_connect($servername, $username, $password, $Database);
// Check connection
if (!$connection) {
  die("Connection failed: " . mysqli_connect_error());
}

$sql = "INSERT INTO t4table (Name, Father_name, email)
VALUES ('shamil', 'Asad', 'shamil@t4tutorials.com')";

if (mysqli_query($connection, $sql)) {
  echo "New record created successfully";
} else {
  echo "Error: " . $sql . "<br>" . mysqli_error($connection);
}

mysqli_close($connection);
?>

Insert date in database using PHP (MySQLi Object oriented)

<?php
$servername = "localhost";
$username = "root";
$password = "";
$Database = "T4TutorialsDotCom";

// Create connection
$connection = new mysqli($servername, $username, $password, $Database);
// Check connection
if ($connection->connect_error) {
  die("Connection failed: " . $connection->connect_error);
}

$sql = "INSERT INTO t4table (Name, Father_name, email)
VALUES ('shamil', 'Asad', 'shamil@t4tutorials.com')";

if ($connection->query($sql) === TRUE) {
  echo "New record created successfully";
} else {
  echo "Error: " . $sql . "<br>" . $connection->error;
}

$connection->close();
?>

Insert date in database using PHP (PDO)

<?php
$servername = "localhost";
$username = "root";
$password = "";
$Database = "T4TutorialsDotCom";

try {
  $connection = new PDO("mysql:host=$servername;dbname=$Database", $username, $password);
  // set the PDO error mode to exception
  $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  $sql = "INSERT INTO T4Table (Name, Father_name, email)
  VALUES ('shamil', 'Asad', 'shamil@t4tutorials.com')";
  // use exec() because no results are returned
  $connection->exec($sql);
  echo "New record created successfully";
} catch(PAsadxception $e) {
  echo $sql . "<br>" . $e->getMessage();
}

$connection = null;
?>

Download PHP code with database to Insert date in database

Other Similar Codes of date in PHP

  1. Simple Insert date value in database using PHP
  2. PHP Insert DATE in Database with datepicker
Exit mobile version