我有以下代碼將數(shù)據(jù)從表單發(fā)送到 SQL 數(shù)據(jù)庫:<?phpsession_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(); }}?>我的數(shù)據(jù)庫表的結構為,studentID 為主鍵自動遞增,除 dob 之外的所有字段均為文本(dob 設置為日期時間)發(fā)送數(shù)據(jù)的表單如下所示:<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 錯誤,但數(shù)據(jù)沒有插入到表中。我認為這可能與日期選擇器與 SQL 中的數(shù)據(jù)類型不匹配有關,但我不確定。有任何想法嗎?
SQL 無法使用 PHP 插入
寶慕林4294392
2023-12-15 15:16:09