1

I have a table called 'incidents' with the following fields:

incidentID (aurtoincrement int) type (text) description (text) date (date) studentID (int) userID (int) schoolID (int)

The following form collects the information:

<form action="behavioursubmit.php" method="post" onsubmit="return validateBehaviour()">
    Incident Type: <select id="incType" name="incType">
        <option value="Classwork">classwork</option>
        <option value="Homework">homework</option>
        <option value="Break incident">break</option>
        <option value="Lunch Incident">lunch</option>
        <option value="Racism">racism</option>
        <option value="Sexism">sexism</option>
        <option value="Truancy">truancy</option>
        <option value="Bullying">bullying</option>
        <option value="Homophobia">homophobia</option>
        <option value="Hate Speech">hatespeech</option>
      </select><br><div id="ertype"></div>
    Incident Description: <textarea name="descInput" autocomplete="off" rows="6" cols="50"></textarea><div id="erdesc"></div>
    Incident Date: <input type="text" id="incDate" name="incDate"><div id="erdate"></div>
    <input type="submit">
</form>

This is the validateBehaviour() function in JS:

function validateBehaviour() {
    var incType = document.getElementsByName("incType")[0].value
    var descInput = document.getElementsByName("descInput")[0].value
    var incDate = document.getElementsByName("incDate")[0].value
    var failed = false;
    if(!incType){
        document.getElementById("ertype").innerHTML = "No type entered";
        failed = true;
    }
    if(!incDate){
        document.getElementById("erdate").innerHTML = "No date entered";
        failed = true;
    }
    if(!descInput){
        document.getElementById("erdesc").innerHTML = "No description entered";
        failed = true;
    }
    return !failed;
}

And this is the PHP to send the data to the DB:

$dbconn = OpenCon();
$studentID = intval($_SESSION['loggedStudent']['studentID']);
$schoolID = $_SESSION['loggedStudent']['schoolID'];
$logger = intval($_SESSION['loggedIn']['userID']);
$incType = $_POST['incType'];
$incDesc = $_POST['incDesc'];
$incDate = $_POST['incDate'];
$sqlstmnt2 = 'INSERT INTO incidents (`type`, `description`, `date`, `studentID`, `userID`, `schoolID`) VALUES (:incType, :incDesc, :incDate, :studentID, :logger, :schoolID)';
$stmtUsr2 = $dbconn -> prepare($sqlstmnt2);
$stmtUsr2 -> bindValue(':incType', $incType);
$stmtUsr2 -> bindValue(':incDesc', $incDesc);
$stmtUsr2 -> bindValue(':incDate', $incDate);
$stmtUsr2 -> bindValue(':logger', $logger);
$stmtUsr2 -> bindValue(':schoolID', $schoolID);
$stmtUsr2 -> bindValue(':studentID', $studentID);
$stmtUsr2 -> execute();
// fetch pupil's ID for use in further queries
$sqlfetch = 'SELECT * FROM students WHERE studentID = (SELECT MAX(studentID) from students)';
$sqlfetchexec = $dbconn -> prepare($sqlfetch);
$sqlfetchexec -> execute();
$row = $sqlfetchexec->fetch();
$_SESSION['loggedStudent'] = $row;
$id = $_SESSION['loggedStudent']['studentID'];
// redirect to pupil's profile
header("Location: pupilprofile.php?id=".$id);
die();

I get that there are major inefficiencies with the code, but all I'm interested in at the moment is why the records aren't submitting. I've var_dumped the parameters but they all seem to be OK. I'm getting no PDO error messages by nothing is going in the DB.

0 Answers0