Integrity constraint violation: 1048 Column 'name' can not be null

4

I'm trying to create a system to upload PDF's and it always returns me the following error:

  

Error Registering! - SQLSTATE [23000]: Integrity constraint violation: 1048 Column 'name' can not 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 do I fix this error?

Another question I have is the following:

How to limit by level of access who can or can not 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 do not know how to limit it with PHP. How to make? Thankful

    
asked by anonymous 23.04.2015 / 06:52

1 answer

3

The error means that you are passing the value to column nome to null and has this field set to NOT NULL in the table.

See if the $titulo variable is populated.

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

It is always good to check if the variables are filled before attempting to insert into the Database.

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

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

Or change the input name="titulo" OU $titulo = $_POST['nome'];

    
23.04.2015 / 10:31