MySQL query or condition to fetch "1970-01-01" date value in the table cell |
MySQL query or condition to fetch "1970-01-01" date value in the table cell |
shankar from vizag |
Dec 9 2023, 08:33 PM
Post
#1
|
Advanced Member Group: Members Posts: 202 Joined: 18-June 13 Member No.: 19,316 |
Greetings
I have a mysql table with date column cell. At the time input from the php page, if a date type input left blank which is saving 1970-01-01 as the cell value. When I use select query to display records, those particular fields displaying 1970-01-01. Could anyone help me either in saving the empty date field as 1970-01-01 as blank or a condition to display a blank field in the php page by the select query when the cell value contains 1970-01-01. Regards shankar |
CharlesEF |
Dec 24 2023, 01:13 AM
Post
#2
|
Programming Fanatic Group: Members Posts: 1,981 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
Try this and let me know about error messages.
CODE <?php [code]session_start(); $con=mysqli_connect("localhost", "root", "", "test"); if(isset($_POST['save'])) { $name=$_POST['name']; $dob=empty($_POST['dob']) ? null : "'" . $_POST['dob'] . "'"; $query="INSERT INTO date(name,date) VALUES('$name', $dob)"; $query_run = mysqli_query($con, $query); $_SESSION['status']="Date values inserted"; } else { $_SESSION['status']="Date values Inserting failed"; } ?> |
shankar from vizag |
Dec 24 2023, 10:47 PM
Post
#3
|
Advanced Member Group: Members Posts: 202 Joined: 18-June 13 Member No.: 19,316 |
Try this and let me know about error messages. CODE <?php [code]session_start(); $con=mysqli_connect("localhost", "root", "", "test"); if(isset($_POST['save'])) { $name=$_POST['name']; $dob=empty($_POST['dob']) ? null : "'" . $_POST['dob'] . "'"; $query="INSERT INTO date(name,date) VALUES('$name', $dob)"; $query_run = mysqli_query($con, $query); $_SESSION['status']="Date values inserted"; } else { $_SESSION['status']="Date values Inserting failed"; } ?> Good Morning Chales Let me first greet you Christmas wishes. When I use the above code, its working fine when name and date input values are given. But it is not working when date column left empty. Regards Shankar This post has been edited by shankar from vizag: Dec 24 2023, 10:49 PM |
shankar from vizag |
Dec 25 2023, 04:06 AM
Post
#4
|
Advanced Member Group: Members Posts: 202 Joined: 18-June 13 Member No.: 19,316 |
Try this and let me know about error messages. CODE <?php [code]session_start(); $con=mysqli_connect("localhost", "root", "", "test"); if(isset($_POST['save'])) { $name=$_POST['name']; $dob=empty($_POST['dob']) ? null : "'" . $_POST['dob'] . "'"; $query="INSERT INTO date(name,date) VALUES('$name', $dob)"; $query_run = mysqli_query($con, $query); $_SESSION['status']="Date values inserted"; } else { $_SESSION['status']="Date values Inserting failed"; } ?> if I try the following line $dob=empty($_POST['dob']) ? null : $_POST['dob']; empty cell is saving with 00-00-0000. Good Morning Chales Let me first greet you Christmas wishes. When I use the above code, its working fine when name and date input values are given. But it is not working when date column left empty. Regards Shankar I also tried the following $con=mysqli_connect("localhost", "root", "", "test"); if(isset($_POST['save'])) { $name=$_POST['name']; if(empty($_POST['dob'])){ $dob=NULL; } else{ $dob=$_POST['dob']; } which also saving 00-00-0000 regards Finally, <?php session_start(); $pdo = new PDO('mysql:host=localhost;dbname=test', 'root', ''); if(isset($_POST['save'])) { $name=$_POST['name']; $dob=$_POST['dob']; $sql = "INSERT INTO date (name, date) VALUES (?, ?)"; $stmt = $pdo->prepare($sql); $stmt->execute([$name, $dob === '' ? NULL : $dob]); $_SESSION['status']="Date values inserted"; } else { $_SESSION['status']="Date values Inserting failed"; } ?> with this change its now working. Thank you Charles for your guidance. But I want to know how can we achieve the same result with conventional mysqli coding instead of PDO application ? Regards This post has been edited by shankar from vizag: Dec 25 2023, 04:47 AM |
Lo-Fi Version | Time is now: 26th May 2024 - 06:33 AM |