Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Quando preencho o cadastro e clico em cadastrar, a informação não é salva no banco de dados, e também, como pode ver na imagem action2, as informações digitadas permanecem nos campos imput e o nome vai para o alerta de erro de mensagem em baixo do campo....
<?php
require_once "config.php";
$idclient = $fullname = $cpfclient = $gender = $phone = $email = $dayagend = $houragend = "";
$idclient_err = $fullname_err = $cpfclient_err = $gender_err = $phone_err = $email_err = $dayagend_err = $houragend_err = "";
if($_SERVER["REQUEST_METHOD"] == "POST"){
//
$input_idclient = trim($_POST["idclient"]);
if(empty($input_idclient)){
$idclient_err = "ID de identificação do cliente.";
} else{
$idclient = $input_idclient;
}
$input_fullname = trim($_POST["fullname"]);
if(empty($input_fullname)){
$fullname_err = "Por favor, insira o nome completo do cliente.";
/*} elseif(!filter_var($input_fullname, FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/^[a-zA-Z\s]+$/")))){
$fullname_err = "Por favor, insira um nome válido de cliente.";*/
} else{
$fullname = $input_fullname;
}
$input_cpfclient = trim($_POST["cpfclient"]);
if(empty($input_cpfclient)){
$cpfclient_err = "Por favor, insira o CPF do cliente.";
} elseif(!ctype_digit($input_cpfclient)){
$cpfclient_err = "Por favor, coloque apenas números do CPF.";
} else{
$cpfclient = $input_cpfclient;
}
$input_gender = trim($_POST["gender"]);
if(empty($input_gender)){
$gender_err = "Por favor, escolha o gênero do cliente.";
} else{
$gender = $input_gender;
}
$input_phone = trim($_POST["phone"]);
if(empty($input_phone)){
$phone_err = "Por favor, insira o telefone do cliente.";
} elseif(!ctype_digit($input_phone)){
$phone_err = "Por favor, é necessário inserir o telefone de contato do cliente.";
} else{
$phone = $input_phone;
}
$input_email = trim($_POST["email"]);
if(empty($input_email)){
$email_err = "Por favor, insira o e-mail do cliente.";
} else{
$email = $input_email;
}
$input_dayagend = trim($_POST["dayagend"]);
if(empty($input_dayagend)){
$dayagend_err = "Por favor, coloque a data do atendimento do cliente.";
} elseif(!ctype_digit($input_dayagend)){
$dayagend_err = "Por favor, é necessário uma data de atendimento para o cliente.";
} else{
$dayagend = $input_dayagend;
}
$input_houragend = trim($_POST["houragend"]);
if(empty($input_houragend)){
$houragend_err = "Por favor, coloque a data do atendimento do cliente.";
} elseif(!ctype_digit($input_houragend)){
$houragend_err = "Por favor, é necessário uma data de atendimento para o cliente.";
} else{
$houragend = $input_houragend;
}
if(empty($idclient) && empty($fullname_err) && empty($cpfclient_err)&& empty($gender_err)&& empty($phone_err)&& empty($email_err)&& empty($dayagend_err)&& empty($houragend_err)){
$sql = "INSERT INTO schedule (idclient, fullname, cpfclient, gender, phone, email, dayagend, houragend) VALUES (?, ?, ?)";
if($stmt = mysqli_prepare($link, $sql)){
mysqli_stmt_bind_param($stmt, "sss", $param_idclient, $param_fullname, $param_cpfclient, $param_gender, $param_phone, $param_email, $param_dayagend, $param_houragend);
$param_idclient = $idclient;
$param_fullname = $fullname;
$param_cpfclient = $cpfclient;
$param_gender = $gender;
$param_phone = $phone;
$param_email = $email;
$param_dayagend = $dayagend;
$param_houragend = $houragend;
if(mysqli_stmt_execute($stmt)){
header("location: index.php");
exit();
} else{
echo "Ops! Algo deu errado. Por favor, tente novamente mais tarde.";
}
}
if($stmt = mysqli_prepare($link, $sql)){
// [...]
mysqli_stmt_close($stmt);
}
}
mysqli_close($link);
}
?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
<title>Agenda dos Clientes</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<style>
.wrapper{
width: 600px;
margin: 0 auto;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<h2 class="mt-5">Cadastro do Cliente</h2>
<p>Por favor, preencha os campos abaixos para agendar o cliente.</p>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<div class="form-group">
<label>ID DO CLIENTE</label>
<input type="text" name="idclient" class="form-control <?php echo (!empty($idclient_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $idclient; ?>">
<span class="invalid-feedback"><?php echo $idclient_err;?></span>
</div>
<div class="form-group">
<label>NOME COMPLETO</label>
<input type="text" name="fullname" class="form-control <?php echo (!empty($fullname_err)) ? 'is-invalid' : ''; ?>"><?php echo $fullname; ?>
<span class="invalid-feedback"><?php echo $fullname_err;?></span>
</div>
<div class="form-group">
<label>CPF</label>
<input type="text" name="cpfclient" class="form-control <?php echo (!empty($cpfclient_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $cpfclient; ?>">
<span class="invalid-feedback"><?php echo $cpfclient_err;?></span>
</div>
<div class="form-group">
<label>GÊNERO</label>
<input type="text" name="gender" class="form-control <?php echo (!empty($gender_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $gender; ?>">
<span class="invalid-feedback"><?php echo $gender_err;?></span>
</div>
<div class="form-group">
<label>Telefone</label>
<input type="text" name="phone" class="form-control <?php echo (!empty($phone_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $phone; ?>">
<span class="invalid-feedback"><?php echo $phone_err;?></span>
</div>
<div class="form-group">
<label>E-mail</label>
<input type="text" name="email" class="form-control <?php echo (!empty($email_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $email; ?>">
<span class="invalid-feedback"><?php echo $email_err;?></span>
</div>
<div class="form-group">
<label>Agendar Dia</label>
<input type="text" name="dayagend" class="form-control <?php echo (!empty($dayagend_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $dayagend; ?>">
<span class="invalid-feedback"><?php echo $dayagend_err;?></span>
</div>
<div class="form-group">
<label>Agendar Horário</label>
<input type="text" name="houragend" class="form-control <?php echo (!empty($houragend_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $houragend; ?>">
<span class="invalid-feedback"><?php echo $houragend_err;?></span>
</div>
<input type="submit" class="btn btn-primary" value="Submit">
<a href="index.php" class="btn btn-secondary ml-2">Cancel</a>
</form>
</div>
</div>
</div>
</div>
</body>
</html>

>
Em 27/11/2021 at 10:46, Omar~ disse:
Já de cara sem olhar a fundo podemos ver....
Quando isso aqui não estiver vazio entramos na condicional....
Então a variável $idclient será o valor do input não vazio...
Aí nessa condição para ser válida $idclient deve ser vazio:
Só para encerrar, na pagina de update, quando aperto submit para alterar as informações do cadastro, acontece o seguinte erro:
**Fatal error**: Uncaught TypeError: mysqli_stmt_close(): Argument #1 ($statement) must be of type mysqli_stmt, bool given in C:\xampp\htdocs\ERP\update.php:92 Stack trace: #0 C:\xampp\htdocs\ERP\update.php(92): mysqli_stmt_close(false) #1 {main} thrown in **C:\xampp\htdocs\ERP\update.php** on line **92**.
Segue a página de código:
<?php
require_once "config.php";
$name = $cpf = $phone = $gender = $dayagend = $houragend = "";
$name_err = $cpf_err = $phone_err = $gender_err = $dayagend_err = $houragend_err = "";
if(isset($_POST["id"]) && !empty($_POST["id"])){
$id = $_POST["id"];
$input_name = trim($_POST["name"]);
if(empty($input_name)){
$name_err = "Por favor, insira o nome do cliente.";
} elseif(!filter_var($input_name, FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/^[a-zA-Z\s]+$/")))){
$name_err = "Por favor, é necessário inserir o nome do cliente.";
} else{
$name = $input_name;
}
$input_cpf = trim($_POST["cpf"]);
if(empty($input_cpf)){
$cpf_err = "Digite o CPF do cliente a ser agendado.";
} elseif(!ctype_digit($input_cpf)){
$cpf_err = "Por favor, insira apenas números.";
} else{
$cpf = $input_cpf;
}
$input_phone = trim($_POST["phone"]);
if(empty($input_phone)){
$phone_err = "Digite o número de telefone do cliente.";
} else{
$phone = $input_phone;
}
$input_gender = trim($_POST["gender"]);
if(empty($input_gender)){
$gender_err = "Escolha o gênero sexual do cliente.";
} else{
$gender = $input_gender;
}
$input_dayagend = trim($_POST["dayagend"]);
if(empty($input_dayagend)){
$dayagend_err = "Selecione a data de agendamento do cliente.";
} else{
$dayagend = $input_dayagend;
}
$input_houragend = trim($_POST["houragend"]);
if(empty($input_houragend)){
$houragend_err = "Selecione o horário de agendamento do cliente.";
} else{
$houragend = $input_houragend;
}
if (empty($name_err) && empty($cpf_err) && empty($phone_err) && empty($gender_err) && empty($dayagend_err) && empty($houragend_err)){
$sql = "UPDATE schedule SET name=?, cpf=?, phone=?, gender=?, dayagend=?, houragend=?, WHERE id=?";
if($stmt = mysqli_prepare($link, $sql)){
mysqli_stmt_bind_param($stmt, "ssssssi", $param_name, $param_cpf, $param_phone, $param_gender, $param_dayagend, $param_houragend, $param_id);
$param_name = $name;
$param_cpf = $cpf;
$param_phone = $phone;
$param_gender = $gender;
$param_dayagend = $dayagend;
$param_houragend = $houragend;
$param_id = $id;
if(mysqli_stmt_execute($stmt)){
header("location: index.php");
exit();
} else{
echo "Ops! Algo deu errado. Por favor, tente novamente mais tarde.";
}
}
mysqli_stmt_close($stmt);
}
mysqli_close($link);
} else{
if(isset($_GET["id"]) && !empty(trim($_GET["id"]))){
$id = trim($_GET["id"]);
$sql = "SELECT * FROM schedule WHERE id = ?";
if($stmt = mysqli_prepare($link, $sql)){
mysqli_stmt_bind_param($stmt, "i", $param_id);
$param_id = $id;
if(mysqli_stmt_execute($stmt)){
$result = mysqli_stmt_get_result($stmt);
if(mysqli_num_rows($result) == 1){
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
$name = $row["name"];
$cpf = $row["cpf"];
$phone = $row["phone"];
$gender = $row["gender"];
$dayagend = $row["dayagend"];
$houragend = $row["houragend"];
} else{
header("location: error.php");
exit();
}
} else{
echo "Ops! Algo deu errado. Por favor, tente novamente mais tarde.";
}
}
// Close statement
mysqli_stmt_close($stmt);
// Close connection
mysqli_close($link);
} else{
// URL doesn't contain id parameter. Redirect to error page
header("location: error.php");
exit();
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Atualizar Cadastro</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<style>
.wrapper{
width: 600px;
margin: 0 auto;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<h2 class="mt-5">Atualizar Cliente</h2>
<p>Insira as informações para atualizar o cadastro do cliente.</p>
<form action="<?php echo htmlspecialchars(basename($_SERVER['REQUEST_URI'])); ?>" method="post">
<div class="form-group">
<label>NOME</label>
<input type="text" name="name" class="form-control <?php echo (!empty($name_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $name; ?>">
<span class="invalid-feedback"><?php echo $name_err;?></span>
</div>
<div class="form-group">
<label>CPF</label>
<input type="text" name="cpf" class="form-control <?php echo (!empty($cpf_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $cpf; ?>">
<span class="invalid-feedback"><?php echo $cpf_err;?></span>
</div>
<div class="form-group">
<label>TELEFONE</label>
<input type="text" name="phone" class="form-control <?php echo (!empty($phone_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $phone; ?>">
<span class="invalid-feedback"><?php echo $phone_err;?></span>
</div>
<div class="form-group">
<label>GÊNERO</label>
<input type="text" name="gender" class="form-control <?php echo (!empty($gender_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $gender; ?>">
<span class="invalid-feedback"><?php echo $gender_err;?></span>
</div>
<div class="form-group">
<label>DIA AGENDADO</label>
<input type="text" name="dayagend" class="form-control <?php echo (!empty($dayagend_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $dayagend; ?>">
<span class="invalid-feedback"><?php echo $dayagend_err;?></span>
</div>
<div class="form-group">
<label>HORA AGENDADA</label>
<input type="text" name="houragend" class="form-control <?php echo (!empty($houragend_err)) ? 'is-invalid' : ''; ?>" value="<?php echo $houragend; ?>">
<span class="invalid-feedback"><?php echo $houragend_err;?></span>
</div>
<input type="hidden" name="id" value="<?php echo $id; ?>"/>
<input type="submit" class="btn btn-primary" value="Submit">
<a href="index.php" class="btn btn-secondary ml-2">Cancel</a>
</form>
</div>
</div>
</div>
</div>
</body>
</html>
Já de cara sem olhar a fundo podemos ver....
>
23 horas atrás, b2black disse:
<input type="text" name="idclient"
Quando isso aqui não estiver vazio entramos na condicional....
>
23 horas atrás, b2black disse:
>
23 horas atrás, b2black disse:
if(empty($idclient) && empty($fullname_err)