sfia Posted April 22, 2020 Share Posted April 22, 2020 $value0 = $_POST['date1']; $value1 = $_POST['date2']; $value2 = $_POST['date3']; $sql = "INSERT INTO Datetable (startdate,enddate, total) VALUES ('$value0','$value1', '$value2')" ; $result = mysqli_query($sql); Hi, I was wondering is it possible to not insert values in MySQL if form entry is left blank? Right now if I dont enter any values in form for my dates than MySQL entry shows 0000-00-00 but I need it not to show anything. Quote Link to comment Share on other sites More sharing options...
requinix Posted April 22, 2020 Share Posted April 22, 2020 Yes. You would accomplish that by writing code to prevent it from inserting values into MySQL if they are left blank. Also, you need to switch to prepared statements now. You can continue using mysqli if you wish, but much of the PHP community prefers PDO because it is a little easier to use. Quote Link to comment Share on other sites More sharing options...
sfia Posted April 23, 2020 Author Share Posted April 23, 2020 $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $form=$_POST; $startdate =$form[ 'date1' ]; $enddate=$form[ 'date2' ]; $total=$form[ 'date3' ]; $sql = "INSERT INTO Datetable (startdate, enddate, total) VALUES (:startdate, :enddate, :total)"; $query= $conn->prepare( $sql ); $query->execute( array( ':startdate'=>$startdate, ':enddate'=>$enddate, ':total'=>$total ) ); I converted mysqli as suggested, and now using PDO and it seems to be working, but I'm completely stumped on how can i create a code to prevent empty values in form for date to pass to database as 0000-00-00. Could someone please help me with that? Or just point to similar thread I can read up on how to make that code? Also what is your opinion on PDO being slower and not as robust as mysqli? Thank you. Quote Link to comment Share on other sites More sharing options...
sfia Posted April 23, 2020 Author Share Posted April 23, 2020 if (validDate($_POST['date2'], $format = 'Y-m-d')){ $date2 = date('Y-m-d', strtotime(trim($_POST['date2']))); } else { $date2 = NULL; } Like something like this? IDK that doesnt seem to work Quote Link to comment Share on other sites More sharing options...
Barand Posted April 23, 2020 Share Posted April 23, 2020 if (form entry is not blank) insert record else don't endif Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted April 23, 2020 Share Posted April 23, 2020 9 hours ago, sfia said: Like something like this? IDK that doesnt seem to work Is validDate() defined in your script? Note that PHP has a built-in date checker. More information can be found here:https://www.php.net/manual/en/function.checkdate.php If you scroll down to the "User Contributed Notes" section, there is a user-defined function validateDate() that you could experiment with. Quote Link to comment Share on other sites More sharing options...
sfia Posted April 23, 2020 Author Share Posted April 23, 2020 I followed that link and used validateDate function below and now my script works!. thank you. Appreciate the help. Just trying to learn some of PHP. function validateDate($date, $format = 'Y-m-d') { $d = DateTime::createFromFormat($format, $date); return $d && $d->format($format) == $date; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.