2016-08-11 89 views
0

我遇到的問題是數據字段(LDA)未將表單中輸入的信息傳遞給$ lda = date('Ymd H:i',的strtotime($ _ POST [ 'LDA']));當我查看提交的數據時,無論我提交什麼,我都會收到1969-12-31 00:00:00的日期。HTML Form將日期字段傳遞給MySQL數據庫使用PHP

我用$ lda = date('Y-m-d H:i',strtotime($ _ POST ['lda']));因爲我得到了一個日期類型錯誤,並且認爲它必須是MySQL需要格式化的日期。

我有以下非常簡單的HTML表單:

<form action="sd-drop-submit.php" method="post"> 
    <p> 
     <label>Semester:</label>  
     <input type="radio" name="semester" value="Spring <?php echo date("Y");?>)">Spring <?php echo date("Y");?> 
     <input type="radio" name="semester" value="Summer <?php echo date("Y");?>">Summer <?php echo date("Y");?> 
     <input type="radio" name="semester" tooltip="Semester" value="Fall <?php echo date("Y");?>" required="yes" message="Please select the semester." tabindex="1">Fall <?php echo date("Y");?> 
    </p> 

    <p> 
     <label>Student First Name:</label> 

     <input type="text" name="stdfname" tooltip="Student's First Name" required="yes" message="Please enter the student's first name." tabindex="2"><br> 
     <label>Student Last Name:</label> 
     <input type="text" name="stdlname" tooltip="Student's Last Name" required="yes" message="Please enter the student's last name." tabindex="3"> 
    </p> 
    <p> 
     <label>Student ID Number:</label> 
     <input type="text" name="stdid" tooltip="Student ID Number" message="Please enter the student's ID number." required="yes" tabindex="4"> 
     <label>Program Code:</label> 
     <input type="text" name="program" tooltip="Program Code" message="Please enter the student's program code." required="yes" tabindex="5"> 
    </p> 

    <p> 
     <label>Course:</label> <input type="text" name="course" tooltip="Course" required="yes" message="Please enter the course information." tabindex="6"> (Example: ACA 111, ENG 101) 

    </p> 
    <p> 
     <label>Section:</label> <input type="text" name="section" tooltip="Section" required="yes" message="Please enter the section information." tabindex="7"> (Example: 01, NT1) 

    </p> 
    <p> 
     <label>Last day of Attendance:</label> 
     <input type="date" name="LDA" tooltip="Last Day of Attendance" required="yes" message="Please enter the last day of attendance." validate="date" tabindex="8"> (Date Format Only - Please use the <a href="SD_NA.cfm">Never Attended</a> form if this student has never attanded.)<br> 
    </p> 

    <p> 
     <label>Reason:</label> 
     <input type="radio" name="reason" tooltip="Reason" value="Employment" required="yes" message="Please select the reason this student is being dropped." tabindex="9">Employment 
     <input type="radio" name="reason" value="Excessive Absences">Excessive Absences 
     <input type="radio" name="reason" value="Illness (Personal/Family)">Illness (Personal/Family) 
     <input type="radio" name="reason" value="Missed Assignments">Missed Assignments 
     <input type="radio" name="reason" value="Student Request">Student Request 
     <input type="radio" name="reason" value="Other">Other 

    </p> 
    <p> 
     <label>Instructor First Name:</label> 
     <input type="text" name="instfname" tooltip="Instructor's First Name" required="yes" message="Please enter the instructor's first name." tabindex="10"> 
    </p> 
    <p> 
     <label>Instructor Last Name:</label> 
     <input type="text" name="instlname" tooltip="Instructor's Last Name" required="yes" message="Please enter the instructor's last name." tabindex="11"> 
    </p> 
    <p> 
     <label>Instructor Email:</label> 
     <input type="text" name="instemail" tooltip="Instructor's Email" required="yes" message="Please enter the instructor's email." tabindex="12"> 
    </p> 
    <p> 
     <label>Comments:</label><br> 
     <textarea name="comments" tooltip="Comments" cols="100" rows="5" tabindex="13"></textarea></p> 


     <input type="submit" name="Submit" value="Complete the request"> 
     <input type="reset" name="Reset" value="Reset" id="Reset"> 
    </p> 
