2017-05-17 71 views
1

我試圖從下拉列表中插入數據到我的數據庫,但是我遇到了一個錯誤,請幫忙。 DOB在我的數據庫中設置爲日期格式。DOB mysql插入

PS。然而,它插入到我的罰款沒有任何問題。

在此先感謝,併爲貧困英語技能對不起=(

下面是我的PHP代碼

<?php 

require '../ppuyakul/php/db_conn.php'; 

error_reporting(0); 
$message = ''; 
$year = $_POST['year']; 
$month = $_POST['month']; 
$date = $_POST['date']; 
$DOB = date("Y-m-d", mktime(0,0,0,$month, $day, $year)); 



if(!empty($_POST['email']) && !empty($_POST['password']) && !empty($_POST['fullname']) && !empty($_POST['username']) && !empty($_POST['password_confirmation']) && !empty($_POST['gender']) && !empty($_POST['country']) && !empty($_POST['state']) && !empty($_POST['city']) && !empty($_POST['day']) && !empty($_POST['month']) && !empty($_POST['year'])): 

    // Enter the new user in the database 
    $sql = "INSERT INTO assignment2 (fullname,username, email, password, passwordcon, gender, country, state, city, day, month, year, DOB) VALUES (:fullname, :username, :email, :password, :password_confirmation, :gender, :country, :state, :city, :day, :month, :year, DOB)"; 
    $stmt = $conn->prepare($sql); 

    $stmt->bindParam(':fullname', $_POST['fullname']); 
    $stmt->bindParam(':username', $_POST['username']); 
    $stmt->bindParam(':email', $_POST['email']); 
    $stmt->bindParam(':password', password_hash($_POST['password'], PASSWORD_BCRYPT)); 
    $stmt->bindParam(':password_confirmation', password_hash($_POST['password_confirmation'], PASSWORD_BCRYPT)); 
    $stmt->bindParam(':gender', $_POST['gender']); 
    $stmt->bindParam(':country', $_POST['country']); 
    $stmt->bindParam(':state', $_POST['state']); 
    $stmt->bindParam(':city', $_POST['city']); 
    $stmt->bindParam(':day', $_POST['day']); 
    $stmt->bindParam(':month', $_POST['month']); 
    $stmt->bindParam(':year', $_POST['year']); 
    $stmt->bindParam(':DOB', $_POST['year'], $_POST['month'], $_POST['day']); 


    if($stmt->execute()): 
    $message = 'Successfully created new user'; 
    else: 
    $message = 'Sorry there must have been an issue creating your account'; 
    endif; 
endif; 

?> 

這裏是我的HTML

<div> 
      <p style="display: inline; margin-right: 1%"><span style="font-weight: bold;">DATE OF BIRTH :</span></p> 
      <select class="formInputDate" name="day" value="" id="day"></select> 
      <select class="formInputDate" name="month" id="month"> 
           <option value="1">Jan</option> 
           <option value="2">Feb</option> 
           <option value="3">Mar</option> 
           <option value="4">Apr</option> 
           <option value="5">May</option> 
           <option value="6">Jun</option> 
           <option value="7">Jul</option> 
           <option value="8">Aug</option> 
           <option value="9">Sep</option> 
           <option value="10">oct</option> 
           <option value="11">Nov</option> 
           <option value="12">Dec</option> 
      </select> 
      <select class="formInputDate" name="year" value="" id="year"> 
      </select> 
      </div> 
+1

1.什麼是錯誤? 2.你期望從'bindParam(':DOB',$ _POST ['year'],$ _POST ['month'],$ _POST ['day'])''? –

+0

這看起來不正確......':月份,:年份,出生日期)'缺少冒號? –

+0

感謝您的回覆,我很新的PHP,哪個命令打印出來的錯誤,因爲現在它只是不插入數據到數據庫,但沒有顯示任何東西, –

回答

0

改變這一行下拉代碼:

$stmt->bindParam(':DOB', $_POST['year'], $_POST['month'], $_POST['day']); 

到:

$stmt->bindParam(':DOB', $_POST['year'].'-'. $_POST['month'].'-'. $_POST['day']); 

的原因是因爲日期格式要求正確格式的日期一樣YYYY-MM-DD。

+0

非常感謝您的答覆,我試了一下,但仍然沒有't工作,即時通訊考慮如果有什麼關係 '$ year = $ _POST ['year']; $ month = $ _POST ['month']; $ date = $ _POST ['date']; $ DOB = date(「Ymd」,mktime(0,0,0,$ year,$ month,$ day));' 像我不確定是否需要聲明變量, 我很新的php =(@Simos Fasouliotis –

+0

我實際上修復它,通過聲明這第一個'$ DOB = date(「Ymd」,strtotime($ _POST ['year']。''。'_ _POST [''一個月 ']。' - ' $ _ POST [ '天']));' 然後 '$ stmt-> bindParam( ':DOB',$ DOB);' 非常感謝您的幫助^ ^」 –