- how to insert date in mysql using php?
- Insert date Into MySQL Using PHP
- CREATE TABLE : members
- Alternative Code in (PDO)
- how to InsertDate in mysql using php form
- Related posts:
- Creating a PHP date in the format for a SQL Timestamp insert
- Note: You might not need to create a PHP date
- Creating a PHP timestamp variable
- A Drupal 7 SQL INSERT with Timestamp example
- Getting a timestamp for some other date and time
- PHP SQL Timestamp inserts
- Display Data in an HTML Table Using PHP & MySQL
- Steps to Display Data From MySQL Database with PHP
- 1. Connect PHP to MySQL Database
- 2. Insert Data Into PHPMyAdmin Table
- 3. Fetch Data From MySQL Table
- 4. Display Data in HTML Table
- 5. Test Yourself to insert data
- More Methods to display Data in HTML Table with PHP
- Display Data using MySQLi Procedure
- Display Data Using MySQLi Object-Oriented
- Display Data Using PDO
- Display Data Using Prepared Statement
- Related posts:
- Date Format in PHP for Insertion in MySQL
- date() in PHP
- Syntax of date()
- Parameters
- date_format() in PHP
- Syntax of date_format()
- Parameters
- Related Article - PHP MySQL
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
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;
Alternative Code in (PDO)
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 . "
" . $e->getMessage(); > $conn = null; ?>
how to InsertDate in mysql using php form
First name:
Last name:
Borth Date:
Email Id:
Related posts:
Creating a PHP date in the format for a SQL Timestamp insert
PHP date/time FAQ: How do I create a date in the proper format to insert a SQL Timestamp field into a SQL database?
Note: You might not need to create a PHP date
First off, you may not need to create a date in PHP like this. If you’re using plain old PHP and a database like MySQL, you can use the SQL now() function to insert data into a SQL timestamp field like this:
INSERT INTO projects (user_id, name, last_updated, date_created) VALUES (5, 'alvin', now(), now());
I just tested this with PHP and MySQL, and it works fine. So that’s one way to populate a SQL timestamp field in a SQL INSERT query.
Creating a PHP timestamp variable
However, if you want to do this all in PHP (or need to, depending on what framework you’re working with), you can get the current date and time in the proper format using just PHP, like this:
If you print this out, your $timestamp field will now contain contents like this:
You can then use this formatted timestamp string in a PHP MySQL insert.
Note: Thanks to the commenters below who suggest using H:i:s instead of G:i:s .
A Drupal 7 SQL INSERT with Timestamp example
Although this isn’t a standard off-the-shelf PHP/MySQL INSERT statement, here’s what a SQL INSERT query looks like when I use this with Drupal 7:
$project = new stdClass(); $project->user_id = get_user_id(); $project->project_count_type = $form_state['values']['type']; $project->name = $form_state['values']['name']; $project->description = $form_state['values']['description']; # get the current time in the proper format for a sql timestamp field $timestamp = date('Y-m-d H:i:s'); # new drupal 7 style insert $id = db_insert('projects') ->fields(array( 'user_id' => $project->user_id, 'project_count_type' => $project->project_count_type, 'name' => $project->name, 'description' => $project->description, 'last_updated' => $timestamp, 'date_created' => $timestamp )) ->execute();
As you can see in the lines I’ve made bold, I’m inserting my PHP timestamp variable into two SQL fields.
Getting a timestamp for some other date and time
Note that the PHP date function defaults to the current date and time, which is exactly what I need for my purposes here. If you need to create a formatted timestamp field for some other date and time, you can do that something like this:
$timestamp = date('Y-m-d H:i:s', mktime(0, 0, 0, 7, 1, 2000));
Here are some other PHP mktime examples:
$tomorrow = mktime(0, 0, 0, date("m") , date("d")+1, date("Y")); $lastmonth = mktime(0, 0, 0, date("m")-1, date("d"), date("Y")); $nextyear = mktime(0, 0, 0, date("m"), date("d"), date("Y")+1);
I pulled those examples from the PHP date page. Please see that page for more information on creating other dates and times (I’m mostly just worried about «now» at this moment).
PHP SQL Timestamp inserts
I hope these timestamp examples have been helpful. As you’ve seen, you can generally just use the SQL ‘NOW()’ function to insert into a SQL timestamp field, but if that doesn’t work for some reason, you can also create a timestamp field in the proper format using just PHP and the date function.
Display Data in an HTML Table Using PHP & MySQL
Hello Developer, In this tutorial, You will learn to display data in an HTML table using PHP and MySQL. Even You will know more to fetch new records from the database and show them in the tabular format using MySQLi procedure, MySQLi Object-oriented, PDO & prepared statement with a new concept & an example.
Steps to Display Data From MySQL Database with PHP
In this step, you will learn to display data from the database dynamically with an advanced coding concept that will help you to improve your coding skills for writing smart & standard code in PHP.
Before getting started, make sure that the local server (xamp/wamp) must be installed in your system and start it if it is not being started already.
Learn Also –
You can test yourself to display data with the following folder structure –
codingstatus/ |__database.php |__table.php |__developers.php |
1. Connect PHP to MySQL Database
You can use the following database connection query to connect PHP to the MySQL database
- $hostName – It contains host name
- $userName – It contains database username
- $password – It contains database password
- $databaseName – It contains database name.
connect_error) < die("Connection failed: " . $conn->connect_error); > ?>
2. Insert Data Into PHPMyAdmin Table
Before displaying data, you have to insert data into the Database. If you have not already done it and don’t know about it, then you can learn it through the following URL –
3. Fetch Data From MySQL Table
Now, You have to fetch data from the MySQL table. So, just follow these points –
- First of all, include a database connection file database.php
- assign $conn to a new variable $db and table name to another variable $table
- Define columns name in an indexed array and assign them to the $columns
- Also, assign fetch_data() function to the $fetchData
fetch_data() – This function accepts three parameters like $db, $table & $column and It contains MySQLi SELECT query that will return records in an array format by fetching from the database
elseif (empty($columns) || !is_array($columns)) < $msg="columns Name must be defined in an indexed array"; >elseif(empty($tableName))< $msg= "Table Name is empty"; >else< $columnName = implode(", ", $columns); $query = "SELECT ".$columnName." FROM $tableName"." ORDER BY id DESC"; $result = $db->query($query); if($result== true)< if ($result->num_rows > 0) < $row= mysqli_fetch_all($result, MYSQLI_ASSOC); $msg= $row; >else < $msg= "No Data Found"; >>else < $msg= mysqli_error($db); >> return $msg; > ?>
4. Display Data in HTML Table
Now, You have to display data in the HTML table. So, you have to follow these points –
- First of all, Include PHP script file developers.php
- Create an HTML table using Bootsrap 4
- Check $fetchData is an array or not with if & else condition
- Then apply foreach loop to the $fetchData
- After that print the required data in the table
S.N Full Name Gender Mobile Number Address City State >else < ?> ?>
5. Test Yourself to insert data
After Implementing previous steps, You can test it yourself to open the following URL in your web browser
https://localhost/codingstatus/table.php
More Methods to display Data in HTML Table with PHP
Now, You should know more methods to display data in the database from the next step. After learning those methods, you can use one of them in your project.
From the next step, I have shared only the source code without a custom function. So that you can directly paste it into your project where you need to implement it.
Display Data using MySQLi Procedure
Display data with MySQLi Procedure
0) < $sn=1; while($data = mysqli_fetch_assoc($result)) < ?> S.N Full Name Gender Mobile No Address City State > else < ?> ?> No data found Display Data Using MySQLi Object-Oriented
Display data with MySQLi Object-Oriented
query($query); ?>num_rows > 0) < $sn=1; while($data = $result->fetch_assoc()) < ?> S.N Full Name Gender Mobile No Address City State > else < ?> ?> No data found Display Data Using PDO
Connect Database with PDO
setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); try < $query = "SELECT fullName, gender, email, mobile, address, city, state FROM developers"; $result = $conn->query($query); ?>
Display Data Using Prepared Statement
Display data using Prepared Statement with MySQLi –
prepare($query); $prepared->execute(); $result = $prepared->get_result(); ?>num_rows > 0) < $sn=1; while($data = $result->fetch_assoc()) < ?> S.N Full Name Gender Mobile No Address City State > else < ?> ?> No data found Display data using Prepared Statement with PDO –
setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); try < $query = "SELECT fullName, gender, email, mobile, address, city, state FROM developers"; $prepared = $conn->prepare($query); $prepared->execute(); $result = $prepared -> fetchAll(PDO::FETCH_ASSOC); ?>
Related posts:
Date Format in PHP for Insertion in MySQL
- date() in PHP
- date_format() in PHP
MySQL is an RDBMS database intended to store relational data. It supports various data types, Date being one of them. As MySQL supports only particular date formats, you need to format the dates before inserting dates into the DB; otherwise, the DB will throw an error.
This article will introduce how to format dates in PHP before inserting them into a MySQL DB.
- DATE : YYYY-MM-DD It only stores the date without time in the range of 1000-01-01 to 9999-12-31 . For example, 2021-10-28 .
- DATETIME : YYYY-MM-DD HH:MI:SS . It stores the date with time in the range of 1000-01-01 00:00:00 to 9999-12-31 23:59:59 . For example, 2021-10-28 10:30:24
- TIMESTAMP : YYYY-MM-DD HH:MI:SS . It stores the date with time in the range of 1970-01-01 00:00:01 to 2038-01-09 03:14:17 . For example, 2021-10-28 10:30:24
- TIME : HH:MI:SS . It stores the time without date in the range of -838:59:59 to 838:59:59 . For example, 10:30:24
- YEAR : YYYY or YY . It stores the year either 4 digits or 2 digits in the range of 70(1970)-69(2069) for 2 digits and 1901-2155 | 0000 for 4 digits. For example, 2021 .
Before learning the solution, let’s understand the concept of date() .
date() in PHP
It is a built-in PHP function that returns the formatted date string.
Syntax of date()
Parameters
- d - The day of the month in the range of 01 to 31
- m - A numeric representation of a month in the range of 01 to 12
- Y - A four-digit representation of a year
- y - A two-digit representation of a year
- H - A two-digit representation of an hour in the range of 00 to 23
- i - A two-digit representation of a minute in the range of 00 to 59
- s - A two-digit representation of a second in the range of 00 to 59
$timestamp : It is an optional parameter that specifies a Unix timestamp in integer format. If not provided, a default value will be taken as the current local time.
php $formated_DATETIME = date('Y-m-d H:i:s'); echo $formated_DATETIME. "
"; // 2021-10-27 14:02:16 $formated_DATE = date('Y-m-d'); echo $formated_DATE. "
"; // 2021-10-27 $formated_TIME = date('H:i:s'); echo $formated_TIME. "
"; //14:03:57 $formated_YEAR = date('Y'); echo $formated_YEAR. "
"; // 2021 ?>
2021-10-27 14:02:16 2021-10-27 14:03:57 2021
date_format() in PHP
It is a built-in PHP function that takes the DateTime object as input and returns the formatted date string.
Syntax of date_format()
date_format($dateObject, $format);
Parameters
$dateObject : It is a mandatory parameter that specifies a DateTime object.
- d - The day of the month in the range of 01 to 31
- m - A numeric representation of a month in the range of 01 to 12
- Y - A four-digit representation of a year
- y - A two-digit representation of a year
- H - A two-digit representation of an hour in the range of 00 to 23
- i - A two-digit representation of a minute in the range of 00 to 59
- s - A two-digit representation of a second in the range of 00 to 59
php $date = date_create("2021/10/27"); $formated_DATETIME = date_format($date, 'Y-m-d H:i:s'); echo $formated_DATETIME. "
"; $formated_DATE = date_format($date, 'Y-m-d'); echo $formated_DATE. "
"; $formated_TIME = date_format($date, 'H:i:s'); echo $formated_TIME. "
"; $formated_YEAR = date_format($date, 'Y'); echo $formated_YEAR. "
"; ?>
2021-10-27 00:00:00 2021-10-27 00:00:00 2021
Shraddha is a JavaScript nerd that utilises it for everything from experimenting to assisting individuals and businesses with day-to-day operations and business growth. She is a writer, chef, and computer programmer. As a senior MEAN/MERN stack developer and project manager with more than 4 years of experience in this sector, she now handles multiple projects. She has been producing technical writing for at least a year and a half. She enjoys coming up with fresh, innovative ideas.