</form> 

,我使用下面的PHP插入到我的數據庫。

<?php  
//create connection 
$con = mysql_connect("localhost", "wccappcust", "[email protected]") or die ('Sorry, could not connect to the directory database server' .mysql_error()); 
//Select database 
mysql_select_db("wccapp16", $con) or die ('Sorry, could not connect to the directory database' .mysql_error()); 

$lda = date('Y-m-d H:i', strtotime($_POST['lda'])); 

//Insert our data 
$sql="INSERT INTO sddrop (stdfname, stdlname, semester, stdid, program, course, section, lda, reason, instfname, instlname, instemail, comments) 
VALUES 
('$_POST[stdfname]', '$_POST[stdlname]', '$_POST[semester]', '$_POST[stdid]', '$_POST[program]', '$_POST[course]', '$_POST[section]', '$lda', '$_POST[reason]', '$_POST[instfname]', '$_POST[instlname]', '$_POST[instemail]', '$_POST[comments]')"; 

if (!mysql_query ($sql, $con)) 
{ 
    die ('Error: ' .mysql_error()); 
} 
echo "<h2>Please print for your records</h2> "; 
echo "<h3>Student Drop Form</h3>"; 

echo "Student Name: $_POST[stdfname] $_POST[stdlname]<br>\n"; 
echo "Student ID: $_POST[stdid]<br>\n"; 
echo "Program: $_POST[program]<br>\n"; 
echo "Semester: $_POST[semester]<br>\n"; 
echo "Course: $_POST[course] $_POST[section]<br>\n"; 
echo "Last Day Attended: $lda<br>\n"; 
echo "Reason:$_POST[reason]<br>\n"; 
echo "Comments: $_POST[comments]<br>\n"; 
echo "<hr>"; 
echo "Instructors Name: $_POST[instfname] $_POST[instlname]<br>\n"; 
echo "Instructors Email: $_POST[instemail]<br>\n"; 

mysql_close($con)  
?> 

我錯過了什麼?

我應該改變

$lda = date('Y-m-d H:i', strtotime($_POST['lda'])); 

$lda = $_POST (date('Y-m-d H:i', strtotime($_POST['lda']))); 

我覺得我是如此接近。任何建議,你可以得到非常感謝。

+0

請確定'$ lda'是您的想法。也許它不是日期時間字符串。 – aynber

+0

您有多個問題。 mysql_已從php7中移除http://php.net/manual/en/intro.mysql.php。如果可以的話,你會想改成mysqli_。你應該使用準備好的語句。你的敏感sql注入。 http://bobby-tables.com/ –

回答

0

在形式上你是使用大寫字母的字段名稱:

<input type="date" name="LDA" tooltip="Last Day of Attendance" required="yes" message="Please enter the last day of attendance." validate="date" tabindex="8"> (Date Format Only - Please use the <a href="SD_NA.cfm">Never Attended</a> form if this student has never attanded.)<br>

所以不是這樣的:

$lda = date('Y-m-d H:i', strtotime($_POST['lda'])); 

請領域相同的名稱作爲窗體字段名稱爲:

$lda = date('Y-m-d H:i', strtotime($_POST['LDA'])); 

GETPOST方法是大小寫敏感

+0

謝謝!我對PHP編碼相當陌生,並不知道_POST和_GET區分大小寫。我將表格做成小寫,並正確傳遞信息。 –

+0

@ C.Sargreant聽起來不錯,請在使用它之前嘗試瞭解每一件新事物。 :) –

1

變化:
$lda = date('Y-m-d H:i', strtotime($_POST['lda']));

分爲:
$lda = date('Y-m-d H:i', strtotime($_POST['LDA']));

1

您日期字段的名稱是大寫的..所以在形式更改日期字段

<input type="date" name="lda" tooltip="Last Day of Attendance" required="yes" message="Please enter the last day of attendance." validate="date" tabindex="8"> (Date Format Only - Please use the <a href="SD_NA.cfm">Never Attended</a> form if this student has never attanded.)<br>