PHP使用日期插入到mysql数据库中

时间:2014-06-10 11:38:39

标签: php mysql

我希望能够将子项添加到数据库中,这些数据库与父项(具有成员ID MID)相关联。我认为错误在于日期格式(至少是我所相信的),我也尝试使用strtotime($ dob),但这并没有改变任何东西。

   $name = htmlspecialchars($_GET['Name']);
   $dob = $_GET['DOB'];
   $newDOB = date("Y-m-d", $dob);
   $mid = $_GET['mid'];

   if(isset($_GET['Name'], $_GET['DOB'], $_GET['mid']))
       $alert = true;
   if(!empty($name) && !empty($newDOB) && !empty($mid))
       add_family_member($mid, $newDOB, $name);

添加成员的功能:

function add_family_member($mid, $dob, $name)
{
    global $con;
    $sql = "INSERT INTO Children(MID, DOB, Name) VALUES(?, ?, ?)";
    $stmt = $con->prepare($sql);
    if($stmt)
    {
        $b = $stmt->bind_param("iss", $mid, $dob, $name);
        if($b)
        {
            $e = $stmt->execute();
            if($e)
                return true;
        }
    }

    return false;
}

2 个答案:

答案 0 :(得分:1)

function add_family_member($mid, $dob, $name)
{
    global $con;
    $sql = "INSERT INTO Children(MID, DOB, Name) VALUES(:mid, :dob, :name)";
    $stmt = $con->prepare($sql);
    if($stmt)
    {
        return $stmt->execute(array(
             'mid' => $mid,
             'dob' => $dob,
             'name' => $name
        ));           
    }

    return false;
}

有关更多示例,请参阅http://www.php.net/manual/en/pdostatement.execute.php

答案 1 :(得分:0)

...试

function add_family_member($mid, $dob, $name)
    {
        global $con;
        $sql = "INSERT INTO Children(MID, DOB, Name) VALUES(:mid, :dob, :name)";
        $stmt = $con->prepare($sql);
        $stmt->bindParam(':mid', $mid);
        $stmt->bindParam(':dob', $dob);
        $stmt->bindParam(':name', $name);
        if ($stmt->execute()) {
            return true;
        }
        return false;
    }