0

Receiving the following error message when trying to add a new record in the database:

Notice: Undefined index: course_title in /Applications/XAMPP/xamppfiles/htdocs/insights/ManageCourses_AddSubmit.php on line 13 NULL Message: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'course_title' cannot be null

I have similar queries which update and delete records in the table and they work fine so I'm not sure why there's a problem with this.

it says NULL for the course_title however in the javascript i serialise the form information so I'm not sure why its not sending it.

the course_details table has an auto-increment field which is automatically updated when a record is added.

Could anyone shine some light on what could be the issue please?

javascript which handles what happens when submit is clicked:

function addCall() {
  var data = $('#addForm').serialize();
  $.post('ManageCourses_AddSubmit.php', data, function(response){

    $("#addForm").html(response);
  }).fail(function(jqXHR, textStatus) {
    alert( "Request failed: " + textStatus );
  });
}

the modal where the form is which gets submitted:

<div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="addModalLabel" aria-hidden="true">
                            <div class="modal-dialog">
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                        <h4 class="modal-title">Add New Record: </h4>
                                    </div>
                                    <div class="modal-body">
                                        <form id="addForm" class="addForm">
                                            <div class="form-group">
                                                <label for="course_code" class="pull-left" class="control-label">Course Code:</label>
                                                <input type="text" class="form-control" id="ourse_code_id" name="code[]" readonly value ="NULL">
                                            </div>
                                            <div class="form-group">
                                                <label for="course_name" class="pull-left" class="control-label">Course Title:</label>
                                                <input type="text" class="form-control" placeholder="Enter Course Title" id="course_name_id" name="title[]">
                                            </div>
                                        </form>
                                    </div>
                                    <div class="modal-footer">
                                        <div class="btn-toolbar">
                                            <button type="button" class="btn btn-primary" id="add_row" name="add_row">Add New Record <span class="glyphicon glyphicon-plus"></button>
                                            <button type="button" class="btn btn-default" class="pull-right" data-dismiss="modal">Close</button>
                                            <button type="button" class="btn btn-success" class="pull-right" onclick="addCall();">Submit <span class="glyphicon glyphicon-saved"></button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

finally the php file:

<?php

include "db_conx.php";

    try
    {
        $db_conx = new PDO("mysql:host=$mysql_hostname;dbname=$mysql_dbname", $mysql_username, $mysql_password);

        $db_conx->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $sql = $db_conx->prepare("INSERT INTO `insights`.`course_details` (`course_code`, `course_title`) VALUES (:course_code, :course_title)");

        $course_title = $_POST['course_title'];
        $course_code = $_POST['course_code'];

        echo var_dump($course_title)."<br>";
        echo var_dump($course_code)."<br>";

        $sql->bindParam(':course_title', $course_title, PDO::PARAM_STR);
        $sql->bindParam(':course_code', $course_code, PDO::PARAM_STR);


        /*** execute the prepared statement ***/
        $sql->execute();

        /*** success message ***/


        $message = "<p class='text-success'> Record Successfully Added <span class='glyphicon glyphicon-ok'/></p>";
    } 
    catch(Exception $e)
    {
        $message = 'Message: ' .$e->getMessage();
    }


die($message);
?>

I've noticed when i use back ticks around (:course_title)it adds the record in fine however the value added is :course_title

Many Thanks

2
  • As it says "Column 'course_title' cannot be null", so either allow course_title to be also NULL, or update your code to not set title as NULL... Commented Mar 3, 2015 at 12:03
  • I am setting course_code to be null because thats an auto increment number. if i set course_title to be null then there will be problems when a user tries to submit invalid information :/ which code are you referring to? Commented Mar 3, 2015 at 12:07

1 Answer 1

1

I think it's a naming mistake.

In your form, you have this input field:

<label for="course_name" class="pull-left" class="control-label">Course Title:</label>
<input type="text" class="form-control" placeholder="Enter Course Title" id="course_name_id" name="title[]">

There you call it course_name_id. Then in your PHP script, you try to get these variables:

$course_title = $_POST['course_title'];
$course_code = $_POST['course_code'];

There you call it course_title.

I suggest you try to clean up all names used. If the table field is called course_title, then use this name for the form and for the PHP controller.

Or, a more elegant approach, try to outsource all names into some XML config files, so your controller does always the same work based on config files :-)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for pointing that out. Must've missed it while i was attempting to do something else!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.