Error: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'comments' cannot be null

Viewed 20124

I have already tried checking for related post but it doesn't seem to help me fix my problem at hand. I am trying to create an page for appointments where data are saved in the database. however, these two error message kept appearing on my window:

Notice: Undefined index: comments in C:\xampp\htdocs\db\connect2.php on line 29 Error: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'comments' cannot be null

here is the code php

<form name="appointments" action="" method="POST">
<label>Name (First/Last)</label> <br>
<input type="text" name="name" id="name" required="required" placeholder="Please Enter Name"> <br>
<label>Email Address</label> <br>
<input type="email" name="email" id="email" required="required" placeholder="youremail@yahoo.com"> <br>
<label>Phone Number</label> <br>
<input type="text" name="contactno" id="contactno" required="required" placeholder="9221234567"> <br>
<label>Nature of Appointment</label> <br>
<select name="service" id="service">
<option value="other">Other</option>
<option value="consultation">Consultation</option>
<option value="Surgery">Surgery</option>
</select> <br>
</div>
<label>Preferred Appointment Date</label> <br>
<input type="date" name="prefDate" id="prefDate"> <br>
<label>Comments</label> <br>
<textarea rows="12" cols="40" name="comments" form="usrform" placeholder="Your comments here..."></textarea> <br>
</div>
<input type="submit" class="btnRegister" name = "schedule" value="Send Your Request">
</form>

Here is my connect2.php

<?php
    //Local Server Information
    $server = "127.0.0.1";
    $username = "root";
    $password = "";
    $db = "myDB";

    $name = "";
    $email = "";
    $contactno = "";
    $service = "";
    $prefDate = "";
    if (isset($_POST['comments']) && !empty($_POST['comments'])) {
        $comments = $_POST['comments'];
    } else {
        $comments = "";
    }
    //Check if connection was successful
    try {
        $con = new PDO("mysql:host=$server;dbname=$db","$username","$password");
        $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        if(isset($_POST['schedule']) )
        {
            $name = $_POST['name'];
            $email = $_POST['email'];
            $contactno = $_POST['contactno'];
            $service = $_POST['service'];
            $prefDate = $_POST['prefDate'];
            $comments = $_POST['comments'];


            $insert = $con->prepare("INSERT INTO appointments(name, email,contactno, service, prefDate, comments) values(:name, :email, :contactno, :service, :prefDate, :comments)");

            $insert->bindParam(':name', $name);
            $insert->bindParam(':email', $email);
            $insert->bindParam(':contactno', $contactno);
            $insert->bindParam(':service', $service);
            $insert->bindParam(':prefDate', $prefDate);
            $insert->bindParam(':comments', $comments);

            $insert->execute();
        }
    } catch(PDOException $e) {
        //die("Oops! Something went wrong with your database.");
        echo "Error: ". $e->getMessage();
    }
?>

This is the line where the error says the problem was.

$comments = $_POST['comments'];

I already tried hard coding it like $comments="you comment"; It went through with no errors and the data appears on the database. However, when I use the code above, error appears. Anyone help me please. Did i miss something? NOt sure what went wrong since the other line seems to work.

thanks

3 Answers

the error message tells itself - you are trying to not assign value in column which cannot be empty. solution is - in your database table, set default value as null to your comments field. if you use phpmyadmin printscreen

or you can run mysql query (or other database, which you use), something like this

ALTER TABLE <table name> MODIFY COLUMN comments <column type> DEFAULT NULL;

You can try this:

$comments = !empty($_POST['comments']) ? $_POST['comments'] : 'Default Comment';

So tried to fix it applying your suggestions. and it seems the one responsible for the error in this portion of the code I entered under php.

<textarea rows="12" cols="40" name="comments" form="usrform" placeholder="Your comments here..."></textarea>

is

form="usrform"

removing it simply made the code to work. it seems that the form stops in the prefDate because i assigned a different form name by accident on the textarea tag.

thanks the two who gave advise that made me see what went wrong

Related