我有以下代码将数据从表单发送到 SQL 数据库:
<?php
session_start();
ob_start();
include 'includes/db_connection.php';
if(empty($_POST)){
header("Location: index.php");
}
else{
try {
$dbconn = OpenCon();
$fname = $_POST['fnameInput'];
$sname = $_POST['snameInput'];
$sex = $_POST['sexInput'];
$dob = $_POST['dobInput'];
$schoolID = $_SESSION['school'];
$sqlstmnt2 = 'INSERT INTO students (`firstName`, `surname`, `dob`, `gender`, `schoolID`) VALUES (:firstName, :surname, :dob, :gender, :schoolID)';
$stmtUsr2 = $dbconn -> prepare($sqlstmnt2);
$stmtUsr2 -> bindValue(':firstName', $fname);
$stmtUsr2 -> bindValue(':surname', $sname);
$stmtUsr2 -> bindValue(':dob', $dob);
$stmtUsr2 -> bindValue(':gender', $sex);
$stmtUsr2 -> bindValue(':schoolID', $schoolID);
$stmtUsr2 -> execute();
// redirect to pupil's profile
header("Location: pupilprofile.php");
die();
}
catch (PDOException $e) {
echo "DataBase Error: The user could not be added.<br>".$e->getMessage();
}
catch (Exception $e) {
echo "General Error: The user could not be added.<br>".$e->getMessage();
}
}
?>
我的数据库表的结构为,studentID 为主键自动递增,除 dob 之外的所有字段均为文本(dob 设置为日期时间)
发送数据的表单如下所示:
<form action="registersubmit.php" method="post">
First Name: <input type="text" autocomplete="off" name="fnameInput"><div id="erfirstname"></div><br>
Surname: <input type="text" autocomplete="off" name="snameInput"><div id="ersurname"></div><br>
Sex: <select id="sexInput" name="sexInput">
<option value="male">Male</option>
<option value="female">Female</option>
</select><div id="ersex"></div><br>
DOB: <input type="text" id="datepicker" name="dobInput"><div id="erdob"></p>
<input type="submit" name="pupilRegSubmit" value="Register" onclick="if(validatePupilReg()) this.form.submit()">
</form>
我没有收到 PDO 错误,但数据没有插入到表中。我认为这可能与日期选择器与 SQL 中的数据类型不匹配有关,但我不确定。有任何想法吗?
慕容708150