[英]Search a MySQL database in PhP
I am trying to search a MySQL
database in PhP
using POST
, but I get the following errors: 我正在尝试使用
POST
在PhP
搜索MySQL
数据库,但出现以下错误:
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
Here is my code: 这是我的代码:
<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
I have tried declaring ban1
, banner1
, and all the other variables outside of the while
statement, yet I still get those errors. 我尝试在
while
语句之外声明ban1
, banner1
和所有其他变量,但仍然遇到这些错误。 Any ideas? 有任何想法吗?
You have to concatenate php variables with html like this 您必须像这样将php变量与html连接
echo "
<div class='jumbotron'>
<h1></h1>
<p>
<b>-----------------------------------------------</b>
<br>
<b>Banned:</b>" . $ban1 .
"<br>
<b>Banner:</b>" . $banner1;
From PHP docs, you can see that 从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.
You can find more info here 您可以在这里找到更多信息
So you have to initialize the variables inside your while loop before you use it to get rid of the notice. 因此,您必须先在while循环中初始化变量,然后才能使用它摆脱通知。
Are you sure that 你确定
if (isset($_POST['submitbtn'])) {
is true? 是真的? you might want to add an else statement to confirm.
您可能需要添加else语句进行确认。 or do you get the same error when you know it is false?
还是在知道错误时得到相同的错误?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.