PHP:从if语句中检索值,以便在同一页面上的html输入标记中使用
我试图从if语句中检索值,也就是从数据库中获取值,这是我的代码PHP:从if语句中检索值,以便在同一页面上的html输入标记中使用,php,html,if-statement,scope,Php,Html,If Statement,Scope,我试图从if语句中检索值,也就是从数据库中获取值,这是我的代码 <!DOCTYPE html> <?PHP include('session.php'); include('recipedbconfig.php'); if(isset($_POST['submit'])) { $postrcpnmesrch = $_POST['rcpnmesrch']; if($_SERVER["REQUEST_METHOD"] =
<!DOCTYPE html>
<?PHP
include('session.php');
include('recipedbconfig.php');
if(isset($_POST['submit'])) {
$postrcpnmesrch = $_POST['rcpnmesrch'];
if($_SERVER["REQUEST_METHOD"] == "POST") {
$sql = "SELECT * FROM recipe_table WHERE recipe_name = '".$postrcpnmesrch."'";
$result = mysqli_query($db, $sql);
$row = mysqli_fetch_assoc($result);
}
}
?>
<html>
<head>
<title>Fresh Veg Admin, Edit Recipes</title>
<link rel=stylesheet type="text/css" href="/FVRC/FVRC.css">
</head>
<body>
<h1>Fresh Veg Admin, Edit Recipes</h1>
<p>User:<b> <?PHP echo $login_session; ?></b>, logged in.</p>
<form action="/FVRC/addeditrecipes.php">
<button type="submit">Back</button>
</form>
<br/>
<hr/>
<table>
<form method="POST">
<tr>
<td>
<input type="text" placeholder="Search for recipes" name="rcpnmesrch">
</td>
<td>
<button type="submit">Search</button>
</td>
</tr>
</form>
</table>
<br/>
<hr/>
<form action="editrecipecheck.php" method="POST">
<table cellspacing=0 cellpadding=5>
<tr>
<td>
</td>
<td>
<label>Recipe Name:</label>
</td>
<td>
<input type="text" placeholder="Recipe Name" name="rcpnme" value="<?php echo $row['recipe_name']; ?>" required>
</td>
</tr>
<tr>
<td>
<label>Ingredient One:</label>
</td>
<td>
<input type="text" placeholder="Ingredient" name="ingnme1" required>
</td>
PHP中的变量范围是按函数定义的,变量不是if
的本地变量。var\u dump($row)
显示了什么?我很清楚,您有两个表单;其中一个不是用
关闭的,除非您忽略了它。另外,
不能成为
的子项。我也没有看到带有提交
名称属性的提交按钮;当用户第一次打开页面时,没有提交表单,它不会进行搜索,并且不会分配$row
。顺便说一句,该错误来自您的输入。最多使用三元运算符。
<td>
<input type="text" placeholder="Recipe Name" name="rcpnme" value="<?php echo $row['recipe_name']; ?>" required>
</td>
<br />
<b>Notice</b>: Undefined index: recipe_name in <b>E:\Program Files\xampp\htdocs\FVRC\editrecipe.php</b> on line <b>52</b>
<br />