0

I want to display the invoice bill for each customer when admin clicks on view bill button for that customer. Also I want to display all the newspaper details in a single invoice when customer name is same.I have included the relevant code and database snapshots.

c_details.php

<?php
session_start();
include_once("connect.php");
$query="SELECT * FROM  sub_details";
$result=mysqli_query($conn,$query);
?>



<table align="center">     
    <h3 style="color: black; font-weight: bold; text-align: center;"> 
    Subscription details</h3><br>
    <tr>            
      <th style="text-align: center;">Id</th>
      <th style="text-align: center;">Name</th>
      <th style="text-align: center; width: 900px">Newspaper</th>
      <th style="text-align: center;">Duration</th>
      <th style="text-align: center;">Price</th>
      <th style="text-align: center; width: 800px">Subscription date</th>
      <th style="text-align: center;">Remaining Days</th>
      <th style="text-align: center;">Bill</th>
    </tr>
    <?php while($rows=mysqli_fetch_assoc($result)) {
      ?>


      <tr>
        <td><?php echo $rows['id']; ?></td>
        <td><?php echo $rows['name']; ?></td>
        <td><?php echo $rows['newspaper']; ?></td>
        <td><?php echo $rows['duration']; ?></td>
        <td><?php echo $rows['price']; ?></td>
        <td><?php echo $rows['sdate']; ?></td>
        <?php  echo var_dump($result->fetch_all(MYSQLI_ASSOC));
        $date1=date_create();
        $date2=date_create($rows['edate']);
        $interval=date_diff($date2,$date1)->format("%a days");
         ?>
         <td><?php echo $interval; ?></td>
         <td>
     <form action="invoice.php" method="GET">   
     <?php $invoiceId = $rows['name']; ?>  
   <button type="submit" onclick= "window.open('invoice.php?name= '<?php echo  
   $rows['name']; ?>)" class="btn btn-primary btn-lg" value="submit" >
   View bill</button></td>
      </form>                                                               
      </tr>
      <?php } ?>
   </table>

invoice.php

