简体   繁体   中英

Display Total from below php+mysql table Code

Whenever user performs a search,I want to display total from paid amount. My code is as follows:

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

This should look a lot better and it's easier. But PLEASE change to PDO or MySQLi. MySQL is deprecated and starting from php 7 it's GONE.

How this query could look using 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 
} 
} 
?>

Hope this helps you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM