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
