我在传递$last
时出现问题:
$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
)
那么,如何在输入表单中传递$last
以获取最后一个插入ID?
代替:
[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 :(得分:0)
您必须在html中使用isset
remi: <input type="text" name="remi[]" value= '<?php if (isset($last)) echo $last; ?>'
答案 1 :(得分:0)
您的'Last'变量在您调用时不会退出或不执行。
虽然您可以在调用时检查变量是否设置,但最佳做法是在脚本顶部使用空值分配变量。这样做更好,因为在调用之前你不必检查所有变量:
放在档案顶部:
$last = "";