Notice: Undefined index

0

I have a problem with a form, I send the variables by post and keep in trouble, but I get a warning Notice: Undefined index: afterwards in C: \ wamp64 \ www \ model \ edit_productos2.php on line 47 and I do not know what can be wrong

<form action="" method="post">
                        <table border="1" align="center" id='usuarios' cellspacing="1" cellpadding="1">
                            <tr>
                                <th>Producto</th>
                                <th>Cantidad</th>
                                <th>Ingreso</th>
                            </tr>
                            <tr class='item2'>

                                <td>
                                    <input type="text" class="cajas" value="<?php echo $row->nombre; ?>" disabled>
                                </td>
                                <td>
                                    <input type="text" class="cajas" value="<?php echo $row->existencia; ?>" disabled> 
                                </td>
                                <td>
                                    <input type="number" id="despues" class="cajas" value="0" name="despues" onchange="return validanumero(this)" required>
                                </td>
                        </table>
                        </br></br>
                        <input type="submit" class="boton" name="submit" value="Aceptar">&nbsp;&nbsp;
                        <input id="boton" class="boton" type="button" value="Cancelar" onclick="window.location = '../vista/administracion_productos.php'">
                    </form>
                    <?php
                    $antes = $row->existencia;
                    $var = $_POST['despues'];
                    $total = $antes+$var;
                    if (isset($_POST['submit'])) {
                        $field = array("existencia" => $total);
                        $tbl = "productos";
                        edit($tbl, $field, 'id_prod', $id);
                        echo "<script language='javascript'>window.location='../vista/administracion_productos.php'</script>";
                    }
                    ?>
                </div>

The code of the page that records the change in the bd

function edit($tblname, $form_data, $field_id, $id) {
$sql = "UPDATE " . $tblname . " SET ";
$data = array();

foreach ($form_data as $column => $value) {

    $data[] = $column . "=" . "'" . $value . "'";
}
$sql .= implode(',', $data);
$sql .= " where " . $field_id . " = '" . $id . "'";
return db_query($sql);
}

in line 47 is where I assign the field to a variable to operate it

$var = $_POST['despues'];

Thank you very much for your collaboration

    
asked by Nestor Bautista 20.02.2018 в 17:11
source

1 answer

0

The problem is that you are trying to use the index "after" in the variable $ _POST which without sending the data does not exist. There are several ways to solve the problem, the easiest is to check if the variable exists for use with the isset function.

$var = (isset($_POST['despues']) ? $_POST['despues'] : 0);

In your case you could move the line to the section within the condition if it verifies that the form has been sent, your code would look like this:

<?php

                    if (isset($_POST['submit'])) {
                        $antes = $row->existencia;
                    $var = $_POST['despues']; // ESTA LINEA LA MOVÍ
                    $total = $antes+$var;     // ESTA LINEA LA MOVÍ
                        $field = array("existencia" => $total);
                        $tbl = "productos";
                        edit($tbl, $field, 'id_prod', $id);
                        echo "<script language='javascript'>window.location='../vista/administracion_productos.php'</script>";
                    }
                    ?>
    
answered by 20.02.2018 / 22:06
source