Store date function

How to insert date from form to database like phpMyAdmin and then show on page while selecting from database?

<td class="text-center"><?php echo date("d/m/Y") ?></td>
Nikhil DangeWeb Developer InternAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rgranlundCommented:
I think this is right but I have not tested it.


<?php 
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

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

$id = '';
//  Set ID to whatever you need it to be.

$query = "SELECT date FROM tablename where ID = $id"; 
$result = $conn->query($query);

if ($result->num_rows > 0) {

    // output data of each row
    while($row = $result->fetch_assoc()) {

$date  = date('d/m/Y', strtotime( $row["date"];));

        echo '<td class="text-center">'.$date. '</td>';
    }
} else {
    echo "No Results";
}
$conn->close();

?>

Open in new window

3

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mohan singhWeb developerCommented:
If you want to display date from database

follow the step:
1 . Fetch your date from database and fire a query 
<?php 
$sql = mysql_query($db_config, "SELECT date FROM table";
$row = mysql_fetch_array($sql);

2. Store your data in a variable 
$date = $row['date']; //now $date fetch your data from your database 

?>

<!--And now you can display your date in any Input field like date, textarea , text etc -->
3. Print or echo your variable in input field 
<input type='date'  value="<?php echo $date; ?>" /><!-- in this field your date will display --> 

Open in new window

2
NerdsOfTechTechnology ScientistCommented:
If the date field is actually called date, date is a reserved word so you will need to add ticks around the field name.

SELECT `date` FROM tablename

Open in new window


Also, use mysqli or PDO for the connection as mysql is deprecated.

Insert date from form to database
You would pull this from $_POST['fieldname'] and sanitize it.
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

NerdsOfTechTechnology ScientistCommented:
I recommend @rgranlund's solution as best, as it uses mysqli.

With these changes it should work:

$query = "SELECT date FROM tablename where ID = $id"; 

Open in new window

to:
$query = "SELECT `datefieldname` FROM tablename where ID = $id"; 

Open in new window


$date  = date('d/m/Y', strtotime( $row["date"];));

Open in new window

to:
$date  = date('d/m/Y', strtotime( $row["date"]));

Open in new window

0
NerdsOfTechTechnology ScientistCommented:
Besides a few syntax issues, @rgranlund's solution uses mysqli and has the appropriate error checking and looping (for multiple records); therefore, I vote that this expert gets full points.
0
Nikhil DangeWeb Developer InternAuthor Commented:
It works
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
CSS

From novice to tech pro — start learning today.