gpt4 book ai didi

php - 从下面的php+mysql表代码中显示总计

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

每当用户执行搜索时,我想显示已支付金额的总计。我的代码如下:

    <?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 } ?>

最佳答案

<?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 开始它就消失了。

这个查询使用 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
}
}
?>

希望对你有帮助!

关于php - 从下面的php+mysql表代码中显示总计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34592045/

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