-3

I want to add 2 rows of a table having numbers in that and I want that sum of 2 rows in a new row of the table

I wrote my code using sql query..

<?php
    $con =  mysqli_connect("localhost", "root", "", "project");
    if(!$con) {
      die('not connected');
    }
    $con =  mysqli_query($con, "SELECT addplace, stayamount, foodamount, airlinesamount, noofdays, totalamount AS sum(stayamount + foodamount + airlinesamount) choose FROM adddetails");

?>
<div class="container">
  <center><h2>view packages</h2></center>  
  <table class="table table-bordered">
    <th>place</th>
    <th>stay cost</th>
    <th>food cost</th>
    <th>flight cost</th>
    <th>no of days</th>
    <th>total amount</th>
    <th>image</th>

    <?php while($row = mysqli_fetch_array($con, MYSQLI_ASSOC)) { ?>
    <tr>
      <td><?php echo $row['addplace']; ?></td>
      <td><?php echo $row['stayamount']; ?></td>
      <td><?php echo $row['foodamount'] ;?></td>
      <td><?php echo $row['airlinesamount'] ;?></td>
      <td><?php echo $row['noofdays'] ;?></td>
      <td><?php echo $row['totalamount'] ;?></td>
      <td><?php echo $row['choose'] ;?></td>
    </tr>
    <?php } ?>
  </table>
</div>

and I am getting an error.

Can any one rewrite my sql query or php to add 2 rows containing numbers and I want the sum of that rows in a new row

[database image in phpmyadmin ][1]

[my table in my web page (image)][2]

Thanking You

code for image

<input  name="choose"  class="form-control" type="file" >

i want the selected image to be displyed in my website as full image,nt the name of the image what should i do..

[in chose row the image is stored][3]

Dharman
  • 30,962
  • 25
  • 85
  • 135
raj
  • 5
  • 1
  • 6
  • If the code/question is not about formulating a SQL, I prefer to do the SUM as PHP code inside the while loop. – r4ccoon Mar 26 '17 at 09:37

2 Answers2

0

Change your mysql query as below and try. I am considering packageid is primary key

SELECT addplace, stayamount, foodamount, airlinesamount, noofdays, 
    SUM(stayamount + foodamount + airlinesamount) AS totalamount, choose
FROM adddetails GROUP BY packageid
Sachin PATIL
  • 745
  • 1
  • 9
  • 16
  • sir, i have one more doubt...in phpmyadmin i want the image to be displayed in when i upload it...but it showing some [BLOB -23 B] i want the full image to be dispalyed @Sachin PATIL – raj Mar 26 '17 at 10:28
  • try this answer: http://stackoverflow.com/questions/7793009/how-to-retrieve-images-from-mysql-database-and-display-in-an-html-tag – Sachin PATIL Mar 26 '17 at 11:15
-1

While it is not source of the problem, it's not good that you are overriding your $con variable with results. Instead use $result to store result of mysqli_query()

<?php
    $con = mysqli_connect("localhost", "root", "", "project");
    if(!$con) {
      die('not connected');
    }

    $result = mysqli_query($con, "SELECT addplace, stayamount, foodamount, airlinesamount, noofdays, totalamount AS sum(stayamount + foodamount + airlinesamount) choose FROM adddetails");
    print_r($result); # make sure u have expected output here, if it works delete this line

?>
<div class="container">
  <center><h2>view packages</h2></center>  
  <table class="table table-bordered">
    <th>place</th>
    <th>stay cost</th>
    <th>food cost</th>
    <th>flight cost</th>
    <th>no of days</th>
    <th>total amount</th>
    <th>image</th>

    <?php while($row = mysqli_fetch_assoc($result)) { ?>
    <tr>
      <td><?php echo $row['addplace']; ?></td>
      <td><?php echo $row['stayamount']; ?></td>
      <td><?php echo $row['foodamount'] ;?></td>
      <td><?php echo $row['airlinesamount'] ;?></td>
      <td><?php echo $row['noofdays'] ;?></td>
      <td><?php echo $row['totalamount'] ;?></td>
      <td><?php echo $row['choose'] ;?></td>
    </tr>
    <?php } ?>
  </table>
</div>
AltzeM
  • 186
  • 1
  • 1
  • 11
  • And what. ```$var = 4;``` ```$var = $var + 1;``` Now ```$var``` store 5 same as ```$con``` store now mysql_result instead mysql connection. – Wolen Mar 26 '17 at 09:13
  • True, but it is not good practice. However, it is not answer to the question, you are right. – AltzeM Mar 26 '17 at 09:17
  • is the new colum total amount requires a colum in database phpmyadmin, my php admin page is above @altzem – raj Mar 26 '17 at 09:33