2014-06-10 61 views
0

PHP插入到mysql數據庫我想能夠將孩子添加到數據庫,這是連接到他們的父母(誰有一個成員id MID)。我相信錯誤在於日期格式(至少我相信),我也嘗試使用strtotime($ dob),但是這並沒有改變任何東西。PHP插入日期爲

$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; 
} 
+0

嘗試刪除,如果($ B ){}或echo $ b –

+0

您不能混用PDO和MySQLi。順便說一句'DateTime'是專門爲...日期和時間;-) – DanFromGermany

+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更多的例子

+0

不起作用,這不僅適用於使用PDO嗎? – Cralle

+0

你不說你用什麼,但是這是pdo。 – Rufinus

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; 
    }