Error Connecting to MySQL with PHP and AJAX Requests

1

I am in a dilemma, so far the code has worked in such a way that it does not return anything and when I return the information to execute the connection and the SQL statement returns null spaces to me.

<?php 

    require_once "../../clases/Conexion.php";
    require_once "../../clases/Clientes.php";
    require_once "../../clases/Articulos.php";

    $c = new conectar();
    $conexion = $c->conexion();

 ?>

<h4 class="mt-3">Vender un Producto</h4>
<div class="row">
    <div class="col-sm-4">
        <form action="" id="frmVentasDeProductos">
            <label for="Cliente">Cliente</label>
            <select class="form-control form-control-sm" id="cliente" name="cliente">
                <option value="A">Selecciona una Opción</option>
                <option value="0">Sin Cliente</option>
                <?php 

                    $sql = "SELECT id_cliente, nombre, apellido FROM clientes";

                    $result = mysqli_query($conexion, $sql);

                    while($r = mysqli_fetch_row($result)) :
                 ?>
                <option value="<?php echo $r[0] ?>"><?php echo $r[1] ?> <?php echo $r[2] ?></option>
                <?php endwhile; ?>
            </select>
            <label for="Artículo">Artículo</label>
            <select class="form-control form-control-sm" id="producto" name="producto">
                <option value="A">Selecciona una Opción</option>
                <?php 
                    $sql = "SELECT id_articulo, nombre FROM articulos";

                    $result = mysqli_query($conexion, $sql);

                    while($r = mysqli_fetch_row($result)) :
                 ?>
                <option value="<?php echo $r[0] ?>"><?php echo $r[1] ?></option>
                <?php endwhile; ?>
            </select>
            <label for="Descripción">Descripción</label>
            <textarea readonly="" name="descripcion" id="descripcion" class="form-control"></textarea>
            <label for="Cantidad">Cantidad</label>
            <input readonly="" type="text" class="form-control form-control-sm" id="cantidad" name="cantidad">
            <label for="Precio">Precio</label>
            <input readonly="" type="text" class="form-control form-control-sm" id="precio" name="precio">
            <span class="btn btn-primary btn-sm btn-block mt-2" id="btnAgregarVenta">Agregar</span>
        </form>
    </div>
    <div class="col-sm-3">
        <div class="" id="imagen_articulo"></div>
    </div>
</div>

<script>
    $(document).ready(function() {
        $('#producto').change(function(){
            $.ajax({
                type:"POST",
                data:"id_articulo=" + $('#producto').val(),
                url:"../procesos/ventas/llenarFormProducto.php",
                success:function(r){
                    console.log(r);
                    // dato=jQuery.parseJSON(r);

                    // $('#descripcionV').val(dato['descripcion']);
                    // $('#cantidadV').val(dato['cantidad']);
                    // $('#precioV').val(dato['precio']);

                    // $('#imgProducto').prepend('<img class="img-thumbnail" id="imgp" src="' + dato['ruta'] + '" />');
                }
            });
        });
    });
</script>

<!-- Script Select 2 -->
<script>
    $(document).ready(function() {
        $('#cliente').select2();
        $('#producto').select2();
    });
</script>

The code has already been added together with the AJAX request

Here is the SCRIPT code, where you will make the AJAX request every time you change, the request is made to the file fillFormProducto.php and print through the console what contains the variable r that arrives as parameter

require_once "../../clases/Conexion.php";
require_once "../../clases/Ventas.php";

$obj= new ventas();

echo json_encode($obj->obtenDatosProducto($_POST['id_articulo']));

In this file I include the files Conexion.php and Ventas.php, in turn, I create a new object of the sales class () and call its function obtainProductData (), which takes as a parameter the product ID of the product. ComboBox, and I print it in json format

<?php 

    class ventas
    {

        public function obtenDatosProducto($id_articulo)
        {
            $c = new conectar();
            $conexion=$c->conexion();

            $sql="SELECT art.nombre,
            art.descripcion,
            art.cantidad,
            img.ruta,
            art.precio 
            from articulos as art 
            inner join imagenes as img
            on art.id_imagen=img.id_imagen 
            AND art.id_articulo='$id_articulo'";
            $result=mysqli_query($conexion,$sql);
            // $ver=mysqli_fetch_row($result);
            // $d=explode('/', $ver[3]);
            // $img=$d[1].'/'.$d[2].'/'.$d[3].''.$d[4];
            // $data=array(
            //  'nombre' => $ver[0],
            //  'descripcion' => $ver[1],
            //  'cantidad' => $ver[2],
            //  'ruta' => $img,
            //  'precio' => $ver[4]
            // );       
            return $result;
        }
    }

Here is the function where I execute the SQL snetencia and create a new object of the connection class and when returning what contains the variable $ result = mysqli_query ($ connection, $ sql);

I get the following

It's something I do not understand, I've been working in the same way, but in the sales module this error persists me, I tried to register more records of other modules and they continue to work correctly. I do not know why he returns all the null fields to me.

One thing that I need to add the structure of my files

    
asked by Alan Amaury Escobedo García 19.03.2018 в 02:12
source

1 answer

1

Your method obtenDatosProductos() has to retrieve the results, because what you return now is an object mysqli_result .

To store the results and return them you can do something like this:

This code should go in obtenDatosProductos() . You must remove the variable $result that you have and the return $result , putting that part like this:

$arrDatos=array();
if ($result = mysqli_query($conexion, $sql)) {
    /* obtener array asociativo */
    while ($row = mysqli_fetch_assoc($result)) {
        $arrDatos[]=$row;
    }
}else{

    $arrDatos["error"]=mysqli_error($conexion);
}

return $arrDatos;
    
answered by 19.03.2018 / 02:51
source