Edit edit with PDO

1
**Este es el error que me tira no se si tengo bien hecho el codigo**   

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE [HY093]: Invalid parameter number: parameter was not defined' in D: \ xampp \ htdocs \ frosted projects \ controllers \ edit_sales.php: 19 Stack trace : # 0 D: \ xampp \ htdocs \ escaped projects \ drivers \ edit_php (19): PDOStatement-> execute () # 1 {main} thrown in

this is my PHP code with PDO

<?php

if ($_POST) {
    $id = $_POST['id'];
    $producto = $_POST['producto'];
    $precio = $_POST['precio'];
    $cantidad = $_POST['cantidad'];
    $fecha = $_POST['fecha'];

    $conexion = new PDO("mysql:host=127.0.0.1;dbname=eladeria", "root", "");
    $conexion->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $db=$conexion;
    $stmt = $db->prepare("update ventas set producto=:producto,cantidad=:cantidad,precio=:precio ,fecha_venta=:fecha where id=:id");
    $stmt->bindParam(":producto",$producto);
    $stmt->bindParam(":cantidad",$cantidad);
    $stmt->bindParam(":precio",$precio);
    $stmt->bindParam(":fecha_venta",$fecha);
    $stmt->bindParam(":id",$id);
    $stmt->execute();

}
?>

This is my form

<form id="formulario" method="post">
                                                    <div class="md-form mb-5">
                                                        <i class="fa fa-user prefix grey-text">
                                                            <label data-error="wrong" data-success="right" for="orangeForm-name">Id</label>
                                                        </i>
                                                        <input type="text" name="id" readonly value="<?php echo $id; ?>"  name="producto" class="form-control validate">

                                                    </div>
                                                    <div class="md-form mb-5">
                                                        <i class="fa fa-user prefix grey-text">
                                                            <label data-error="wrong" data-success="right" for="orangeForm-name">Producto</label>
                                                        </i>
                                                        <input type="text" id="orangeForm-name" name="producto" class="form-control validate">

                                                    </div>

                                                    <div class="md-form mb-5">
                                                        <i class="fa fa-user prefix grey-text">
                                                            <label data-error="wrong" data-success="right" for="orangeForm-name">Cantidad</label>
                                                        </i>
                                                        <input type="text" id="orangeForm-name" name="cantidad" class="form-control validate">

                                                    </div>


                                                    <div class="md-form mb-5">
                                                        <i class="fa fa-user prefix grey-text">
                                                            <label data-error="wrong" data-success="right" for="orangeForm-name">Precio</label>
                                                        </i>
                                                        <input type="text" id="orangeForm-name" name="precio" class="form-control validate">

                                                    </div>


                                                    <div class="md-form mb-5">
                                                        <i class="fa fa-user prefix grey-text">
                                                            <label data-error="wrong" data-success="right" for="orangeForm-name">Fecha de la
                                                                venta</label>
                                                        </i>
                                                        <input type="date" id="orangeForm-name" name="fecha" class="form-control validate">

                                                    </div>

                                                </form>
    
asked by user9472850 11.08.2018 в 03:36
source

2 answers

2

Try correcting the following details

  • To validate the type of request if it is GET or POST; you must do it by reading the global variable $SERVER['REQUEST_METHOD']
  • In the UPDDATE statement you have fecha_venta=:venta but in the bindParam you have $stmt->bindParam(":fecha_venta",$fecha); ie the placeholders do not match
  • Therefore your code should look like this

    if ($_SERVER['REQUEST_METHOD'] == 'POST'){
        $id = $_POST['id'];
        $producto = $_POST['producto'];
        $precio = $_POST['precio'];
        $cantidad = $_POST['cantidad'];
        $fecha = $_POST['fecha'];
    
        $conexion = new PDO("mysql:host=127.0.0.1;dbname=eladeria", "root", "");
        $conexion->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $db=$conexion;
        $stmt = $db->prepare("update ventas set producto=:producto,cantidad=:cantidad,precio=:precio ,fecha_venta=:fecha where id=:id");
        $stmt->bindParam(":producto",$producto);
        $stmt->bindParam(":cantidad",$cantidad);
        $stmt->bindParam(":precio",$precio);
        $stmt->bindParam(":fecha",$fecha);
        $stmt->bindParam(":id",$id);
        $stmt->execute();
    }
    
        
    answered by 11.08.2018 / 03:48
    source
    1

    Hello, you have to make this correction in your code:

    $stmt->bindParam(":fecha_venta",$fecha);
    

    for this

    $stmt->bindParam(":fecha",$fecha);
    

    this could serve you as an idea:

    <?php
    $conexion = new PDO("mysql:host=127.0.0.1;dbname=eladeria", "root", "");
    $conexion->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $db=$conexion;
    $id = "";
    $producto ="";
    $precio = "";
    $cantidad ="";
    $fecha="";
    
    if ($_POST) {
        $id = $_POST['id'];
        $producto = $_POST['producto'];
        $precio = $_POST['precio'];
        $cantidad = $_POST['cantidad'];
        $fecha = $_POST['fecha'];
    
    
        $stmt = $db->prepare("update ventas set producto=:producto,cantidad=:cantidad,precio=:precio ,fecha_venta=:fecha where id=:id");
        $stmt->bindParam(":producto",$producto);
        $stmt->bindParam(":cantidad",$cantidad);
        $stmt->bindParam(":precio",$precio);
        $stmt->bindParam(":fecha",$fecha);
        $stmt->bindParam(":id",$id);
        $stmt->execute();
    
    }
    ?>
    <form id="formulario" method="post">
        <div class="md-form mb-5">
            <i class="fa fa-user prefix grey-text">
                <label data-error="wrong" data-success="right" for="orangeForm-name">Id</label>
            </i>
            <input type="text" name="id"   value="<?php echo $id; ?>" name="producto" class="form-control validate">
    
        </div>
        <div class="md-form mb-5">
            <i class="fa fa-user prefix grey-text">
                <label data-error="wrong" data-success="right" for="orangeForm-name">Producto</label>
            </i>
            <input type="text" id="orangeForm-name" value="<?php echo $producto; ?>" name="producto" class="form-control validate">
    
        </div>
    
        <div class="md-form mb-5">
            <i class="fa fa-user prefix grey-text">
                <label data-error="wrong" data-success="right" for="orangeForm-name">Cantidad</label>
            </i>
            <input type="text" id="orangeForm-name" value="<?php echo $cantidad; ?>" name="cantidad" class="form-control validate">
    
        </div>
    
    
        <div class="md-form mb-5">
            <i class="fa fa-user prefix grey-text">
                <label data-error="wrong" data-success="right" for="orangeForm-name">Precio</label>
            </i>
            <input type="text" id="orangeForm-name" value="<?php echo $precio; ?>" name="precio" class="form-control validate">
    
        </div>
    
    
        <div class="md-form mb-5">
            <i class="fa fa-user prefix grey-text">
                <label data-error="wrong" data-success="right" for="orangeForm-name">Fecha de la
                    venta</label>
            </i>
            <input type="date" id="orangeForm-name" value="<?php echo $fecha; ?>" name="fecha" class="form-control validate">
    
        </div>
    <div class="md-form mb-5">
        <input type="submit" name="button" value="Enviar">
    </div>  
    </form>
    

    Greetings ....

        
    answered by 11.08.2018 в 04:33