Jump to content

show no records when there are no data in sql table and if there is data then go to pdf page


Recommended Posts

<body>
  <div class="header">
    <?php include('navbar.php'); ?>
  </div>
  <div class="container">
    <div class="row mt-4">
      <div class="col-sm-3">
        <p>Patient Name :</p>
      </div>
      <div class="col-sm-3">
        <p><?php echo $_POST['name']; ?></p>
      </div>
    </div>
    <div class="row" id="fir">
      <div class="col-sm-3">
        <p>UHID :</p>
      </div>
      <div class="col-sm-3">
        <p><?php echo $_POST['id']; ?></p>
      </div>
    </div>
    <?php
    $_SESSION['p_name'] = $_POST['name'];
    $_SESSION['p_id'] = $_POST['id'];
    ?>
    <form action="personal_medical_record_download.php" method="POST">
      <div class="form-header mt-4 text-center">Medical Record</div>
      <div class="card">
        <div class="card-header">
          <h2>Any Allergies</h2>
        </div>
        <div class="card-body">
          <?php
          $uhid = $_SESSION['p_id'];
          $conn = mysqli_connect("127.0.0.1", "root", "iMpAcTHeaLTH7%", 'threephih');
          $query = " Select * from allergies where uhid = '$uhid' ";
          $query_res = mysqli_query($conn, $query);
          while ($query_run = mysqli_fetch_array($query_res)) {
          ?>
            <div class="form-group">
              <label for="1">1. Are you allergic to any substance?</label>
              <p style="margin-left: 25px"><?php echo $query_run['ques_1']; ?></p>
            </div>
            <div class="form-group">
              <label for="2">2. What happened to you when you took this substance?</label>
              <p style="margin-left: 25px"><?php echo $query_run['ques_2']; ?></p>
            </div>
            <div class="form-group">
              <label for="3">3. When was this reaction?</label>
              <p style="margin-left: 25px"><?php echo $query_run['ques_3']; ?></p>
            </div>
            <div class="form-group">
              <label for="4">4. Since when have you taken that substance?</label>
              <p style="margin-left: 25px"><?php echo $query_run['ques_4']; ?></p>
            </div>
            <div class="form-group">
              <label for="5">5. Any other kind of allergies?</label>
              <p style="margin-left: 25px"><?php echo $query_run['ques_5']; ?></p>
            </div>
          <?php
          }
          ?>
            </div>
      </div>
      <div class="submit-button  mt-4 text-center">
        <button type="submit" value="Pdf" name="pdf">PDF</button>
      </div>
    </form>
  </div>
</body>

I'm trying to show that "No records" message when there are no data in sql table but if there is data then go to the pdf page. please help me

First step: fix your code so that you do not use a loop to read a single row. Read the one single row and then output it.

After you've done that, you'll know whether you have that one single row to output, and if not...

actually i'm totally new and i dont know how can i fix my code

I'm trying to fetch data from different sql table for eg: allergies table, health record table if there is data available in that table then display whole page with pdf option if not then display full page only "no records found" 

9 hours ago, do-rami said:

actually i'm totally new and i dont know how can i fix my code

This code uses a loop to fetch records, which suggests there are multiple records it might need to fetch. It does not sound like that is the case.

$query_res = mysqli_query($conn, $query);
while ($query_run = mysqli_fetch_array($query_res)) {

The fix is simple: keep the mysqli_fetch_array but don't use a while loop.

$query_res = mysqli_query($conn, $query);
$query_run = mysqli_fetch_array($query_res);

You can then add an if to check if that fetch returned anything.

$query_res = mysqli_query($conn, $query);
$query_run = mysqli_fetch_array($query_res);
if ($query_run) {
    ...
}

Inside this new block is where you put your answers. After the block you add an else for the "no records found" message.

$query_res = mysqli_query($conn, $query);
$query_run = mysqli_fetch_array($query_res);
if ($query_run) {
    ...
} else {
    ...
}

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.