hello. i got this error and i have no idea why or what is going on. could someone explain in layman's term please and help me out.

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'event_id' cannot be null' in /home/dhsbnet/public_html/ems/add_guest.php:36 Stack trace: #0 /home/dhsbnet/public_html/ems/add_guest.php(36): PDOStatement->execute() #1 {main} thrown in /home/dhsbnet/public_html/ems/add_guest.php on line 36

code being referred to, line 36 is $guests->execute() :

if(isset($_SESSION['sess_user_id']))
{
  if(isset($_POST['save']))
  {
    require "connection.php";
    $name = $_POST['guest-name'];
    $event = $_POST['event'];
    $session = $_SESSION['sess_user_id'];
    $status = "";
    $status .= "0";

    $guests = $dbh->prepare("INSERT INTO guest(guser_id,guest_name,event_id,status) VALUES (?,?,?,?)");

    for($i = 0; $i < count($_POST['guest-name']); $i++)
    {
      if(trim($_POST['guest-name'][$i]) !== '')
      {
        $guests->bindParam(1, $session, PDO::PARAM_INT);
        $guests->bindParam(2, $name[$i], PDO::PARAM_STR);
        $guests->bindParam(3, $event, PDO::PARAM_INT);
        $guests->bindParam(4, $status, PDO::PARAM_INT);
        $guests->execute();

        if($guests->rowCount() > 0)
        {
          echo "<script>window.location.href = 'guest_list.php';</script>";
        } 
      }
    }
  }
}

<div class="dropdown">
    <label>Event : <label/>
    <select name="event" id="dd-event" required>
      <option value="0" selected>Event</option>
      <?php foreach($retrieve as $r): ?>
      <option value="<?=$r['event_id']?>"><?=$r['event_name']?></option>
      <?php endforeach ?>
    </select>
  </div>

in db event_id column is int, not null, not auto incremented. TIA!

Recommended Answers

All 4 Replies

Check your generated HTML for the dropdown. It says you are trying to insert an event without an ID. Probably, one of the options does not have a value. If it is, check your POST array for the value of event.

but i checked it. i changed this:

<option value="<?=$r['event_id']?>"><?=$r['event_name']?></option>

to

<option value="<?=$r['event_id']?>"><?=$r['event_id']?></option>

so i could c what the event id and it does show the id.

column 'event_id' cannot be null

The error says it is. Check the $_POST array.

i used var_dump and it is null. coz i always automatically assume its a php problem i didn't check my html so i did and found that the drop down wasn't in the form tag.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.