gpt4 book ai didi

php - PHP- Uncaught Error :由于调用id而在 bool 值上调用成员函数fetch_assoc()

转载 作者:行者123 更新时间:2023-11-28 23:16:59 25 4
gpt4 key购买 nike

我正在尝试测试一个查看所有数据表单,并在表单下面添加两个按钮,即“编辑表单”并返回。下面是视图窗体。
view test
我想让edit按钮获取ID以进入编辑表单,但它给了我这个错误。
edit error
这些是密码。
视图窗体(viewtest.php):

<?php
require("config.php");
$id = filter_input(INPUT_GET, 'id');
$sql = "SELECT * FROM contracts WHERE `id` = $id";
$result = $con->query($sql);
$row = $result->fetch_assoc();
?>

<!DOCTYPE html>

<html>
<head>
<title> View a Contract </title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1">

</head>

<body>

<h2>Viewing a Contract Form</h2>

<form action="" method="POST">
<table>

<tr>
<th colspan='2'>Contract Details</th>
</tr>

<tr>
<th>ID</th>
<td><?php echo $row['id']; ?></td>
</tr>

<tr>
<th>Contract Title</th>
<td><?php echo $row['contract_title']; ?></td>
</tr>


</table>

</form>

<a href='viewedit.php?id=".$row["id"]."'>Edit</a>
<a href="view.php" class="btn btn-default" role="button"> Go Back</a>


</body>
</html>

视图编辑(View Edit.php):
<?php
require("config.php");
$id = filter_input(INPUT_GET, 'id');
error_reporting( ~E_NOTICE );

if(isset($_POST['edit']) ){

$contract_title = $_POST['contract_title'];

$upd= "UPDATE `contracts` SET `contract_title` = ? WHERE `id` = ?";
$stmt = $con->prepare($upd);
$null = NULL;
$stmt->bind_param("si",$contract_title,$id);
$stmt->execute();

if ($stmt->errno){
echo "FAILURE!!! " . $stmt->error;
} else {
$successMsg = "Contract Successfully Updated!";
}
$stmt->close();

}

?>


<html>
<head><title>Edit a Contract</title>

<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1">


</head>
<body>


<h1>Edit Contract Form</h1>

<form role="form" action="" method="POST" enctype="multipart/form-data">

<h3>Contract Details</h3><hr>

<label>ID: </label> <?php echo $id; ?> <br>
<input type="hidden" name="id" value="<?php echo $id; ?>" /><br>

<?php
$sql = "SELECT * FROM contracts WHERE `id` = $id";
$result = $con->query($sql);
$row = $result->fetch_assoc();
?>

<label>Contract Title</label>
<input class="form-control" type="text" name="contract_title" size="45" value="<?php echo htmlspecialchars($row['contract_title']); ?>" />
<br>


<button type="submit" class="btn btn-success btn-md" name="edit">
<span class="glyphicon glyphicon-pencil"></span> Update Contract
</button>
<a href="view.php" class="btn btn-default" role="button">Cancel</a>


</form>

</body>
</html>
<?php
$con->close();
?>

不知何故,如果我将视图表单(viewtest.php)中的行从 <a href='viewedit.php?id=".$row["id"]."'>Edit</a>改为 <a href='viewedit.php?id="id"'>Edit</a>的话,它就不会在编辑表单(View edit.php)中返回数据库中的数据。
如何解决这个问题?

最佳答案

活到死几乎给了你一个小错误的答案,代码应该是

<a href='viewedit.php?id=<?php echo $row["id"];?>'>Edit</a>

你也可以这样写,但是要确保你的short_标签在php.ini中是打开的
<a href='viewedit.php?id=<?=$row["id"]?>'>Edit</a>

关于php - PHP- Uncaught Error :由于调用id而在 bool 值上调用成员函数fetch_assoc(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43445180/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com