Problem in including data through a form using PHP and Mysql

Asked

Viewed 50 times

0

<?php
define('HOST', 'localhost');
define('USUARIO', 'root');
define('SENHA', '');
define('DB', 'login');

$conexao = mysqli_connect(HOST, USUARIO, SENHA, DB) or die ('Nao foi possivel conectar');

I have a problem to make an entry in the database, whenever I try to send an error where the page is redirected but it is not done the actual insertion in the table,Besides, it is a reused code of the same project only made changes to the insertions and variables if it is useful I am using bootstrap 4 and Jquery 3.4.1

<?php
include("conexao.php");

$produto = mysqli_real_escape_string($conexao, trim($_POST['produto']));
$quantidadeLote = mysqli_real_escape_string($conexao, trim($_POST['quantidadeLote']));
$precoCompra = mysqli_real_escape_string($conexao, trim($_POST['precoCompra']));
$preceVenda = mysqli_real_escape_string($conexao, trim($_POST['preceVenda']));
$fornecedor = mysqli_real_escape_string($conexao, trim($_POST['fornecedor']));

$sql = "INSERT INTO produtos(produto, quantidadeLote, precoCompra, precoVenda, fornecedor) VALUES ('$produto',
'$quantidadeLote', '$precoCompra', '$preceVenda', 'fornecedor')";

$conexao->close();

header('Location: produtos.php');
exit;
?>
<?php
include ('menu.php');
?>
<link rel="stylesheet" type="text/css" href="./assets/css/painel.css">

<main role="main" class="col-md-9 ml-sm-auto col-lg-10 px-4">
	<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
		<h1 class="h2">Cadastro de Produtos</h1>
	</div>
  <div id="container">

  	<form action="processaCP.php" action="POST">
  	  <div class="form-row">
  	      <div class="form-group col-md-6">
  	        <label for="Produto">Produto</label>
  	        <input type="text" class="form-control" name="produto" placeholder="Produto">
  	      </div>
  	      <div class="form-group col-md-3">
  	        <label for="Quantidade">Quantidade Lote</label>
  	        <input type="text" class="form-control" name="quantidadeLote" placeholder="100">
  	      </div>
  	    </div>
  	    <div class="form-row">
	  	    <div class="form-group col-md-3">
	  	    	<label for="ComraUnidade">Preço de Compra Unidade</label>
				    <input type="text" class="form-control" name="precoCompra" placeholder="R$0,00">
	  	    </div>
          <div class="form-group col-md-3">
    				<label for="VendaUnidade">Preço de Venda Unidade</label>
    				<input type="text" class="form-control" name="preceVenda" placeholder="R$0,00">
			</div>
			<div class="form-group col-md-6">
			  <label for="Fornecedor">Fornecedor</label>
			  <input type="text" class="form-control" name="fornecedor">
			</div>
		</div>
		<br>	
		<div class="input-group-prepend" style="display:inline-block; vertical-align: middle;">
      <button class="btn btn-outline-success" type="submit">Savar</button>
      &ensp;<!-- espaço -->
      <button class="btn btn-outline-secondary" type="reset">Cancelar</button>
    </div>
  	</form>
  	<br>
  	<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
  		<h1 class="h3">Lista de Produtos</h1>
  	</div>
  </div>
    <span id="conteudoP"></span>
 </main>

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
 <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
 <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
 <script type="text/javascript" src="./assets/js/painel.js"></script>
 <script type="text/javascript" src="./assets/js/jquery-3.4.1.min.js"></script>
 <script type="text/javascript" src="./assets/js/jquery.mask.min.js"></script>
 <script>
     $(document).ready(function () {
       $.post('listarP.php', function(retorna){
         //Subtitui o valor no seletor id="conteudo"
         $("#conteudoP").html(retorna);
       });
     });
   </script>

  • post the error that occurs, also include the other files . php

  • in your $connection, you’re not giving commit, wouldn’t that be it?

  • @The error that occurs is: Object not found! The requested URL was not found on this server. The link on the referred page seems to be in error or out of date. Please inform the author of this page about the error. If you believe you have encountered a problem on the server, please contact the webmaster. Error 404 localhost Apache/2.4.39 (Win64) Openssl/1.1.1c PHP/7.3.6

  • when you click Save, it gives a post to "processaCP.php", it should not be finding that file in the server address.

  • plus this Cód is the 'copy' of an outo Cód that worked perfectly

  • the issue is not the code, it is the address of the server, for some reason it is not hitting the right address, try to put the address in the browser and check if it hits this file . php

  • does not think it shows the error Notice: Undefined index: product in C: xampp htdocs drugstore processCP.php on line

  • post the file code process CP.php, check with the Google Chrome inspector or other browser for network information, and see if Save loses any link

  • processaCP.php ja esta inckuido is the first code above html plus i vo take a look at google inpertor thanks

  • post the related file code.php, can you tell us where this error message you refer to comes from? Object not found!

Show 5 more comments

2 answers

0

This is Emerson, all right? I took a quick look at your code and noticed that when creating your SQL, you do not inform the last field as variable, you put "vendor" instead of "$vendor".

Another detail, I don’t know if PHP recognizes the variable of your text the way you put it, normally I put the variable inside keys {}

You put it like this:

$sql = "INSERT INTO produtos(produto, quantidadeLote, precoCompra, precoVenda, fornecedor) VALUES ('$produto',
'$quantidadeLote', '$precoCompra', '$preceVenda', 'fornecedor')";

That way you used I don’t remember working, I know it works like this:

$sql = "INSERT INTO produtos(produto, quantidadeLote, precoCompra, precoVenda, fornecedor) VALUES ('{$produto}', '{$quantidadeLote}', '{$precoCompra}', '{$preceVenda}', '{$fornecedor}')";

I hope I helped, a hug.

0

As you said the code was copied from another working service, your problem can only be with the BD user. Check the permissions designed (read, write, update, delete data, modify tables, etc) to register users in Mysql.

TIP: Abandon mysqli, migrate to PDO (PHP Data Object).

Browser other questions tagged

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