How to add the content of an input to a data in a MySQL table in php?

2

I have a database on my main computer filled with records of different products, I needed to perform a search and after the product was found through the bar code it would proceed to add an amount entered by a user, the part of the connection to the database works for me and the same to search, the part of the sum is the one that I can not specify. in my search engine I have this code:

<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <title>Buscador</title>
</head>
<body>
    <form action="buscador.php" method="POST" autocomplete="off">
        <input type="text" name="palabra" placeholder="¿Qué esta buscando?">
        <input type="submit" value="Buscar">
    </form>
    <?php
        if(isset($_POST['palabra'])){
            require_once "php/connect.php";
            require_once "procesos/buscar.php";
        }
    ?>
     <form action="buscador.php" method="POST" autocomplete="off">
        <input type="text" name="conteo" placeholder="Cantidad">
        <input type="submit" value="Sumar">
        </form>
        <?php
            if(isset($_POST['conteo'])){
                require_once "php/connect.php";
                require_once "procesos/conteo.php";
            }
    ?>
 </body>
</html>'

In the count.php I have this code:

<?php
$conteo=$_POST['conteo'];
$palabra=$_POST['palabra'];
$query= "UPDATE inventario_principal SET conteo = conteo + '%$conteo%' WHERE codigo = '%$palabra%'";

But I still can not make it work, I'm pretty new to php and I would like to know what the problem is. The error it gives me is that the variable "Word" has no index but I think it is declared in the first form.

    
asked by Jeffrey Cedeño 18.05.2018 в 00:08
source

1 answer

1

The error occurs because you would have to add an input in the second form for the word field, since each form is independent of each other.

On the other hand, I think you can put it another way and save a couple of lines of code, by calling the PHP file directly from the form.

In your form add the file "count.php" in action. You must also add an input for the word variable.

<form action="conteo.php" method="POST" autocomplete="off">
 <input type="text" name="palabra" placeholder="¿Qué esta buscando?">
 <input type="text" name="conteo" placeholder="Cantidad">
 <input type="submit" value="Sumar">
</form>

Edit the file count.php to include the connection to the database and make the query from there.

if (isset($_POST['submit'])) {
 include_once 'conexion.php';

 $conteo=mysqli_real_escape_string($conn, $_POST['conteo']);
 $palabra=mysqli_real_escape_string($conn, $_POST['palabra']);

 $query= "UPDATE inventario_principal SET conteo = conteo + '$conteo' WHERE
 codigo = '$palabra'";
 $result = mysqli_query($conn, $query);
} else {
 header("Location: ./index.php");
 exit();
}

This way it should work. The variable $ conn comes from conexion.php, you can adapt it according to how you establish the connection in your project.

    
answered by 18.05.2018 / 12:41
source