I m trying to display the records of 2 tables in a single table using php and mysql. I have 2 tables member and payment
member
id | name |.....
----------
101 | abc |..
and payment table contains
id | uid | amount
----------
1 | 101 | 1200
i want to display the record of entered no.My table displays all the rows. I want to display the record of only 1 number.
here is the code.. This is page1 where user as to enter a number and the records of dat number as to display in second page.
<form method="post" action="reportdet2.php">
<label type="text" name="name" maxlength="50" size="30" class="label">Enter the Membership Number which You waant to edit</label><br />
<input type="text" name='id' placeholder="enter Membership Number" class="input" size="40"/><br />
<span class="field">(* Required field)</span><br /><br />
<input type="hidden" name='uid' placeholder="" class="input" size="40"/><br />
<input type="submit" name="submit" value="SUBMIT" class="button"><br /><br /><br /><br />
</form>
</body>
</html>
page2
<?php
mysql_connect("localhost","root","");
mysql_select_db("anthonys");
if(isset($_POST['submit']))
{
$id= $_POST['id'];
if( ! ctype_alnum($id) )
die('invalid id');
$uid=$_POST['uid'];
$query="SELECT m.id, m.fathername, m.mothername
FROM member m JOIN payment p ON m.id = p.uid";
if(mysql_num_rows($run)>0){
echo "<script>window.open('reportdet2.php','_self')</script>";
}
else {
echo "<script>alert('Membership No is Invalid!')</script>";
}
}
?>
<th>No</th>
<th>Name</th>
<th>UID</th>
<th>Amount</th>
</tr>
<?php
$id="";
$id=$_REQUEST["id"];
$uid=$_REQUEST["uid"];
mysql_connect("localhost","root","");
mysql_select_db("anthonys");
$query="SELECT m.id, m.fathername, m.mothername
FROM member m JOIN payment p ON m.id = p.uid";
$run= mysql_query($query);
while($row=mysql_fetch_array($run))
{
$id=$row[0];
$name=$row[1];
$uid=$row[2];
$amount=$row[3];
?>
<tr align='center'>
<td><?php echo $id; ?></td>
<td><?php echo $name; ?></td>
<td><?php echo $uid; ?></td>
<td><?php echo $amount; ?></td>
</tr>
<?php } ?>
</table>
<?php
$result = mysql_query("SELECT sum(amount) FROM payment") or die(mysql_error());
while ($rows = mysql_fetch_array($result)) {
?>
<div class="pull-right">
<div class="span">
<div class="alert alert-success"><i class="icon-credit-card icon-large"></i> Total: <?php echo $rows['sum(amount)']; ?></div>
</div>
</div>
<?php }
?>
</body>
</html>
Add a WHERE m.id = $id
clause to your SQL queries to only show the record(s) that match $id
.
Note that your (example) scripts are susceptible for all kinds of nasty attacks... I do not know whether your actual application has safety measures to prevent SQL injection , etc.. If not, you might want to add them !
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.