此代码用于编辑标题为“ tickets”的表中的记录/条目,其中id为主键,而“ serialno”为故障单的序列号。其余的成员(如member_id,id-off和id_emp)是来自其他名为“ members”,“ employees”和“ offices”的表的索引。这是执行此脚本时收到的消息:
警告:为中的foreach()提供了无效的参数 C:\ xampp \ htdocs \ luckypool \ updateserial.php,第67行
<?php
require "config.php";
require "common.php";
if (isset($_POST['submit'])) {
try {
$connection = new PDO($dsn, $username, $password, $options);
$ticket =[
"id" => $_POST['id'],
"serialno" => $_POST['serialno'],
"draw" => $_POST['draw'],
"member_id" => $_POST['member_id'],
"id_off" => $_POST['id_off'],
"id_emp" => $_POST['id_emp'],
"date" => $_POST['date']
];
$sql = "UPDATE tickets
SET id = :id,
serialno = :serialno,
draw = :draw,
member_id = :member_id,
id_off = :id_off,
id_emp = :id_emp,
date = :date
WHERE id = :id";
$statement = $connection->prepare($sql);
$statement->execute($ticket);
} catch(PDOException $error) {
echo $sql . "<br>" . $error->getMessage();
}
}
if (isset($_GET['id'])) {
try {
$connection = new PDO($dsn, $username, $password, $options);
$id = $_GET['id'];
$sql = "SELECT * FROM tickets WHERE id = :id";
$statement = $connection->prepare($sql);
$statement->bindValue(':id', $id);
$statement->execute();
$ticket = $statement->fetch(PDO::FETCH_ASSOC);
} catch(PDOException $error) {
echo $sql . "<br>" . $error->getMessage();
}
} else {
echo "Something went wrong!";
exit;
}
?>
<?php require "templates/header.php"; ?>
<?php if (isset($_POST['submit']) && $statement) : ?>
<blockquote><?php echo escape($_POST['serialno']); ?> successfully updated.</blockquote>
<?php endif; ?>
<h2>Edit Ticket Record</h2>
<form method="post">
<?php foreach ($ticket as $key => $value) : ?>
<label for="<?php echo $key; ?>"><?php echo ucfirst($key); ?></label>
<input type="text" name="<?php echo $key; ?>" id="<?php echo $key; ?>" value="<?php echo escape($value); ?>" <?php echo ($key === 'id' ? 'readonly' : null); ?>>
<?php endforeach; ?>
<input type="submit" name="submit" value="Submit">
</form>
<a href="index.php">Back to home</a>
<?php require "templates/footer.php"; ?>
答案 0 :(得分:0)
use isset($tickets) before foreach loop
<form method="post">
<?php
if (isset($ticket) && is_array($tickets)) {
foreach ($ticket as $key => $value) : ?>
<label for="<?php echo $key; ?>"><?php echo ucfirst($key); ?></label>
<input type="text" name="<?php echo $key; ?>" id="<?php echo $key; ?>" value="<?php echo escape($value); ?>" <?php echo ($key === 'id' ? 'readonly' : null); ?>>
<?php
endforeach;
}
?>
<input type="submit" name="submit" value="Submit">
</form>