Vehicle registration with PHP and Mysql

Asked

Viewed 132 times

1

Good morning, I’m starting in php and Mysql. I’m trying to develop a system for registering a vehicle (with photo), and studying a little, I got into php language together with MYSQL. It turned out that I generated my scripts, but no error message appears and does not save the data in my database. Can anyone help me?

index.php file

<?php

    include("classe/conexao.php");
    $marca = isset( $_POST ['marca']) ? $_POST['marca']:null;
    $modelo = isset( $_POST ['modelo']) ? $_POST['modelo']:null;
    $ano = isset( $_POST ['ano']) ? $_POST['ano']:null;
    $msg = false;
  if(isset($_FILES['arquivo'])){
    $extensao = strtolower(substr($_FILES['arquivo']['name'], -4)); //pega a extensao do arquivo
    $novo_nome = md5(time()) . $extensao; //define o nome do arquivo
    $diretorio = "upload/"; //define o diretorio para onde enviaremos o arquivo
    move_uploaded_file($_FILES['arquivo']['tmp_name'], $diretorio.$novo_nome); //efetua o upload

    $sql_code = "INSERT INTO veiculos (id, marca, modelo, ano, arquivo, data) VALUES(null, '$marca', '$modelo', '$ano', '$novo_nome', NOW())";
    if($mysqli->query($sql_code))
      $msg = "Arquivo enviado com sucesso!";
    else
      $msg = "Falha ao enviar arquivo.";
  }
     ?>

HTML

<html>
<title>Cadastro de veículos</title>
<body>
<?php if(isset($msg) && $msg != false) echo "<p> $msg </p>"; ?>
    <form action="index.php" method="POST" enctype="multipart/form-data">
        Marca: <input name="marca" type="text">
        Modelo: <input name="modelo" type="text">
        Ano: <input name="ano" type="text">
        Arquivo: <input type="file" required name="arquivo">
        <input type="submit" value="Cadastrar">
    </form>
</body>
</html>

File class/connected.php

<?php
    $host = "localhost";
    $usuario = "root";
    $senha = "";
    $bd = "sitemoraes";
    $mysqli = new mysqli($host, $usuario, $senha, $bd);
        if($mysqli->connect_errno)
            echo "Falha na conexão: (".$mysqli->connect_errno.") ".$mysqli->connect_error;
?>

Print of the database: Print do banco de dados

-EDIT- After the changes that Jackson and anonymity recommended, the script still doesn’t work. Does not save anything in the database and returns the "Failed to send file."

  • Hello Guga, first thing, leave it this way: $sql_code = "INSERT INTO veiculos (marca, modelo, ano, arquivo, data) VALUES('$marca', '$modelo', '$ano', '$novo_nome', NOW())";, like the ID is an auto increment, you should not send it in the code.

  • Like the field id is a primary key it can not be NULL. In his INSERT change NULL for DEFAULT or in the manner indicated by Jackson.

  • Thanks for your help with the code.. I did it Jackson’s way and yours anonymously, but in both cases now appears the message "Failed to send file." and does not send anything to the database.

  • Guga, give a var_dump($_FILES['arquivo']) and see if this guy is returning anything... I suspect that the fact that no message appears may be the fact that he is not entering the if()...

  • Jacson, var dump is giving: 'name' => string 'Moraesaltini.png' (length=16) 'type' => string 'image/png' (length=9) 'tmp_name' => string 'C: wamp64 tmp phpEFD.tmp' (length=24) 'error' => int 0 'size' => int 607658 'size' => int 0

  • Check the values of $marca, $modelo, $ano, $novo_nome before entering into the database. All your columns are NOT NULL. Modify your script to use Prepared statements because the way you’re doing your code is susceptible to attack.

Show 1 more comment
No answers

Browser other questions tagged

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