简体   繁体   中英

SQL not Inserting using PHP

I have the following code sending data from a form to an SQL database:

<?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();
    }
}
?>

My database table is structured as with studentID being a primary key auto incrementing and all fields apart from dob as text (dob is set as datetime)

The form which is sending the data looks like this:

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

I'm not getting a PDO error but the data is not inserting in to the table. I'm thinking that it might be something to do with the date picker not matching with the data type in SQL but I'm not sure. Any ideas?

Get MySQL Errors in PHP

You can know if the MySQLi statement succeeded by printing the results of $statement->execute() . To quote the POD...

Return Values

Returns TRUE on success or FALSE on failure. (Source: PHP.net )

If it returns TRUE , then maybe there is a different problem going on than the statement failing.

You can also see the results of MySQL statement with error() and errno() , from the MySQLi package in PHP. You don't list it in your code, but you must have defined $dbconn in a way like this...

$dbconn = new mysqli($hostname, $username, $password, $database);

After executing with $stmtUsr2 -> execute(); , try running this code...

print_r($dbconn->error);
print_r($dbconn->errno);

Your Specific Case

Change your datepicker's format to match MySQL's insert format.

$(selector).datepicker({
        dateFormat: 'yy-mm-dd',
});

More Help

I have another answer on getting more debug info here: debug_print_backtrace() to String for log-file .

Add

dateFormat: 'yy-mm-dd'

into to the Datepicker function to match the format with the MySQL date time field

IS onclick="if(validatePupilReg()) this.form.submit() Working?

Use AJAX / JQuery for form submission:

$('form').on('click', function() {  

    var form_input = $(this).serialize();

    $.ajax({
        type: "POST",
        url: "/ajax_calls/xxx",
        data: form_input,
        success: function(this_result){

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM