PDO undefined index and uncaught exception errors

I am getting these errors. Kindly assist

Notice: Undefined index: message in line 18 and Fatal error: Uncaught exception ‘PDOException’ with message ‘SQLSTATE[23000]: Integrity constraint violation: 1048 Column ‘bloodgroups’ cannot be null’ on line 33

ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

include_once 'includes/config.php'; 

  if(isset($_POST['submit'])){

    $fullname=$_POST['fullname'];
    $mobilenumber=$_POST['mobilenumber'];
    $email=$_POST['email'];
    $age=$_POST['age'];
    $gender=$_POST['gender'];
    $blodgroups=$_POST['bloodgroups'];
    $donate=$_POST['donate'];
    $location=$_POST['location'];
    $message=$_POST['message']; //line 18
    $status=1;
    

    $sql="INSERT INTO  tblblooddonars(fullname,mobilenumber,email,age,gender,bloodgroups,donate,message,status) VALUES(:fullname,:mobilenumber,:email,:age,:gender,:bloodgroups,:donate,:message,:status)";
    $query = $dbh->prepare($sql);
    $query->bindParam(':fullname',$fullname,PDO::PARAM_STR);
    $query->bindParam(':mobilenumber',$mobilenumber,PDO::PARAM_STR);
    $query->bindParam(':email',$email,PDO::PARAM_STR);
    $query->bindParam(':age',$age,PDO::PARAM_STR);
    $query->bindParam(':gender',$gender,PDO::PARAM_STR);
    $query->bindParam(':bloodgroups',$bloodgroups,PDO::PARAM_STR);
    $query->bindParam(':donate',$donate,PDO::PARAM_STR);
    $query->bindParam(':message',$message,PDO::PARAM_STR);
    $query->bindParam(':status',$status,PDO::PARAM_STR);
    $query->execute(); //line 33

    if($lastInsertId) {

      $msg="Your info submitted successfully";

    }else {

      $error="Something went wrong. Please try again";
    }
  }
?>

You didn’t post the form HTML, but the undefined index error message suggests the form does not have an input with the name “message”.

The PDOException is easy once you see it.
$blodgroups != $bloodgroups

Yes i saw it and changed it but still an error…

Integrity constraint violation: 1048 Column ‘message’ cannot be null…

I did post the form with Method =POST

Yes it does not have because its a textarea…

What I meant is you didn’t post the HTML here so it could be looked at, not that the form didn’t have the method attribute value POST.

Mittineague wants to see the HTML part of your form code, because he thinks you havent put a field in it with the name ‘message’. Even textareas must have a name attribute.

Your browser needs to know what $_POST value to put the data into. If you dont give it a name, it doesn’t know where to put it… so it just plain doesn’t include it anywhere.

2 Likes

sorry for that here is the form…

<?php
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

include_once 'includes/config.php'; 

  if(isset($_POST['submit'])){

    $fullname=$_POST['fullname'];
    $mobilenumber=$_POST['mobilenumber'];
    $email=$_POST['email'];
    $age=$_POST['age'];
    $gender=$_POST['gender'];
    $bloodgroups=$_POST['bloodgroups'];
    $donate=$_POST['donate'];
    $location=$_POST['location'];
    $message=$_POST['message'];
    $status=1;
    

    $sql="INSERT INTO  tblblooddonars(fullname,mobilenumber,email,age,gender,bloodgroups,donate,message,status) VALUES(:fullname,:mobilenumber,:email,:age,:gender,:bloodgroups,:donate,:message,:status)";
    $query = $dbh->prepare($sql);
    $query->bindParam(':fullname',$fullname,PDO::PARAM_STR);
    $query->bindParam(':mobilenumber',$mobilenumber,PDO::PARAM_STR);
    $query->bindParam(':email',$email,PDO::PARAM_STR);
    $query->bindParam(':age',$age,PDO::PARAM_STR);
    $query->bindParam(':gender',$gender,PDO::PARAM_STR);
    $query->bindParam(':bloodgroups',$bloodgroups,PDO::PARAM_STR);
    $query->bindParam(':donate',$donate,PDO::PARAM_STR);
    $query->bindParam(':message',$message,PDO::PARAM_STR);
    $query->bindParam(':status',$status,PDO::PARAM_STR);
    $query->execute(); 

    if($lastInsertId) {

      $msg="Your info submitted successfully";

    }else {

      $error="Something went wrong. Please try again";
    }
  }
