2016-05-24 58 views
1

我有路過$last一個問題:php lastInsertId()。在表單中值未定義變量數組

$last = $db->lastInsertId(); 

我需要在數組傳遞lastInsertId()從表單字段,因爲

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

lastInsertId()工程附近的形式,我得到正確的最後一個ID:

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

但從形式:

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

我:

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 

     ) 

那麼,怎樣才能在我輸入的形式得到最後插入ID傳遞$last

用於代替:

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

這裏是我的代碼:

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> 

回答

0

您還必須使用isset在HTML

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

沒有,我嘗試過,但事實並非如此。 – rafpre

+0

[表名:DetalleRemisiones:私人] => detalleremi [ID] => [雷米] =>數組 ( [0] => [1] => [2] => ) – rafpre

0

你的 '最後' 變量不存在或者是當你調用它時不會執行。

雖然你可以調用它的時候,但最好的做法,在與空值腳本的頂部分配變量如果變量isset檢查。這是更好,因爲你沒有打電話給他們之前檢查所有的變量:在你的文件的頂部

地點:

$last = ""; 
+0

無改變。我得到[表名:DetalleRemisiones:私人] => detalleremi [ID] => [雷米] =>數組 ( [0] => [1] => [2] => ) – rafpre

+0

但在字段最後插入ID是可以的,remi 353 – rafpre