0
I’m trying to make a insert
from a photo to my database but every time I finish filling the fields and enter the error:
Field 'foto' doesn't have a default value
I couldn’t find out if the lack of value was in the database or in the HTML itself but I noticed that the user and password fields are still entered except for the photo field.
Follow the code lines below next to the database table I am entering
index php.
<html>
<head>
<title>Cadastro paciente</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
</head>
<body>
<?php include_once 'conexao.php';
if(isset($_POST['usuario'])&& isset($_POST['senha'])&&isset($_POST['foto'])){
$nome= $_POST['usuario'];
$senha= $_POST['senha'];
$foto= $_POST['foto'];
try{
$username='root';
$password='';
$pdo= new PDO('mysql:host=localhost;dbname=pit',$username,$password);
$pdo->setAttribute(PDO::AFTER_ERRMODE,PDO::ERRMODE_EXPECTATION);
$stmt=$pdo->prepare('INSERT INTO login_e_cadastro VALUES(:usuario,:senha,:foto)');
$stmt->execute(array(
':usuario'=>$usuario,
':senha'=>$senha,
':foto'=>$foto,
));
if ($stmt->rowCount() > 0) {
echo "<script>alert('paciente cadastrado');</script>";
}
} catch(PDOException $e){
echo 'Error:'.$e->getMessage();
}
}
?>
<div class="container">
<div class="row">
<div class="col-12">
<div class="card-body space">
<h3 align="center" class="card-title">Cadastro Paciente</h3>
<form action="" method="post" >
<div class="form-group">
<label>Nome:</label>
<input type="usuario" class="form-control" name="usuario" required>
</div>
<div class="form-group">
<label>senha:</label>
<input type="password" class="form-control" name="senha" required>
</div>
<input type="file" name="foto">
<div align="center">
<input type="submit" value="cadastrar-se" class="btn btn-primary text-center" name="salvar">
<a class="btn btn-danger text-center" href="javascript:history.back()"> Voltar</a>
</div>
</form>
</div>
</div>
</div>
</div>
</body>
</html>
php connection.
<?php
$mysqli = new mysqli('localhost', 'root', '', 'pit') or die(mysqli_error($mysqli));
if (isset($_POST['salvar'])) {
$usuario = $_POST['usuario'];
$senha = $_POST['senha'];
$mysqli->query("INSERT INTO login_e_cadastro (usuario, senha) VALUES('$usuario','$senha')") or die($mysqli->error);
}
?>
Related: Help - PHP Specific Upload
– Icaro Martins
May also be useful: PHP: Manage file upload
– Icaro Martins
In PHP you are only entering user and password
$mysqli->query("INSERT INTO login_e_cadastro (usuario, senha) VALUES('$usuario','$senha')")
There’s no mention of the photo. Another thing, in your form the action property is empty that means you are passing the request to the server via javascript (Httprequest or Jquery Ajax), it would be good to put this code in Javascript in the question.– Augusto Vasques
Related: Mysqli vs PDO - which is the most recommended to use?
– Icaro Martins