Integrity Constraint Violation: 1048 Column 'name' cannot be null

Asked

Viewed 7,965 times

4

I am trying to create a system to upload PDF’s and always returns me the following error:

Error Registering Record! - SQLSTATE[23000]: Integrity Constraint Violation: 1048 Column 'name' cannot be null

and with all fields returns the same error

The script is this:

<?php require("header.php");?>
<?php require("menu.php");

if(isset($_POST['bt_save_df']))
{
    $titulo = $_POST['titulo'];

    $uploaddir = 'desenhos/arquivos/';
    $pastadestino = 'desenhos/arquivos/';
    // Pega a extensão do arquivo
    $dividir2 = end(explode(".", $_FILES['imagem1']['name']));

    // Gera um nome aleatório
    $nome = time();
    $nome_imagem2 = $nome . '1.' . $dividir2;
    $uploadfile = $uploaddir . $nome . '1.' . $dividir2;
    if (move_uploaded_file($_FILES['imagem1']['tmp_name'], $uploaddir . $nome_imagem2)) {

    }

    $arquivo = $pastadestino.$nome_imagem2;


    $sql = 'INSERT INTO tbl_desenho (nome, arquivo, codigo, codcontrol, tipo, material, denominacao)';
    $sql .= ' VALUES (:nome, :arquivo, :codigo, :codcontrol, :tipo, :material, :denominacao)';

    try{

        $create = $db->prepare($sql);
        $create->bindValue(':nome', $titulo, PDO::PARAM_STR);
        $create->bindValue(':arquivo', $arquivo, PDO::PARAM_STR);
        $create->bindValue(':codigo', $codigo, PDO::PARAM_STR);
        $create->bindValue(':codcontrol', $codcontrol, PDO::PARAM_STR);
         $create->bindValue(':tipo', $tipo, PDO::PARAM_STR);
        $create->bindValue(':material', $material, PDO::PARAM_STR);
        $create->bindValue(':denominacao', $denominacao, PDO::PARAM_STR);

        if($create->execute() ){
            echo '<script type="text/javascript" >
                    alert( "Registro Cadastrado com Sucesso!"); location.href="ger_arquivos.php";
            </script>';

        }


    } catch (PDOException $e) {
            echo "Erro ao Cadastrar Registro! - " . $e->getMessage();
    }
}

?>


                <!-- BEGIN Main Content -->
                <div class="row-fluid">
                    <div class="span12">
                        <div class="box">
                            <div class="box-title">
                                <h3><i class="icon-reorder"></i> Cadastro de Desenhos</h3>
                                <div class="box-tool">
                                    <a data-action="collapse" href="#"><i class="icon-chevron-up"></i></a>
                                    <a data-action="close" href="#"><i class="icon-remove"></i></a>
                                </div>
                            </div>
                            <div class="box-content">
                                <form action="cad_arquivo.php" class="form-horizontal" id="validation-form" method="post" enctype="multipart/form-data">   
                                    <div class="control-group">
                                        <label class="control-label" for="titulo">Titulo:</label>
                                        <div class="controls">
                                            <div class="span12">
                                                <input type="text" name="nome" id="nome" class="input-xlarge" />
                                            </div>
                                        </div>
                                    </div>

                                    <div class="control-group">
                                        <label class="control-label" for="codigo">Código:</label>
                                        <div class="controls">
                                            <div class="span12">
                                                <input type="text" name="codigo" id="codigo" class="input-xlarge" />
                                            </div>
                                        </div>
                                    </div>

                                    <div class="control-group">
                                        <label class="control-label" for="titulo">Código de Controle:</label>
                                        <div class="controls">
                                            <div class="span12">
                                                <input type="text" name="codcontrol" id="codcontrol" class="input-xlarge" />
                                            </div>
                                        </div>
                                    </div>

                                    <div class="control-group">
                                        <label class="control-label" for="select">Tipo:</label>
                                        <div class="controls">
                                            <div class="span12">
                                                <select name="tipo" id="tipo">
                                                    <option value=""> -- Selecione --</option>
                                                    <option value="Produção">Produção</option>
                                                    <option value="Circuito de freios">Kits</option>
                                                </select>
                                            </div>
                                        </div>
                                    </div>

                                    <div class="control-group">
                                        <label class="control-label" for="titulo">Material:</label>
                                        <div class="controls">
                                            <div class="span12">
                                                <input type="text" name="material" id="material" class="input-xlarge" />
                                            </div>
                                        </div>
                                    </div>

                                    <div class="control-group">
                                        <label class="control-label" for="titulo">Denominação:</label>
                                        <div class="controls">
                                            <div class="span12">
                                                <input type="text" name="denominacao" id="denominacao" class="input-xlarge" />
                                            </div>
                                        </div>
                                    </div>


                                    <div class="control-group">
                                      <label class="control-label">Arquivo</label>
                                      <div class="controls">
                                         <div class="fileupload fileupload-new" data-provides="fileupload">
                                            <div class="input-append">
                                               <div class="uneditable-input">
                                                  <i class="icon-file fileupload-exists"></i> 
                                                  <span class="fileupload-preview"></span>
                                               </div>
                                               <span class="btn btn-file">
                                                   <span class="fileupload-new">Selecione o arquivo</span>
                                                   <span class="fileupload-exists">Alterar</span>
                                                   <input type="file" class="default" name="imagem1" id="imagem1"/>
                                               </span>
                                               <a href="#" class="btn fileupload-exists" data-dismiss="fileupload">Remove</a>
                                            </div>
                                         </div>
                                      </div>
                                   </div>



                                    <div class="form-actions">
                                        <input type="submit" class="btn btn-primary" name="bt_save_df" value="Salvar">
                                        <a href="ger_arquivos.php"><button type="button" class="btn">Voltar</button></a>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
                <!-- END Main Content -->

<?php require("footer.php");?>

How to remedy this error?

Another question I have is this::

How to limit by access level who can and cannot access this page? I made a column in the table tbl_acesso called permission, the value should go from 1 to 5, which will set the access level, but I don’t know how to limit it with PHP. How to do it? Grateful

1 answer

3


Error means you are passing the value to the column nome to null and has this field defined in the table as NOT NULL.

See if the variable $titulo is filled.

$create->bindValue(':nome', $titulo, PDO::PARAM_STR);

It is always good to check if the variables are filled in before trying to enter in the database.

$titulo = filter_input(INPUT_POST , 'titulo');
if(!empty($titulo))
{
    //insere na base de dados
}

I just noticed you have the input of the title with name="nome" and when fetching the variable you use $titulo = $_POST['titulo'];

Or do you change the input name="titulo" OR the $titulo = $_POST['nome'];

  • The value is not being passed to the variables, I do not understand the reason. What may be happening?

  • Look at my question.

  • Now you are returning the SQLSTATE[HY093] error: Invalid Parameter number: Parameter was not defined

  • @user21746 You have bindValue(':titulo',... and then in query you have VALUES (:nome that’s the problem, they have to be the same.

  • I left as "name" in everything, now the error is Registration Error! - SQLSTATE[23000]: Integrity Constraint Violation: 1048 Column 'codigo' cannot be null

  • @user21746 See the code variable to see if it is filled. This one you should know by yourself, was the reason for the question.

  • @user21746 Let’s go continue this discussion in chat.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.