nadiam Posted October 14, 2014 Share Posted October 14, 2014 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! Quote Link to comment https://forums.phpfreaks.com/topic/291610-fatal-error-integrity-constraint-violation/ Share on other sites More sharing options...
mac_gyver Posted October 14, 2014 Share Posted October 14, 2014 the $event variable is apparently a null value. what debugging have you done to find out what is in $event? also, your table definition doesn't make sense. an auto-increment column is by design supposed to be for the database engine to assign values for, not for you to insert your own values. p.s. see my reply in your other thread concerning using the bindParam() statements in the wrong place in the code. Quote Link to comment https://forums.phpfreaks.com/topic/291610-fatal-error-integrity-constraint-violation/#findComment-1493495 Share on other sites More sharing options...
jcbones Posted October 14, 2014 Share Posted October 14, 2014 event_id is NOT auto incremented. It still stands that the event_id is passing NULL value. You also shouldn't need to re-bind any values except the $name value. That is all that should be in the loop. Maybe a: echo '<pre>' . print_r($_POST,true) . '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/291610-fatal-error-integrity-constraint-violation/#findComment-1493552 Share on other sites More sharing options...
Solution nadiam Posted October 15, 2014 Author Solution Share Posted October 15, 2014 i used var_dump and the result IS null. then coz i always automatically think its a php problem i took a look instead at my html and found that the drop down wasn't in the form tags. Quote Link to comment https://forums.phpfreaks.com/topic/291610-fatal-error-integrity-constraint-violation/#findComment-1493559 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.