<?php
include_once("connect.php");   
$invoiceId = isset($_GET['name']) ? $_GET['name'] : null;
if($invoiceId) {
    $query = "SELECT * FROM  sub_details WHERE name = ? limit 1";
    $result = mysqli_query($conn,$query);
?>  

    <?php while($rows=mysqli_fetch_assoc($result)) {
              ?>
         <form style="text-align: left;margin-left: 30px" class="register-form" id="register-form">
        <div class="form-group"> Bill no: &nbsp;&nbsp;&nbsp;&nbsp; <input type="text" name="id" value="<?php echo $rows['id']; ?>"></div><br>
        <div class="form-group">Name:  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input type="text" name="name" value="<?php echo $rows['name']; ?>"></div><br>
        <div class="form-group">Address: &nbsp; <input type="text" name="address" value="<?php echo $rows['address']; ?>"></div><br><br>
        </form>
         </th>
         </tr>

         <td>
         <table cellpadding="5px" cellspacing="6px" style="width: 75%; border-radius:20px;">
         <tr>
            <th>Newspaper</th>
            <th >Duration</th>  
            <th >Price</th>             
         </tr>

        <tr>
            <td ><?php echo $rows['newspaper']; ?></td>
            <td><?php echo $rows['duration']; ?></td>       
            <td><?php echo $rows['price']; ?></td>

        </tr>

        <tr>
            <td>DELIVERY CHARGES</td>
            <td colspan="3" style="padding-right:60px;text-align: right;">50</td>
        </tr>

        <tr>
            <th>Total</th>         
             <?php
             $t_price=$rows['price']+ 50; ?>
         <th colspan="3" style="text-align: right;padding-right: 55px"><?php echo $t_price; ?></th>
        </tr>

        <tr>
            <th colspan="4" >
                <br><br><br><br><br><br><br>

            </th>
        </tr>
        </table>
        </td>   


         <tr>
            <th colspan="4" style="border-top-color: #ffff4d">
        <p style="text-align: left;">Note: Clients are requested to pay the bill before 5th of every month.</p> 
            </th>
          </tr>
          <?php } ?>
        </table>

Database screenshots

Newspaper details

Mahesh nainar
  • 31
  • 1
  • 8
  • 1
    Please include more details of want you want to do. Also note that PHP is a back-end language, and you need an interaction in the front-end to achieve that. – Yuri-M-Dias Mar 03 '20 at 18:02

1 Answers1

2

First, you should pass the invoice ID for each record in c_details.php so that you can identify them later:

<button type="submit" onclick= "window.open('invoice.php?id='<?php echo $rows['id']; ?>)" class="btn btn-primary btn-lg" value="submit" >View bill</button>

It will produce URLs like invoice.php?id=<id>, where <id> is the ID for each record in the database. So, for example, a record with the ID 102 will be invoice.php?id=102.

On invoice.php, you can retrieve the ID with $_GET['id']. You should adjust your query to fetch the data for the given invoice:

$invoiceId = isset($_GET['id']) ? $_GET['id'] : null;
if($invoiceId) {
    $query = $conn->prepare("SELECT * FROM sub_details, signup_c WHERE id = ? limit 1";
    $query->bind_param('s', $invoiceId);
    $query->execute();
    $result = $query->get_result();
}

If you're using PHP 7.0 or later, you can simplify your code using the null coalescing operator:

$invoiceId = $_GET['id'] ?? null;

I strongly recommend you to learn about how to prevent SQL injections in PHP.

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • @dharman you're totally right. I've just updated my answer. ;) – Cadu De Castro Alves Mar 04 '20 at 00:51
  • Great job, but OP seems to be using mysqli, not PDO. Also there is `??` operator in PHP so you do not have to use the long ternary. You can simply do `$invoiceId = $_GET['id'] ?? null` – Dharman Mar 04 '20 at 01:09
  • @dharman null coalescing is a PHP 7+ feature and I don't know which version s/he is using. But I'll update it with a disclaimer. – Cadu De Castro Alves Mar 04 '20 at 01:38
  • @CaduDeCastroAlves I tried your method the link is working fine but I am not getting the output. I got these errors Notice: Undefined variable: result in C:\xampp\htdocs\NEWSCART\invoice.php on line 52 Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null given in C:\xampp\htdocs\NEWSCART\invoice.php on line 52 – Mahesh nainar Mar 04 '20 at 04:05
  • @mahesh You need to enable error reporting. https://stackoverflow.com/a/22662582/1839439 – Dharman Mar 04 '20 at 09:13
  • @cadu There's no other version available anymore. If someone is not using PHP 7 yet you should give a big warning. Never hold back from giving up to date answers. – Dharman Mar 04 '20 at 09:15
  • @CaduDeCastroAlves I am not getting any output in invoice.php. The url is not showing the name.I got this on the url(http://localhost/NEWSCART/invoice.php?name=) – Mahesh nainar Mar 04 '20 at 11:04
  • @Maheshnainar could you please update your question with the actual code? – Cadu De Castro Alves Mar 04 '20 at 11:09
  • @Maheshnainar you didn't echo the name: `window.open('invoice.php?name=')`. It should be `window.open('invoice.php?name=')`. – Cadu De Castro Alves Mar 05 '20 at 10:24
  • @Maheshnainar there is an issue in your SQL code as well. You didn't adjust your code to bind the value to the query. Please, double check my example and make the adjustments. – Cadu De Castro Alves Mar 05 '20 at 10:28
  • I tried with printing echo statement but the url doesn't work – Mahesh nainar Mar 05 '20 at 10:57
  • @Maheshnainar I didn't understand. Could you provide more information about what you've tried? – Cadu De Castro Alves Mar 05 '20 at 14:04
  • I added echo statement before $rows in c_details.php but when I click on view bill button a blank page with invoice.php? is opened the name of the user is not displayed in the url. Also I changed the query same as you posted still it doesn't work. – Mahesh nainar Mar 05 '20 at 14:15
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/209092/discussion-between-mahesh-nainar-and-cadu-de-castro-alves). – Mahesh nainar Mar 05 '20 at 14:17