Error undefined, php / mysql

-1

Hello, they give me an error in the following line so that it is not doing it correctly:

$registro2 = $registro->fetchAll();

Full php code:

<?php
include('conexion.php');
$id = $_POST['id-prod'];
$proceso = $_POST['pro'];
$nombre = $_POST['nombre'];
$tipo = $_POST['tipo'];
$precio_uni = $_POST['precio-uni'];
$precio_dis = $_POST['precio-dis'];
$fecha = date('Y-m-d');
//VERIFICAMOS EL PROCESO
$query='';
require_once 'conexion.php';
$database_connection = database_connect();

switch($proceso){
    case 'Registro':
        $database_connection->query("INSERT INTO productos (nomb_prod, tipo_prod, precio_unit, precio_dist, fecha_reg)VALUES('$nombre','$tipo','$precio_uni','$precio_dis', '$fecha')");
    break;

    case 'Edicion':
        $database_connection->query("UPDATE productos SET nomb_prod = '$nombre', tipo_prod = '$tipo', precio_unit = '$precio_uni', precio_dist = '$precio_dis' WHERE id_prod = '$id'");
    break;
}


//ACTUALIZAMOS LOS REGISTROS Y LOS OBTENEMOS

$registro = $database_connection->query("SELECT * FROM productos ORDER BY id_prod ASC");

//CREAMOS NUESTRA VISTA Y LA DEVOLVEMOS AL AJAX

echo '<table class="table table-striped table-condensed table-hover">
            <tr>
                <th width="300">Nombre</th>
                <th width="200">Tipo</th>
                <th width="150">Precio Unitario</th>
                <th width="150">Precio Distribuidor</th>
                <th width="50">Opciones</th>
            </tr>';
$registro2 = $registro->fetchAll();
foreach ($registro2 as $registro){
    echo '<tr>
                <td>'.$registro2['nomb_prod'].'</td>
                <td>'.$registro2['tipo_prod'].'</td>
                <td>S/. '.$registro2['precio_unit'].'</td>
                <td>S/. '.$registro2['precio_dist'].'</td>
                <td><a href="javascript:editarProducto('.$registro2['id_prod'].');" class="glyphicon glyphicon-edit"></a> <a href="javascript:eliminarProducto('.$registro2['id_prod'].');" class="glyphicon glyphicon-remove-circle"></a></td>
                </tr>';
}

echo '</table>';
?>

The rest works well for me. It is a jquery registry with php and page. Basically what happens to me when I show with the foreach I get the following error, if I show a field nothing happens but if I show several it gives me this:

  

Notice: Undefined index: name_prod in   C: \ wamp \ www \ www \ registration \ php \ add_product.php on line 42

    
asked by Perl 15.09.2016 в 02:10
source

1 answer

1

In the foreach documentation you can see that the format is:

foreach (expresión_array as $valor) {
    sentencias usando $valor

But in your code you are doing

foreach (expresión_array as $valor) {
    sentencias usando expresión_array

That is incorrect, because $registro2 (your expresión_array ) does not contain the parameters you are trying to access; is $registro (your $valor ) which contains them and which you should use within foreach . Instead of doing this:

            <td>'.$registro2['nomb_prod'].'</td>
            <td>'.$registro2['tipo_prod'].'</td>
            <td>S/. '.$registro2['precio_unit'].'</td>
            <td>S/. '.$registro2['precio_dist'].'</td>

You should be doing this other:

            <td>'.$registro['nomb_prod'].'</td>
            <td>'.$registro['tipo_prod'].'</td>
            <td>S/. '.$registro['precio_unit'].'</td>
            <td>S/. '.$registro['precio_dist'].'</td>
  

Apart from that, your code has serious security vulnerabilities and may suffer SQL injection attacks. You should use prepared statements instead of dynamic ones.

    
answered by 15.09.2016 в 05:43