C.Sargreant C.Sargreant - 3 months ago 31
MySQL Question

HTML Form passing a date field to MySQL database using PHP

The issue I am having is that the data field (LDA) is not passing the information entered on the form into the $lda = date('Y-m-d H:i', strtotime($_POST['lda'])); When I look at the data submitted, regardless of what I submitted, I am getting a date of 1969-12-31 00:00:00.

I used $lda = date('Y-m-d H:i', strtotime($_POST['lda'])); because I was getting a date type error and relized that it must be that the date needed formated for MySQL.

I have the following very simple HTML form:

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


and I am using the following PHP to INSERT INTO my database.

<?php
//create connection
$con = mysql_connect("localhost", "wccappcust", "Y3!!f00tb@11") 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)
?>


What am I missing?

Should I change

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


to be

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


I think I am so close. Any advice you can get is greatly appreciated.

Answer

In form you are using name of field in capital letters as:

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

So instead of this:

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

Please name of field same as in form field name as:

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

GET and POST method are case-sensitive.