我正在尝试使用MySQL
在PhP
中搜索POST
数据库,但我收到以下错误:
Notice: Undefined variable: ban1 in /Applications/XAMPP/xamppfiles/htdocs/bans.php on line 266
Notice: Undefined variable: banner1 in /Applications/XAMPP/xamppfiles/htdocs/bans.php on line 268
Notice: Undefined variable: bantl1 in /Applications/XAMPP/xamppfiles/htdocs/bans.php on line 270
Notice: Undefined variable: banreason1 in /Applications/XAMPP/xamppfiles/htdocs/bans.php on line 274
Notice: Undefined variable: ban2 in /Applications/XAMPP/xamppfiles/htdocs/bans.php on line 276
Notice: Undefined variable: banner2 in /Applications/XAMPP/xamppfiles/htdocs/bans.php on line 278
Notice: Undefined variable: bantl2 in /Applications/XAMPP/xamppfiles/htdocs/bans.php on line 280
Notice: Undefined variable: banreason2 in /Applications/XAMPP/xamppfiles/htdocs/bans.php on line 286
这是我的代码:
<h1>Punishments
<div class='col-sm-3 col-md-3 pull-right' style='float:right'>
<div class='container'>
<div class='row'>
<div class='col-sm-3 col-xs-12 col-lg-3'>
<form class='form-search' action='./bans.php' method='post'>
<div class='input-group'>
<input type='text' class='form-control' name='psch' placeholder='Search Players'>
<span class='input-group-btn'>
<button type='submit' name='submitbtn' class='btn btn-search'><span class='glyphicon glyphicon-search'></span></button>
</span>
</div>
</form>
</div>
</div>
</div>
</div>
</h1>
<br>
<hr>";
$ban1 = "";
require ("./connect.php");
if (isset($_POST['submitbtn'])) {
$search = $_POST['psch'];
$searched = mysql_real_escape_string($search);
$result = mysql_query('SELECT * FROM bans WHERE banned="$searched" ORDER BY id DESC LIMIT 0,1') or die('Invalid query: ' . mysql_error());
while ($row = mysql_fetch_assoc($result)) {
$ban1 = $row['banned'];
$banner1 = $row['banner'];
$bantl1 = $row['timeleft'];
$banreason1 = $row['reason'];
$banappeal1 = $row['appealed'];
$banacceptor1 = $row['acceptor'];
$bantime1 = $row['time'];
}
$result = mysql_query('SELECT * FROM bans WHERE banned="$searched" ORDER BY id DESC LIMIT 1,1') or die('Invalid query: ' . mysql_error());
while ($row = mysql_fetch_assoc($result)) {
$ban2 = $row['banned'];
}
$result = mysql_query('SELECT * FROM bans WHERE banned="$searched" ORDER BY id DESC LIMIT 2,1') or die('Invalid query: ' . mysql_error());
while ($row = mysql_fetch_assoc($result)) {
$ban3 = $row['banned'];
}
$result = mysql_query('SELECT * FROM bans WHERE banned="$searched" ORDER BY id DESC LIMIT 3,1') or die('Invalid query: ' . mysql_error());
while ($row = mysql_fetch_assoc($result)) {
$ban4 = $row['banned'];
}
$result = mysql_query('SELECT * FROM bans WHERE banned="$searched" ORDER BY id DESC LIMIT 4,1') or die('Invalid query: ' . mysql_error());
while ($row = mysql_fetch_assoc($result)) {
$ban5 = $row['banned'];
}
echo "
<div class='jumbotron'>
<h1></h1>
<p>
<b>-----------------------------------------------</b>
<br>
<b>Banned:</b>$ban1
<br>
<b>Banner:</b>$banner1
<br>
<b>Time Left:</b>$bantl1
<br>
<b>Reason:</b>$banreason1
<br>
<b>-----------------------------------------------</b>
<br>
<b>Banned:</b>$ban2
<br>
<b>Banner:</b>$banner2
<br>
<b>Time Left:</b>$bantl2
<br>
<b>Reason:</b>$banreason2
<br>
<b>-----------------------------------------------</b>
</p>
<p></p>
</div>
";
}
else
我尝试声明ban1
,banner1
以及while
语句之外的所有其他变量,但我仍然会收到这些错误。有什么想法吗?
答案 0 :(得分:0)
你必须将php变量与html(如
)连接起来echo "
<div class='jumbotron'>
<h1></h1>
<p>
<b>-----------------------------------------------</b>
<br>
<b>Banned:</b>" . $ban1 .
"<br>
<b>Banner:</b>" . $banner1;
答案 1 :(得分:0)
从PHP文档中,您可以看到
E_NOTICE level error is issued in case of working with uninitialized variables, however not in the case of appending elements to the uninitialized array. isset() language construct can be used to detect if a variable has been already initialized.
您可以找到更多信息here
因此,在使用它之前必须初始化while循环中的变量以消除通知。
答案 2 :(得分:0)
你确定吗
if (isset($_POST['submitbtn'])) {
是真的吗?您可能想要添加else语句来确认。或者当你知道它是假的时,你会得到同样的错误吗?