<?php include("db_connect.php"); header('Content-type: text/html; charset=utf-8'); if (isset($_POST["add"])) { $sql = mysql_query("INSERT INTO `message` (`name`, `surname`, `patronymic`, `date`, `text`) VALUES ('".$_POST['name']."','".$_POST['surname']."','".$_POST['patronymic']."','".$_POST['date']."','".$_POST['text']."')"); } ?> <!DOCTYPE html> <html lang="ru"> <head> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Bootstrap Template</title> <link href="css/bootstrap.css" rel="stylesheet"> <link rel="stylesheet" type="text/css" href="css/style.css"> <link href="https://fonts.googleapis.com/css?family=Martel|Rubik" rel="stylesheet"> </head> <body> <header> <div class="container"> <div class="row"> <form action="index.php" method="POST" class="col-lg-6" role="form"> <div class="form-group"> <label for="surname">Фамилия</label> <input type="name" class="form-control qwerty" name="surname" id="surname"> </div> <div class="form-group"> <label for="name">Имя</label> <input type="name" class="form-control qwerty" name="name" id="name"> </div> <div class="form-group"> <label for="patronymic">Отчетсво</label> <input type="name" class="form-control qwerty" name="patronymic" id="patronymic"> </div> <div class="form-group"> <label for="date">Дата рождения</label> <input type="date" class="form-control qwerty" name="date" id="date" placeholder="Дата"> </div> <div class="form-group"> <label for="text">Биография</label> <textarea name="text" class="form-control" rows="7" id="text"></textarea> </div> <button type="submit" name="add" class="btn btn-primary">Добавить</button> </form> </div> <hr /> <table class="table"> <thead> <tr> <th class="col-md-1">ID</th> <th class="col-md-3" >Дата рождения</th> <th class="col-md-2">Фамилия</th> <th class="col-md-2">Имя</th> <th class="col-md-2">Отчество</th> <th class="col-md-2">Биография</th> </tr> </thead> <tbody> <?php $result=mysql_query("SELECT id,date,surname,name,patronymic,text FROM message"); while ($row=mysql_fetch_array($result)) { // выводим данные echo "<tr>\n\t<td>".$row["id"]."</td>"."\n\t"."<td>"."".$row["date"]." </td>"."\n\t"."<td>"."".$row["surname"]."</td>"."\n\t"."<td>"."".$row ["name"]."</td>"."\n\t"."<td>"."".$row["patronymic"]."</td>"."\n\t"."<td> "."".$row["text"]."</td>"."\n\t"."</tr>"."\n"; } mysql_close(); ?> </tbody> </table> </div> </header> <script src="js/bootstrap.js"></script> </body> </html> |
2 answers
MySQL has a handy date and time query function: DATE_FORMAT (). In the example below, the date will be submitted in the form DD.MM.YYYY
% d - Day of the month, 2 digits with leading zero
% m - Month ordinal number with leading zero
% Y - year
Between them there can be almost any characters, I put a dot:
$result = mysql_query("SELECT id, DATE_FORMAT(`date`, "%d.%m.%Y"), surname, name, patronymic, text FROM message"); You can read more here.
Another option: date () operator.
Converts a date to text.
Description here
- Thank you very much - Artem Fomkin
|
Sobsno, here:
$result=mysql_query( "SELECT id,DATE_FORMAT(`date`,'%d.%m.%Y') as `date`,surname,name,patronymic,text FROM message" ); It would also be nice to rewrite your db_connect.php to a mysqli or PDO connection.
- Thank you very much)) - Artem Fomkin
- Do you know by any chance how to sort the table by title and that only a specific column is sorted, I can’t find a solution all day - Artem Fomkin
- @Artemfomkin, you can try to formulate the question somehow clearly and preferably with an example. And, as I understand it, it makes sense to put it in a separate question. - Alexander Belinsky
- When clicking on the header of the corresponding column, the data in the table should be sorted by increasing / decreasing information in a specific column - Artem Fomkin
- There are 6 columns id, date of birth, last name, first name, middle name, and when I click on the id column, only the id column should be sorted in ascending / descending order, the others do not change. Probyval through DataTables but it sorts all values. - Artem Fomkin
|