从更新操作的记录接收数据时出现问题(尝试访问 bool 类型值的数组偏移量)
Problems receiving data from a record for update operation (Trying to access array offset on value of type bool)
我无法让我的 form_update_manga.php
页面从 form_read_manga.php
页面发送的记录中接收值。
None 字段 return 记录中的任何值,因此无法更新表单。顺便说一句,没有办法在这种形式下做任何事情。我找不到合乎逻辑的解决方案。
form_update_manga.php页面出现的错误如下图所示:
Image here
大多数表单字段出错:尝试访问 bool 类型值的数组偏移量
Manga.php
<?php
require_once '../model/BD.php';
class Manga extends BD {
private $id_manga, $title, $publisher, $volumes, $discount, $value;
public function getId() { return $this->id_manga; }
public function getTitle() { return $this->title; }
public function getPublisher() { return $this->publisher; }
public function getVolumes() { return $this->volumes; }
public function getDiscount() { return $this->discount; }
public function getValue() { return $this->value; }
public function setId($id_manga) { $this->id_manga = $id_manga; }
public function setTitle($title) { $this->title = $title; }
public function setPublisher($publisher) { $this->publisher = $publisher; }
public function setVolumes($volumes) { $this->volumes = $volumes; }
public function setDiscount($discount) { $this->discount = $discount; }
public function setValue($value) { $this->value = $value; }
}
?>
MangaDAO.php
protected $table = 'manga';
public function readManga() {
try {
$sql = "SELECT * FROM $this->table WHERE id_manga = :id_manga";
$stm = BD::getInstance()->prepare($sql);
$stm->bindValue(':id_manga', $this->getId());
$stm->execute();
return $stm->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
echo "Error PDO" . $e->getMessage();
die();
} catch (Exception $e) {
echo "Error" . $e->getMessage();
die();
}
}
public function updateManga() {
try {
$sql = "UPDATE $this->table SET title = :title, publisher = :publisher,
volumes = :volumes, discount = :discount, value = :value WHERE id_manga = :id_manga";
$stm = BD::getInstance()->prepare($sql);
$stm->bindValue(':id_manga', $this->getId());
$stm->bindValue(':title', $this->getTitle());
$stm->bindValue(':publisher', $this->getPublisher());
$stm->bindValue(':volumes', $this->getVolumes());
$stm->bindValue(':discount', $this->getDiscount());
$stm->bindValue(':value', $this->getValue());
return $stm->execute();
} catch (PDOException $e) {
echo "Error PDO" . $e->getMessage();
die();
} catch (Exception $e) {
echo "Error ". $e->getMessage();
die();
}
}
form_read_manga.php
<div class="container" style="margin-top: 40px;">
<h4 class="text-center">Manga list</h4>
<br>
<table class="table table-hover">
<thead>
<tr>
<th>ID</th>
<th>Title</th>
<th>Publisher</th>
<th>Volumes</th>
<th>Discount</th>
<th>Value collection</th>
<th>Register date</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php
require_once "../model/MangaDAO.php";
$manga = new MangaDAO();
foreach ($manga->readAllMangas() as $value){
echo '<tr>';
echo '<td>'.$value['id_manga'].'</td>';
echo '<td>'.$value['title'].'</td>';
echo '<td>'.$value['publisher'].'</td>';
echo '<td>'.$value['volumes'].'</td>';
echo '<td>'.$value['discount'].'</td>';
echo '<td>R$'.$value['value'].'</td>';
echo '<td>'.date('d/m/Y H:i:s', strtotime($value['register_date'])).'</td>';
echo '<td>';
echo '<a class="btn btn-info" href="/mangas/view/form_update_manga.php?id_manga='.$value['id_manga'].'"role="button" ><i class="fa fa-edit"></i> Update</a>';
echo " ";
echo '<a class="btn btn-danger" href="/mangas/controller/delete_manga.php?id_manga='.$value['id_manga'].'"role="button"><i class="fa fa-trash"></i> Delete</a>';
echo '</td>';
echo '</tr>';
}
?>
</tbody>
</table>
</div>
form_update_manga.php
<?php
require_once "../model/MangaDAO.php";
$mangaDAO = new MangaDAO();
if (!isset($_GET['id_manga'])) {
echo '<p><a href="../view/form_read_manga.php"><button>Redo operation</button></a></p>';
die();
} else {
$data = $mangaDAO->readManga($_GET['id_manga']);
}
?>
<div class="container" id="t_container" style="margin-top: 20px;">
<h4 class="text-center">Update collection</h4>
<small class="form-text text-muted text-center">All fields are mandatory</small>
<br>
<form method="POST" action="/mangas/controller/update_manga.php">
<div class="form-group">
<label>ID:</label>
<input type="number" class="form-control" name="id_manga" required value="<?php echo $data['id_manga'] ?>" disabled>
</div>
<div class="form-group">
<label>Title:</label>
<input type="text" class="form-control" name="title" required maxlength="50" value="<?php echo $data['title'] ?>">
</div>
<div class="form-group">
<label>Publisher:</label>
<select class="custom-select" name="publisher" required value="<?php echo $data['publisher'] ?>">
<option selected value="">Choose the publisher</option>
<option value="Astral Comics" title="Astral Comics">Astral Comics</option>
<option value="Clamp" title="Clamp">Clamp</option>
<option value="Conrad" title="Conrad">Conrad</option>
<option value="Darkside Books" title="Darkside Books">Darkside Books</option>
<option value="Devir" title="Devir">Devir</option>
<option value="JBC" title="JBC">JBC</option>
<option value="NewPop" title="NewPop">NewPop</option>
<option value="Nova Sampa" title="Nova Sampa">Nova Sampa</option>
<option value="Panini" title="Panini">Panini</option>
<option value="Pipoca & Nanquim" title="Pipoca & Nanquim">Pipoca & Nanquim</option>
<option value="Veneta" title="Veneta">Veneta</option>
<option value="Other" title="Other">Other</option>
</select>
</div>
<div class="form-group">
<label>Volumes amount:</label>
<input type="text" class="form-control" name="volumes" required onkeypress="$(this).mask('000', {reverse: true});" value="<?php echo $data['volumes'] ?>">
</div>
<div class="form-group">
<label>Discount:</label>
<input type="text" class="form-control" name="discount" required onkeypress="$(this).mask('000.00', {reverse: true});" value="<?php echo $data['discount'] ?>">
</div>
<div class="form-group">
<label>Value collection:</label>
<input type="text" class="form-control" name="value" required value="<?php echo $data['value'] ?>" onkeypress="$(this).mask('000.00', {reverse: true});">
</div>
<button type="submit" name="btn_update" class="btn btn-info btn-lg btn-block"><i class="fa fa-edit"></i> Update</button>
</form>
</div>
用 MangaDAO.php
中的代码替换您的代码
protected $table = 'manga';
public function readManga($mangaId) {
try {
$sql = "SELECT * FROM $this->table WHERE id_manga = :id_manga";
$stm = BD::getInstance()->prepare($sql);
$stm->bindValue(':id_manga', $mangaId);
$stm->execute();
return $stm->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
echo "Error PDO" . $e->getMessage();
die();
} catch (Exception $e) {
echo "Error" . $e->getMessage();
die();
}
}
您必须将选定的属性设置为特定值,请参考以下代码。
<div class="form-group">
<label>Publisher:</label>
<select class="custom-select" name="publisher" required value="<?php echo $data['publisher'] ?>">
<option value="" <?php if($data['publisher']==''){echo 'selected';} ?>>Choose the publisher</option>
<option value="Astral Comics" title="Astral Comics" <?php if($data['publisher']=='Astral Comics'){echo 'selected';} ?>>Astral Comics</option>
<option value="Clamp" title="Clamp" <?php if($data['publisher']=='Clamp'){echo 'selected';} ?>>Clamp</option>
<option value="Conrad" title="Conrad" <?php if($data['publisher']=='Conrad'){echo 'selected';} ?>>Conrad</option>
<option value="Darkside Books" title="Darkside Books" <?php if($data['publisher']=='Darkside Books'){echo 'selected';} ?>>Darkside Books</option>
<option value="Devir" title="Devir" <?php if($data['publisher']=='Devir'){echo 'selected';} ?>>Devir</option>
<option value="JBC" title="JBC" <?php if($data['publisher']=='JBC'){echo 'selected';} ?>>JBC</option>
<option value="NewPop" title="NewPop" <?php if($data['publisher']=='NewPop'){echo 'selected';} ?>>NewPop</option>
<option value="Nova Sampa" title="Nova Sampa" <?php if($data['publisher']=='Nova Sampa'){echo 'selected';} ?>>Nova Sampa</option>
<option value="Panini" title="Panini" <?php if($data['publisher']=='Panini'){echo 'selected';} ?>>Panini</option>
<option value="Pipoca & Nanquim" title="Pipoca & Nanquim" <?php if($data['publisher']=='Pipoca & Nanquim'){echo 'selected';} ?>>Pipoca & Nanquim</option>
<option value="Veneta" title="Veneta" <?php if($data['publisher']=='Veneta'){echo 'selected';} ?>>Veneta</option>
<option value="Other" title="Other" <?php if($data['publisher']=='Other'){echo 'selected';} ?>>Other</option>
</select>
</div>
我无法让我的 form_update_manga.php
页面从 form_read_manga.php
页面发送的记录中接收值。
None 字段 return 记录中的任何值,因此无法更新表单。顺便说一句,没有办法在这种形式下做任何事情。我找不到合乎逻辑的解决方案。
form_update_manga.php页面出现的错误如下图所示:
Image here
大多数表单字段出错:尝试访问 bool 类型值的数组偏移量
Manga.php
<?php
require_once '../model/BD.php';
class Manga extends BD {
private $id_manga, $title, $publisher, $volumes, $discount, $value;
public function getId() { return $this->id_manga; }
public function getTitle() { return $this->title; }
public function getPublisher() { return $this->publisher; }
public function getVolumes() { return $this->volumes; }
public function getDiscount() { return $this->discount; }
public function getValue() { return $this->value; }
public function setId($id_manga) { $this->id_manga = $id_manga; }
public function setTitle($title) { $this->title = $title; }
public function setPublisher($publisher) { $this->publisher = $publisher; }
public function setVolumes($volumes) { $this->volumes = $volumes; }
public function setDiscount($discount) { $this->discount = $discount; }
public function setValue($value) { $this->value = $value; }
}
?>
MangaDAO.php
protected $table = 'manga';
public function readManga() {
try {
$sql = "SELECT * FROM $this->table WHERE id_manga = :id_manga";
$stm = BD::getInstance()->prepare($sql);
$stm->bindValue(':id_manga', $this->getId());
$stm->execute();
return $stm->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
echo "Error PDO" . $e->getMessage();
die();
} catch (Exception $e) {
echo "Error" . $e->getMessage();
die();
}
}
public function updateManga() {
try {
$sql = "UPDATE $this->table SET title = :title, publisher = :publisher,
volumes = :volumes, discount = :discount, value = :value WHERE id_manga = :id_manga";
$stm = BD::getInstance()->prepare($sql);
$stm->bindValue(':id_manga', $this->getId());
$stm->bindValue(':title', $this->getTitle());
$stm->bindValue(':publisher', $this->getPublisher());
$stm->bindValue(':volumes', $this->getVolumes());
$stm->bindValue(':discount', $this->getDiscount());
$stm->bindValue(':value', $this->getValue());
return $stm->execute();
} catch (PDOException $e) {
echo "Error PDO" . $e->getMessage();
die();
} catch (Exception $e) {
echo "Error ". $e->getMessage();
die();
}
}
form_read_manga.php
<div class="container" style="margin-top: 40px;">
<h4 class="text-center">Manga list</h4>
<br>
<table class="table table-hover">
<thead>
<tr>
<th>ID</th>
<th>Title</th>
<th>Publisher</th>
<th>Volumes</th>
<th>Discount</th>
<th>Value collection</th>
<th>Register date</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php
require_once "../model/MangaDAO.php";
$manga = new MangaDAO();
foreach ($manga->readAllMangas() as $value){
echo '<tr>';
echo '<td>'.$value['id_manga'].'</td>';
echo '<td>'.$value['title'].'</td>';
echo '<td>'.$value['publisher'].'</td>';
echo '<td>'.$value['volumes'].'</td>';
echo '<td>'.$value['discount'].'</td>';
echo '<td>R$'.$value['value'].'</td>';
echo '<td>'.date('d/m/Y H:i:s', strtotime($value['register_date'])).'</td>';
echo '<td>';
echo '<a class="btn btn-info" href="/mangas/view/form_update_manga.php?id_manga='.$value['id_manga'].'"role="button" ><i class="fa fa-edit"></i> Update</a>';
echo " ";
echo '<a class="btn btn-danger" href="/mangas/controller/delete_manga.php?id_manga='.$value['id_manga'].'"role="button"><i class="fa fa-trash"></i> Delete</a>';
echo '</td>';
echo '</tr>';
}
?>
</tbody>
</table>
</div>
form_update_manga.php
<?php
require_once "../model/MangaDAO.php";
$mangaDAO = new MangaDAO();
if (!isset($_GET['id_manga'])) {
echo '<p><a href="../view/form_read_manga.php"><button>Redo operation</button></a></p>';
die();
} else {
$data = $mangaDAO->readManga($_GET['id_manga']);
}
?>
<div class="container" id="t_container" style="margin-top: 20px;">
<h4 class="text-center">Update collection</h4>
<small class="form-text text-muted text-center">All fields are mandatory</small>
<br>
<form method="POST" action="/mangas/controller/update_manga.php">
<div class="form-group">
<label>ID:</label>
<input type="number" class="form-control" name="id_manga" required value="<?php echo $data['id_manga'] ?>" disabled>
</div>
<div class="form-group">
<label>Title:</label>
<input type="text" class="form-control" name="title" required maxlength="50" value="<?php echo $data['title'] ?>">
</div>
<div class="form-group">
<label>Publisher:</label>
<select class="custom-select" name="publisher" required value="<?php echo $data['publisher'] ?>">
<option selected value="">Choose the publisher</option>
<option value="Astral Comics" title="Astral Comics">Astral Comics</option>
<option value="Clamp" title="Clamp">Clamp</option>
<option value="Conrad" title="Conrad">Conrad</option>
<option value="Darkside Books" title="Darkside Books">Darkside Books</option>
<option value="Devir" title="Devir">Devir</option>
<option value="JBC" title="JBC">JBC</option>
<option value="NewPop" title="NewPop">NewPop</option>
<option value="Nova Sampa" title="Nova Sampa">Nova Sampa</option>
<option value="Panini" title="Panini">Panini</option>
<option value="Pipoca & Nanquim" title="Pipoca & Nanquim">Pipoca & Nanquim</option>
<option value="Veneta" title="Veneta">Veneta</option>
<option value="Other" title="Other">Other</option>
</select>
</div>
<div class="form-group">
<label>Volumes amount:</label>
<input type="text" class="form-control" name="volumes" required onkeypress="$(this).mask('000', {reverse: true});" value="<?php echo $data['volumes'] ?>">
</div>
<div class="form-group">
<label>Discount:</label>
<input type="text" class="form-control" name="discount" required onkeypress="$(this).mask('000.00', {reverse: true});" value="<?php echo $data['discount'] ?>">
</div>
<div class="form-group">
<label>Value collection:</label>
<input type="text" class="form-control" name="value" required value="<?php echo $data['value'] ?>" onkeypress="$(this).mask('000.00', {reverse: true});">
</div>
<button type="submit" name="btn_update" class="btn btn-info btn-lg btn-block"><i class="fa fa-edit"></i> Update</button>
</form>
</div>
用 MangaDAO.php
中的代码替换您的代码protected $table = 'manga';
public function readManga($mangaId) {
try {
$sql = "SELECT * FROM $this->table WHERE id_manga = :id_manga";
$stm = BD::getInstance()->prepare($sql);
$stm->bindValue(':id_manga', $mangaId);
$stm->execute();
return $stm->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
echo "Error PDO" . $e->getMessage();
die();
} catch (Exception $e) {
echo "Error" . $e->getMessage();
die();
}
}
您必须将选定的属性设置为特定值,请参考以下代码。
<div class="form-group">
<label>Publisher:</label>
<select class="custom-select" name="publisher" required value="<?php echo $data['publisher'] ?>">
<option value="" <?php if($data['publisher']==''){echo 'selected';} ?>>Choose the publisher</option>
<option value="Astral Comics" title="Astral Comics" <?php if($data['publisher']=='Astral Comics'){echo 'selected';} ?>>Astral Comics</option>
<option value="Clamp" title="Clamp" <?php if($data['publisher']=='Clamp'){echo 'selected';} ?>>Clamp</option>
<option value="Conrad" title="Conrad" <?php if($data['publisher']=='Conrad'){echo 'selected';} ?>>Conrad</option>
<option value="Darkside Books" title="Darkside Books" <?php if($data['publisher']=='Darkside Books'){echo 'selected';} ?>>Darkside Books</option>
<option value="Devir" title="Devir" <?php if($data['publisher']=='Devir'){echo 'selected';} ?>>Devir</option>
<option value="JBC" title="JBC" <?php if($data['publisher']=='JBC'){echo 'selected';} ?>>JBC</option>
<option value="NewPop" title="NewPop" <?php if($data['publisher']=='NewPop'){echo 'selected';} ?>>NewPop</option>
<option value="Nova Sampa" title="Nova Sampa" <?php if($data['publisher']=='Nova Sampa'){echo 'selected';} ?>>Nova Sampa</option>
<option value="Panini" title="Panini" <?php if($data['publisher']=='Panini'){echo 'selected';} ?>>Panini</option>
<option value="Pipoca & Nanquim" title="Pipoca & Nanquim" <?php if($data['publisher']=='Pipoca & Nanquim'){echo 'selected';} ?>>Pipoca & Nanquim</option>
<option value="Veneta" title="Veneta" <?php if($data['publisher']=='Veneta'){echo 'selected';} ?>>Veneta</option>
<option value="Other" title="Other" <?php if($data['publisher']=='Other'){echo 'selected';} ?>>Other</option>
</select>
</div>