2016-08-11 4 views
0

Das Problem, das ich habe, ist, dass das Datenfeld (LDA) nicht die in das Formular eingegebenen Informationen in das $ lda = Datum ('Ymd H: i') strtotime ($ _ POST ['lda'])); Wenn ich mir die eingereichten Daten anschaue, bekomme ich unabhängig davon, was ich eingereicht habe, ein Datum von 1969-12-31 00:00:00.HTML Formular übergibt ein Datumsfeld an MySQL-Datenbank mit PHP

Ich habe $ lda = Datum ('Y-m-d H: ich', strtotime ($ _ POST ['lda'])); weil ich einen Datumstypfehler erhielt und relized, dass es das Datum sein muss, das für MySQL formatiert wird.

Ich habe folgendes sehr einfaches HTML-Formular:

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

und ich bin mit dem folgenden PHP in meiner Datenbank einzufügen.

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

Was fehlt mir?

Soll ich ändern

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

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

ich zu sein denke ich, so nah bin. Jeder Rat, den Sie bekommen können, wird sehr geschätzt.

+0

Stellen Sie sicher, dass '$ lda' ist, was Sie denken, dass es ist. Vielleicht ist es kein Datum Zeit Zeichenfolge. – aynber

+0

Sie haben mehrere Probleme. Der mysql_ wurde von php7 http://php.net/manual/en/intro.mysql.php entfernt. Sie werden zu mysqli_ wechseln, wenn Sie können. Sie sollten vorbereitete Anweisungen verwenden. Ihre anfällige sql Injektion. http://bobby-tables.com/ –

Antwort

0

In Form Sie in Form ändern sind Verwenden des Feldnamens in Großbuchstaben als:

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

Anstatt also diese:

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

Bitte nennen Feld gleichen wie in Form Feldnamen wie:

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

GET und POST Verfahren sind case-sensitive.

+0

Vielen Dank! Ich bin ziemlich neu in PHP-Programmierung und wusste nicht, dass _POST und _GET Groß-und Kleinschreibung. Ich habe das Formular in Kleinbuchstaben geschrieben und es gibt die Informationen korrekt weiter. –

+0

@ C.Sargreant, das klingt gut, bitte versuchen Sie, über jede neue Sache zu lernen, bevor Sie es verwenden. :) –

1

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

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

1

Sie stammen Feldnamen aktiviert ist .. so Ihr Datumsfeld

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