1
Good guys, I need help with the following: I want to insert 3 simple information in the database, product name, price and image. Outside the form I opened a tag to show the output, but it only shows the name! and in the database no new records appear, only the ones I added directly in via SQL commands. here is the code:
<?php
include('config.php');
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="author" content="Publio Elon">
<meta name="description" content="Curso de HTML5 e CSS3">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=yes">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<title>Title of the document</title>
</head>
<body>
<form method="POST" action="">
<div class="form-group">
<label for="produto">Produto</label>
<input type="text" name="p_name" class="form-control" id="produto" aria-describedby="cadProduto" placeholder="Nome do Produto">
<small>Lembre-se de capitalizar a Primeira letra do nome do produto</small>
</div>
<div class="form-group">
<label for="preço">Preço do Produto</label>
<input type="text" name="image"class="form-control" id="preço" placeholder="Preço">
<small>Preço do produto deve ser dado apenas em números e ponto.</small>
</div>
<div class="form-group">
<label for="preço">Imagem do produto</label>
<input type="text" name="price" class="form-control" id="preço" placeholder="Nome da Imagem">
<small>Exemplo: teste.jpg ou teste.png verificar a extensão das imagens.</small>
</div>
<button type="submit" name="submit" class="btn btn-primary">Cadastrar</button>
</form>
<?php
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
$p_nameErr = $priceErr = $imgErr ="";
$p_name = $price = $image ="";
$txtGet = filter_input_array(INPUT_POST, FILTER_DEFAULT);
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($txtGet["p_name"])) {
$p_nameErr = "Nome do produto esta vazio";
} else {
$p_name = test_input($txtGet["p_name"]);
if (!preg_match("/^[a-zA-Z]*$/", $p_name)) {
$p_name = "Apenas letras e espaço permitido";
}
}
}
echo "<h2>Sáida</h2>";
echo "$p_name";
echo "<br>";
echo "$image";
echo "<br>";
echo "$price";
echo "<br>";
if(isset($_POST["submit"])){
$sql = "INSERT INTO products(p_name, image, price) values('$p_name', '$image', '$price')";
$result = mysqli_query($link, $sql);
}
echo "Produto cadastrado com sucesso!!";
?>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</body>
</html>
config.php
<?php
session_start();
$link = mysqli_connect("localhost", "root", "admin");
mysqli_select_db($link, "loja");
?>
If I understand correctly, the variables you display only receive an empty string, but never the form values, then you have nothing to show anyway; and your record in the database does not happen because it is never satisfied the condition of your If. I leave it to my homework to find out why.
– Woss
I realized where is the problem, I started to pass the parameter to capture only the name, and the regex is not suitable, but one thing that leaves me confused, if it only receives empty strings, because it does not add even the blanks in the BD?
– John Jones
After I passed the parameters and rewritten the regular expressions without laziness, I was able to correct all the mistakes, thank you very much, that’s what you said!
– John Jones