2016-08-11 7 views
0

私が抱えている問題は、フォームに入力された情報をデータフィールド(LDA)が$ lda = date( 'Ymd H:i' strtotime($ _ POST ['lda']));私が提出したデータに関わらず、提出されたデータを見ると、私は1969-12-31 00:00:00の日付を取得しています。HTMLフォームがPHPを使用してMySQLデータベースにデータを渡す

$ 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 = $_POST (date('Y-m-d H:i', strtotime($_POST['lda']))); 

する

$lda = 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'])); 

POSTメソッドをGETです大文字と小文字を区別する

+0

ありがとうございました!私はかなりPHPのコーディングをしていて、_POSTと_GETが大文字と小文字を区別していることを知らなかった。フォームを小文字にして、情報を正しく渡しています。 –

+0

@ C.Smarereantは良いと思いますが、使用する前にすべての新しいものについて学びましょう。 :) –

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> 
関連する問題