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

Asked
Active3 hr before
Viewed126 times

6 Answers

columnintegrityconstraint
90%

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'title' cannot be null (SQL: insert into posts (title, body, updated_at, created_at) values (?, ?, 2019-10-14 17:41:00, 2019-10-14 17:41:00)) , Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers

It looks like you are sending null value for the title and to catch out the validation errors earlier on in your application, you should consider introducing some validation in your controller

    < ? php

    namespace App\ Http\ Controllers;

    use Illuminate\ Http\ Request;
    use App\ post;

    class postcontroller extends Controller {
       public
       function store(Request $request) {
          $request - > validate([
             'title' => 'required',
             'body' => 'required',
          ]);

          $add = Post::create([
             'title' => $request - > input('title'),
             'body' => $request - > input('body')
          ]);

          session() - > flash('msg', 'new post added successfully!');

          return redirect() - > route('posts.show', $add - > id);
       }
load more v
88%

Notice: Undefined index: comments in C:xampphtdocsdbconnect2.php on line 29 Error: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'comments' cannot be null,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:,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.,Had already downloaded the driver and it didn't work. Found a new site for the driver and this one works.

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();
} ?
>
load more v
72%

PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'title' cannot be null,The error message "PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'title' cannot be null" appears in the Drupal error log (admin/reports/dblog),User warning: Illegal mix of collations (PDOException: SQLSTATE[HY000]: General error: 1267),The tricky thing is that it not clear from the error message which module is causing the problem. See the following thread for a way to find the answer: How I solved my "PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'title' cannot be null" error.

You may find the broken entity by the following drush command:

drush eval 'foreach (entity_get_info() as $entity_type => $entity_info) { empty($entity_info['label']) && var_dump($entity_type, $entity_info); };'

drush eval 'foreach (entity_get_info() as $entity_type => $entity_info) { empty($entity_info['
label ']) && var_dump($entity_type, $entity_info); };'
load more v
65%

During create new post on fresh install. Magento ver. 2.2.2,SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'created_at' cannot be null, query was: INSERT INTO mst_blog_post_entity (author_id, type, parent_id, created_at, updated_at) VALUES (?, ?, ?, NULL, '2018-01-21 09:37:18'), Integrations → , Pricing Plans → Compare plans → Contact Sales → Education →

During create new post on fresh install.
Magento ver. 2.2.2

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'created_at' cannot be null, query was: INSERT INTO mst_blog_post_entity (author_id, type, parent_id, created_at, updated_at) VALUES (?, ?, ?, NULL, '2018-01-21 09:37:18')

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'created_at'
cannot be null, query was: INSERT INTO

During create new post on fresh install.
Magento ver. 2.2.2

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'created_at' cannot be null, query was: INSERT INTO mst_blog_post_entity (author_id, type, parent_id, created_at, updated_at) VALUES (?, ?, ?, NULL, '2018-01-21 09:37:18')

 (

During create new post on fresh install.
Magento ver. 2.2.2

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'created_at' cannot be null, query was: INSERT INTO mst_blog_post_entity (author_id, type, parent_id, created_at, updated_at) VALUES (?, ?, ?, NULL, '2018-01-21 09:37:18')

,

During create new post on fresh install.
Magento ver. 2.2.2

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'created_at' cannot be null, query was: INSERT INTO mst_blog_post_entity (author_id, type, parent_id, created_at, updated_at) VALUES (?, ?, ?, NULL, '2018-01-21 09:37:18')

,

During create new post on fresh install.
Magento ver. 2.2.2

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'created_at' cannot be null, query was: INSERT INTO mst_blog_post_entity (author_id, type, parent_id, created_at, updated_at) VALUES (?, ?, ?, NULL, '2018-01-21 09:37:18')

,

During create new post on fresh install.
Magento ver. 2.2.2

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'created_at' cannot be null, query was: INSERT INTO mst_blog_post_entity (author_id, type, parent_id, created_at, updated_at) VALUES (?, ?, ?, NULL, '2018-01-21 09:37:18')

,

During create new post on fresh install.
Magento ver. 2.2.2

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'created_at' cannot be null, query was: INSERT INTO mst_blog_post_entity (author_id, type, parent_id, created_at, updated_at) VALUES (?, ?, ?, NULL, '2018-01-21 09:37:18')

) VALUES( ? , ? , ? , NULL, '2018-01-21 09:37:18')
load more v
75%

Example: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'image' cannot be null (SQL: update `logins` set `name` = testdata, `image` = ?, `logins`.`updated_at` = 2020-12-22 11:29:55 where `id` = 48)

//dd($request->all());

$user = User::create([
   'name' => $request - > name,
   'email' => $request - > email,
   'password' => bcrypt('password')
]);

$profile = Profile::create([
40%

Hi everyone im having this issue with a row on my posts table.,And this is part of my Controller "PostsController" :,if nullable in stickers column add nullable() chain method,This is the Posts Migration file:

This is the form:

                    {{ Form::label('title', ' Título', array('class' => 'fa fa-pencil')) }}

                    {{ Form::text('title', null, array('class' => 'form-control', 'required' => '', 'maxlength' => '255', 'placeholder' => 'Ingresar Título')) }}

                    {{ Form::label('body', ' Contenido', array('class' => 'fa fa-pencil-square-o')) }}

                    {{ Form::textarea('body', null, ['class' => 'form-control', 'required' => '', 'placeholder' => 'Redactar Contenido']) }}

                    {{ Form::submit('Publicar', array('class' => 'btn btn-info')) }}


                </div>
            </div> <!-- col-md-8 end -->


            <div class="col-md-4">
            <div class="input-group">
                <h3 class="text-center">Categoría e imágen</h3>
                <hr>


                     <br> <hr>

                    {{ Form::label('stickers', ' Etiqueta', array('class' => 'fa fa-tags')) }}
                    {{ Form::text('stickers', '', array('class' => 'form-control', 'maxlength' => '20', 'placegolder' => 'Ingresar Etiqueta')) }}

                    <br> <hr>
                    {{ Form::label('postimg', ' Seleccionar Imagen', array('class' => 'fa fa-picture-o')) }}
                    {{ Form::file('postimg') }}

                {!! Form::close() !!}

This is the Posts Migration file:

public
function up() {
   Schema::create('posts', function(Blueprint $table) {
      $table - > increments('id');
      $table - > string('title');
      $table - > string('stickers');
      $table - > text('body');
      $table - > string('postimg');
      $table - > timestamps();
   });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public
function down() {
   Schema::dropIfExists('posts');
}

And this is part of my Controller "PostsController" :

public
function create() {
   return view('posts.create');
}

/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public
function store(Request $request) {
   // validate the data
   $this - > validate($request, array(
      'title' => 'required|max:255',
      'body' => 'required',
   ));

   // store in database
   $post = new Post;

   $post - > title = $request - > title;
   $post - > body = $request - > body;
   $post - > stickers = $request - > stickers;
   $post - > postimg = $request - > postimg;

   $post - > save();

   Session::flash('success', 'La publicación se ha creado correctamente');

   // redirect to another page
   return redirect() - > route('posts.show', $post - > id);
}

/**
 * Display the specified resource.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public
function show($id) {
   $post = Post::find($id);
   return view('posts.show') - > withPost($post);
}

/**
 * Show the form for editing the specified resource.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public
function edit($id) {
   //
}
load more v

Other "column-integrity" queries related to "SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'post' cannot be null"