将日期值插入mysqli数据库 - 而不是发布值

时间:2017-05-10 09:57:20

标签: php database mysqli

嗨,请这不是一个重复的问题...尝试过前一个问题对我不起作用。

将问题发布日期输入到数据库。我在我的应用程序中有大约五个日期字段输入,但我只发布了date_of_birth,使用相同的概念,其余的帖子没有发布。

此镜头表明该值有效但后来在DB显示的是<0000-00-00

这是我的代码 的 HTML

           <div class="form-group col-sm-6 col-md-2">
                <label for="date_of_birth"><strong>DOB:</strong> </label>
                <input type="text" name="date_of_birth" class="form-control underline-input" value='<?php echo $date_of_birth; ?>' placeholder="YYYY-MM-DD">
            </div> 
            <div class="form-group col-sm-6 col-md-2">
                <label for="dob"><strong>DOB 2:</strong> </label>
                <input type="text" name="dob" class="form-control underline-input" value='<?php echo $dob; ?>' placeholder="YYYY-MM-DD">
            </div> 

PHP

        <?php

    // start session
    session_start();
    error_reporting(E_ALL); ini_set('display_errors', 1);

    // include connection
    require_once('include/connection.php');
    //   require_once('include/pdo-config.php');

    // if user is loggin, redirected to homepage
    if(isset($_SESSION['user_type'])){
    header('Location: index.php');
    }

            $userId = $_SESSION['user_id'];
            $userName = $_SESSION['user_name'];
                            //         $errors = array();
                            //         $data = array();
    $error = [] ;
    $status = "";
    if(isset($_POST['submit']))    {
     // recommended solution
          $firstname = trim($_POST['firstname']);
          $lastname = trim($_POST['lastname']);
          $date_of_birth = trim($_POST['date_of_birth']);
          $dob = trim($_POST['dob']);;                                                  
          $created_at = $_POST['created_at']; 

                        $created_at = date('Y-m-d');                                            
                        $dob = date('Y-m-d');                                           
                        $date_of_birth = date('Y-m-d');
                   if(!($stmt = $con->prepare("INSERT INTO task (firstname, lastname, date_of_birth, dob, created_at) 
                      VALUES (?,?,?,?,?)"))) {
                      echo "Prepare failed: (" . $con->errno . ")" . $con->error;
                  }

                  if(!$stmt->bind_param('sssss', $firstname, $lastname, $date_of_birth, $dob,  $created_at)){
                   echo "Binding paramaters failed:(" . $stmt->errno . ")" . $stmt->error;
                  }

                  if(!$stmt->execute()){
                   echo "Execute failed: (" . $stmt->errno .")" . $stmt->error;
                  }

            $stmt->close();

                  if($stmt) {
                    $status = '<div class="alert alert-success">New Task Added successfully!</div>';

                            }else{
                    $status = '<span class="alert alert-danger">Error in your query</div>';
                  }

                            $stmt->close();
      }                                     

也许你会有眼睛,我没有...尝试过不同角度没有运气。我也打开了我的错误报告,这是http://prntscr.com/f64zz4

的错误

更新 HTML

                <form name="task" role="task" id="task_form" data-parsley-validate="" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post" autocomplete='off'>

    <div class="row">
    <!--                                    <div class="form-group col-sm-6 col-md-2">
    <label for="id"><strong>Client ID:</strong> </label>
        <input type="text" name="id" class="form-control underline-input" id="input07" placeholder="01..." disabled>                                            
    </div>                                          -->
    <div class="form-group col-sm-6 col-md-2">
    <label for="firstname"><strong>First Name:</strong> </label>
    <input type="text" name="firstname" class="form-control underline-input" value='<?php echo $_POST['firstname']; ?>' placeholder="first name"
             data-parsley-trigger="change"
             data-parsley-range="[3, 15]"
             required-no>                                                                                                                                                                                                                                                                                                        
    </div>
    <div class="form-group col-sm-6 col-md-2">
    <label for="lastname"><strong>Last name:</strong> </label>
    <input type="text" name="lastname" class="form-control underline-input" value='<?php echo $_POST['lastname']; ?>' placeholder="last name"
             data-parsley-trigger="change"
             data-parsley-range="[3, 15]"
             required-no>
    </div>  

    <div class="form-group col-sm-6 col-md-2">
                <label for="date_of_birth"><strong>DOB:</strong> </label>
                <input type="text" name="date_of_birth" class="form-control underline-input" value='<?php echo $date_of_birth; ?>' placeholder="YYYY-MM-DD">
                </div> 
                <div class="form-group col-sm-6 col-md-2">
                <label for="dob"><strong>DOB 2:</strong> </label>
                <input type="text" name="dob" class="form-control underline-input" value='<?php echo $dob; ?>' placeholder="YYYY-MM-DD">
                </div> 

    </div>               

PHP

            if(isset($_POST['submit']))    {
            // recommended solution
                    $firstname = trim($_POST['firstname']);
                    $lastname = trim($_POST['lastname']);
            $date_of_birth = trim($_POST['date_of_birth']);
            $dob = trim($_POST['dob']);;                                                    
                    $created_at = $_POST['created_at']; 

                    $created_at = date('Y-m-d');                                            
                    $dob = date('Y-m-d');                                           
                    $date_of_birth = date('Y-m-d');
                 if(!($stmt = $con->prepare("INSERT INTO task (firstname, lastname, date_of_birth, dob, created_at) 
                                            VALUES (?,?,?,?,?)"))) {
                                            echo "Prepare failed: (" . $con->errno . ")" . $con->error;
            }   

数据库模式 http://prntscr.com/f65ihm

1 个答案:

答案 0 :(得分:0)

以下是如何做到这一点。我有用户mysqli prepared statement

日期将以您想要的相同格式插入。

这是MYSQ数据库表的图像

TABLE IMAGE

这是我的PHP和MYSQLI代码:

    <?php

    $link = new mysqli ('localhost','root','admin','demo1');
    if($link->connect_error){
        die ("connection failed".$link->connect_error);
    }

    if(isset($_POST['submit'])){
        $firstname = trim($_POST['fname']);
        $lastname = trim($_POST['lname']);
        $date_of_birth = trim($_POST['date_of_birth']);
        $dob = trim($_POST['dob']);
        $created_at = date('Y-m-d');

        $sql = $link->stmt_init();

        if($sql->prepare("INSERT INTO demodate (fname,lname,date_of_birth,dob,created_at) VALUES (?,?,?,?,?)")){
            $sql->bind_param('sssss',$firstname,$lastname,$date_of_birth,$dob,$created_at);
            if($sql->execute()){
                echo "Successfully inserted";
            }
            else
            {
                echo "Failed to inserted";
            }
        }
        else
        {
            echo "Error Inserting".$link->error;
        }
    }
?>
<html>
    <head>

    </head>
    <body>
        <form action="" method="post">
            <input type="text" name="fname" placeholder="Enter First Name...">
            <input type="text" name="lname" placeholder="Enter Last Name...">
            <input type="date" name="date_of_birth" placeholder="Enter Date of Birth YYYY/MM/DD...">
            <input type="date" name="dob" placeholder="Enter DOB YYYY/MM/DD...">
            <input type="submit" name="submit" value="submit">
        </form>
    </body>
</html>

希望我已经回答了你的问题,如果有什么需要回答你可以自由地问