每当用户执行搜索时,我想显示付费金额的总额。我的代码如下:
<?php
$sql = "SELECT * FROM users";
if(isset($_POST['search'])){
$search_term = mysql_real_escape_string($_POST['search_box']);
$sql .= " WHERE first_name = '{$search_term}'";
$sql .= " OR last_name = '{$search_term}'";
$sql .= "OR month = '{$search_term}'";
}
$query = mysql_query($sql) or die(mysql_error());
?>
<form name="search_form" method="POST"action="displaydata.php">
Search: <input type="text"name="search_box" value="" />
<input type="submit" name="search"value="Search..">
</form>
<table width="70%" cellpadding="5" cellspace="5">
<tr><td>Student ID</td><td>First Name</td><td>Last Name</td><td>Paid Amount</td><td>Course</td><td>Month</td><td>Year</td></tr>
<H3>Payment Voucher</H3><?php while ($row =mysql_fetch_array($query)) {?>
<tr><td><?php echo $row ['student_id']; ?></td><td><?php echo $row ['first_name']; ?></td><td><?php echo $row ['amount']; ?></td><td><?php echo $row ['month']; ?></td>
</tr>
<?php } ?>
答案 0 :(得分:0)
<?php
if(isset($_POST['search'])){
$search_term = mysql_real_escape_string($_POST['search_box']);
$sql = "SELECT * FROM users WHERE first_name = '{$search_term}' OR last_name = '{$search_term}' OR month = '{$search_term}'";
$query = mysql_query($sql) or die(mysql_error());
?>
<form name="search_form" method="POST"action="displaydata.php">
Search: <input type="text"name="search_box" value="" />
<input type="submit" name="search"value="Search..">
</form>
<table width="70%" cellpadding="5" cellspace="5">
<tr><td>Student ID</td><td>First Name</td><td>Last Name</td><td>Paid Amount</td><td>Course</td><td>Month</td><td>Year</td></tr>
<H3>Payment Voucher</H3><?php while ($row =mysql_fetch_array($query)) {?>
<tr><td><?php echo $row ['student_id']; ?></td><td><?php echo $row ['first_name']; ?></td><td><?php echo $row ['amount']; ?></td><td><?php echo $row ['month']; ?></td>
</tr>
<?php
}
}
?>
这应该看起来好多了,而且更容易。但请转到PDO或MySQLi。 MySQL已被弃用,从PHP 7开始它就是GONE。
使用PDO查看此查询的方式:
<?php
$conn = new PDO("mysql:host=localhost;dbname=database", 'user', 'password');
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if(isset($_POST['search'])){
$search_term = htmlEntities($_POST['search_box'], ENT_QUOTES);
$sql = $conn->prepare("SELECT * FROM users WHERE first_name = :search_term OR last_name = :search_term OR month = :search_term");
$sql->bindParam(':search_term', $search_term, PDO::PARAM_STR);
$sql->execute();
?>
<form name="search_form" method="POST"action="displaydata.php">
Search: <input type="text"name="search_box" value="" />
<input type="submit" name="search"value="Search..">
</form>
<table width="70%" cellpadding="5" cellspace="5">
<tr><td>Student ID</td><td>First Name</td><td>Last Name</td><td>Paid Amount</td><td>Course</td><td>Month</td><td>Year</td></tr>
<H3>Payment Voucher</H3>
<?php while ($row = $sql->fetch(PDO::FETCH_ASSOC)) {?>
<tr><td><?php echo $row ['student_id']; ?></td><td><?php echo $row ['first_name']; ?></td><td><?php echo $row ['amount']; ?></td><td><?php echo $row ['month']; ?></td>
</tr>
<?php
}
}
?>
希望这能帮到你!