php lastInsertId(). Undefined variable in a form value for array

222 views Asked by At

I am having a problem passing $last:

$last = $db->lastInsertId();

I need to pass lastInsertId() in array from form field

<input type="text" name="remi[]" value= '<?php echo $last; ?>' /> 

lastInsertId() works because near the form I get the correct last id:

// here $last variable is ok
<?php if(isset($last)){ echo $last; }; ?>

But from the form:

remi: <input type="text" name="remi[]" value= '<?php echo $last; ?>' /><br />

I got:

DetalleRemisiones Object
(
    [conn:DetalleRemisiones:private] => PDO Object
        (
        )

    [table_name:DetalleRemisiones:private] => detalleremi
    [id] => 
    [remi] => Array
        (
            [0] => 
Notice:  Undefined variable: last in C:\xampp\htdocs\noe\remisiones_create_mas_remision3.php on line 85

            [1] => 
Notice:  Undefined variable: last in C:\xampp\htdocs\noe\remisiones_create_mas_remision3.php on line 93

            [2] => 
Notice:  Undefined variable: last in C:\xampp\htdocs\noe\remisiones_create_mas_remision3.php on line 98

        )

So, how can I pass $last in the input form to get the last insert id?

for instead:

 [remi] => Array
    (
        [0] => 256
        [1] => 256
        [2] => 256
    )

Here is my code:

include_once 'config/database.php';

    $database = new Database();
    $db = $database->getConnection();

    include_once 'objects/remisiones.php';
    $remision = new remisiones($db);

    include_once 'objects/detalleremisiones.php';
    $detalleremision = new detalleremisiones($db);


if($_POST){

    $remision->clienteId = $_POST['clienteId'];
    $remision->fecha = $_POST['fecha'];

    $remision->create();
    $last = $db->lastInsertId();

    $detalleremision->remi = $_POST['remi']; 
    $detalleremision->trat = $_POST['trat'];
    $detalleremision->precio = $_POST['precio'];
    $detalleremision->cantidad = $_POST['cantidad'];         

    $detalleremision->create2();

}
?>

<form action="remisiones_create_mas_remision3.php" method="post">
        <tr>
            <td>Fecha</td>
            <td><input type='text' name='fecha' class='form-control' required></td>
        </tr>
        <tr>
            <td>Cliente</td>
            <td>
            <?php
            // read the clientes from the database
            include_once 'objects/remisiones.php';

            $remision = new Remisiones($db);
            $stmt = $remision->readclientes();

            echo "<select class='form-control' name='clienteId' required>";
            echo "<option>Seleccione cliente...</option>";

            while ($row_cliente = $stmt->fetch(PDO::FETCH_ASSOC)){
                extract($row_cliente);
                echo "<option value='{$id}'>{$clienteId} {$apellido} {$apellido2} {$nombre}</option>";
            }     
        echo "</select>";
        ?>
        </td>
    </tr>

<br><br><br>

//
    <?php // here $last variable is ok
     if(isset($last)){ echo $last; }; ?>/


    remi: <input type="text" name="remi[]" value= '<?php echo $last; ?>' /><br />
    trat: <input type="text" name="trat[]" /><br />
    precio: <input type="text" name="precio[]" /><br />
    cantidad: <input type="text" name="cantidad[]" /><br />     

    remi: <input type="text" name="remi[]" value= '<?php echo $last; ?>' /><br /> 
    trat: <input type="text" name="trat[]" /><br />
    precio: <input type="text" name="precio[]" /><br />
    cantidad: <input type="text" name="cantidad[]" /><br />

    remi: <input type="text" name="remi[]" value= '<?php echo $last; ?>' /><br /> 
    trat: <input type="text" name="trat[]" /><br />
    precio: <input type="text" name="precio[]" /><br />
    cantidad: <input type="text" name="cantidad[]" /><br />
      <input type="submit" value="Enviar">
</form>
2

There are 2 answers

1
nospor On

You must use isset also in html

remi: <input type="text" name="remi[]" value= '<?php if (isset($last)) echo $last; ?>' 
2
SG_Rowin On

Your 'Last' variable does not exit or is not executed when you are calling it.

Although you can check if the variable isset when calling it, but a best practice would be assigning the variables at top of your script with empty values. This is better because you dont have to check all variables before calling them:

Place at top of your file:

$last = "";