?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Donate/Acceptor Panel</title>
    <link href="css/user.css" rel="stylesheet">
  </head>
  <body>

  <?php 
  include_once 'includes/config.php';
  $sql = $dbh -> prepare('SELECT * FROM users WHERE id = :id');
  $sql->bindValue(':id', $_SESSION['id']);
  $sql->execute();
  $result = $sql->fetch();

	echo'WELCOME    '.strtoupper($result['username']);

	?>

	</br>
	</br>
	<button onclick="window.location.href='logout.php'">Logout</button>
  <hr>
  <h1 style="text-align: center;">Become a Donor/Acceptor</h1>

  <div class="container">
      <form action="" method="POST">
      <section>
        <div style="float:left; margin-right:20px;">
          <label for="fname">Full Name<span class="required">*</span></label>
          <input type="text" id="fname" name="fullname" placeholder="Your Full Name..">
        </div>

         <div>
          <label for="mobilenumber">Mobile No.<span class="required">*</span></label>
          <input type="text" id="mobilenumber" name="mobilenumber" placeholder="Your Mobile Number..">
        </div>

        <div style="float:left;margin-right:20px;">
          <label for="email">Email ID<span class="required">*</span></label>
          <input type="text" id="email" name="email" placeholder="Your Email Address..">
        </div>

        <div >
          <label for="age">Age<span class="required">*</span></label>
          <input type="text" id="age" name="age" placeholder="Your Age..">
        </div>

      <div style="float:left; margin-right:20px;">
        <label for="gender">Gender<span class="required">*</span></label>
        <select id="gender" name="gender">
          <option value="male">Male</option>
          <option value="female">Female</option>
        </select>
        </div>

         <div>
        <label for="bloodgroups">Blood groups<span class="required">*</span></label>
        <select id="bloodgroups" name="bloodgroups">
          <option value="AB-">AB-</option>
          <option value="B-">B-</option>
          <option value="AB+">AB+</option>
          <option value="A-">A-</option>
          <option value="O-">O-</option>
          <option value="B+">B+</option>
          <option value="A+">A+</option>
          <option value="O+">O+</option>
        </select>
        </div>

        <div style="float:left;margin-right:20px;">
        <label for="donor/acceptor">Donor/Acceptor<span class="required">*</span></label>
        <select id="donor/acceptor" name="donate">
          <option value="donor">Donor</option>
          <option value="acceptor">Acceptor</option>
         
        </select>
        </div>

        <div>
        <label for="location">Location<span class="required">*</span></label>
        <select id="location" name="location">
          <option value="Mombasa CBD">New york</option>
          <option value="Changamwe">Texas</option>
          <option value="Tudor">California</option>
          <option value="Majengo">Philly</option>
          <option value="Nyali">L.A</option>
          <option value="Bombolulu">Arizona</option>
          <option value="Likoni">Florida</option>
          <option value="Kongowea">Hawaii</option>
        </select>
        </div>
        

        <br style="clear:both;" />
  
         </section>

        <section>
        <div>
        <label for="message">Message</label>
        <textarea rows="4" cols="50" name="message" class="required">Write Something. </textarea> 
        </div>
        </section>
        <br style="clear:both;" />
        <div>
        <input type="submit" name="submit" value="Submit">
        </div>

       
      </form>
</div>

  </body>
</html>

OK, so the form has a textarea called "message". Is some text being entered into the textarea? I know you have assigned it a class called “required”, but is that the same as setting the HTML5 “required” attribute?

You should really do some validation checks on the data before you use it - and check that each form variable exists before you try to assign it to something else, as you do when you look at the “submit” form variable.

Doubt it. Text areas don’t have the required attribute IIRC. You have to set it up in JavaScript to require it.

———

The main problem here is that you have a column that has a default of NOT NULL. This means that you are required to insert something into it. You have to look at your prepared statements to make sure those columns are being inserted to. If you want to leave these fields empty then you have to set the columns to default as NULL. I always default everything except primary keys to NULL because I can’t predict what I want to insert into.

Thanks I have understood…

That’s a bit of a blanket statement. You can’t say for every table, but there are plenty of examples where you would want NOT NULL. Such as which users, the username and passport may never be NULL. In an order the price may never be NULL, etc.

1 Like

True. Though you can set prices to a specific default value if it isn’t being inserted for example, what if the price of something was free? Then you’d have to insert 0 into the price range since you are giving something away for free. And for usernames, what if they haven’t picked a username yet? Setting the username to NULL and then later changing that value to whatever the user wants seems to be sufficient or what if you rely on the user ID and not the username? Then you wouldn’t really need the username. Though I do understand the passport part. It makes sense to require those kinds of data.

I was presuming that at some point when the rest is working, the OP will be adding some validation to the user-supplied data, which will include checks to see that all required information is present, selections are valid, and so on. It’s great that they use prepared statements to start off with, of course that will help a little.

I thought they did. I can’t find the spec, but it certainly works when I’ve tried it.

1 Like

Going by MDN it looks like textarea required now has good browser support.

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/textarea

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.