Unable To View All Entries In Table - php

I wonder whether someone may be able to help me please.
Firstly my apologies. I would have liked to provide a link to this rather than printing the code below, but I need this to run off my live server tables.
I'm using the code below to generate a table of all location records pertinent to the current user.
<form name="locationsconsole" id="locationsconsole" method="post" action="locationsaction.php">
<table width="864" cellpadding="0" cellspacing="0">
<thead>
<tr>
<th width="17"></th>
<th width="99"><div align="center">Location Name</div></th>
<th width="287"><div align="left">Location Address</div></th>
<th width="88"><div align="center">No. Of Finds Made </div></th>
<th width="86"></th>
<th width="72"></th>
<th width="84"></th>
</tr>
</thead>
<tbody>
<?php
$query = "SELECT l.locationid, f.locationid, l.locationname, l.userid, l.returnedaddress, count(f.locationid) as totalfinds FROM detectinglocations as l left join finds as f on l.locationid=f.locationid WHERE l.userid='$idnum' ORDER BY l.locationname";
$result = mysql_query($query) or die('error');
if (mysql_num_rows($result) == 0)
echo"<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td colspan='6'><div align='center'><strong>There are no records set up</strong></div></td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>";
else
{
while($obj=mysql_fetch_object($result))
{
?>
<tr>
<td><input type="hidden" name="lid" value="<?php echo $obj->locationid;?>"/></td>
<td><div align="center"><?php echo $obj->locationname;?></div></td>
<td><div align="left"><?php echo $obj->returnedaddress;?></div></td>
<td><div align="center"><?php echo $obj->totalfinds;?></div></td>
<td><div align="center"><input name="type" type="submit" value="View Details"/></div></td>
<td><div align="center"><input name="type" type="submit" value="Add Finds"/></div></td>
<td><div align="center"><input name="type" type="submit" value="Add Images"/></div></td>
<td width="129"><div align="left"><input name="type" type="submit" value="View Location Finds"/></div></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
</form>
Although the query is retrieving the right information and the buttons on the row work, the problem I'm having is that although there should 3 records shown in the list, only the first is shown.
I'm the first to admit that I'm certainly no expert when it comes to PHP, but I've been working on this for days and written the script many, many times, but I just can't seem to find a solution.
I just wondered whether someone could possibly look at this please and let me know where I'm going wrong.

As pointed out by both #Bulk and #ShawnVaser there was a problem with my query. This is the solution:
Query
$query = "SELECT l.*, COUNT(f.locationid) as totalfinds FROM detectinglocations l LEFT JOIN finds f ON f.locationid = l.locationid WHERE l.userid = '28' GROUP BY l.locationname";
$result = mysql_query($query);

Related

how to use sql query result in html table

I am trying to create a html table showing results from php sql query. it is a result page of students php code is as under
$r1=$_GET["r"];
$con=mysqli_connect(localhost,chumspai_tlss,Tls121,chumspai_tlsResult);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM nursery_blue_ WHERE sr_='$r1'");
while($row = mysqli_fetch_array($result))
{
html code is
<pre>
<form name="frmResult" id="frmResult" action="" method="post" onsubmit="return checkEmpty();">
<table width="80%" cellpadding="5" cellspacing="5" border="0">
<tr>
<td class="heading noborder">Enter Your Roll Number:</td>
<td class="noborder"><input type="text" id="r" name="r" value="" /></td>
</tr>
<tr>
<!--
<td class="heading noborder">Enter Your Name:</td>
<td class="noborder"><input type="text" id="name" name="name" value="" /></td>
</tr>
<tr>
<td class="heading noborder">Search by</td>
<td class="noborder"><input type="radio" id="option" name="option" value="rno" checked="checked" />
Roll No
<input type="radio" id="option" name="option" value="name" />
Name </td>
</tr>
-->
<tr>
<td class="noborder"> </td>
<td class="noborder"><input type="submit" name="submit" value="Search" />
<input type="reset" name="reset" value="Clear" />
</td>
</tr>
<!--<tr>
<td colspan="2"> <embed src="images/wait.swf"></embed></td>
</tr> -->
</table>
</form>
<div style="border:1px solid #000000;">
<table width="100%" cellpadding="10" cellspacing="0" border="0">
<tr>
<td class="heading grey" width="30%">RNO</td>
<td><?php
Print $row['sr_'];
?>
</td>
</tr>
<tr>
<td class="heading grey">NAME</td>
<td class="shade"></td>
</tr>
<tr>
<td class="heading grey">FATHER</td>
<td></td>
</tr>
<tr>
<td class="heading grey">regno</td>
<td></td>
</tr>
</table>
<table width="100%" cellpadding="10" cellspacing="0" border="0">
<tr class="grey">
<td rowspan="2" class="heading">Sr.no </td>
<td rowspan="2" class="heading">Name of subject </td>
<td rowspan="2" class="heading">Maximum Marks</td>
<td colspan="7" class="heading">detail of marks Obtained</td>
<tr class="grey">
<td class="heading">PART ONE</td>
<td class="heading">Total</td>
</tr>
<tr>
<td>1</td>
<td>Urdu</td>
<td></td>
<td> </td>
<td></td>
</tr>
<tr class="shade">
<td>2</td>
<td>English</td>
<td></td>
<td> </td>
<td></td>
</tr>
<tr>
<td>3</td>
<td>Islamyat</td>
<td></td>
<td> </td>
<td></td>
</tr>
<tr class="shade">
<td>4</td>
<td>pakstudies</td>
<td></td>
<td> </td>
<td></td>
</tr>
<tr class="shade">
<td>6</td>
<td></td>
<td></td>
<td></td>
<td>0</td>
</tr>
<tr>
<td>7</td>
<td></td>
<td></td>
<td></td>
<td>0</td>
</tr>
<tr class="shade">
<td>8</td>
<td></td>
<td></td>
<td></td>
<td>0</td>
</tr>
<tr class="shade">
<td>9</td>
<td></td>
<td></td>
<td></td>
<td>0</td>
</tr>
<tr class="grey">
<td colspan="2" class="heading">TOTAL</td>
<td class="heading">1100</td>
<td colspan="4" class="heading"></td>
</tr>
<tr class="grey">
<td colspan="3" class="heading">NOTIFICATION</td>
<td class="heading"></td>
<td class="heading"></td>
<td colspan="2" class="heading"></td>
</tr>
<tr>
<td colspan="7">(i) This provisional result intimation is issued as a notice only. Errors and omissions are excepted.</td>
</tr>
</table>
</pre>
please help me how to embed this php query with this html table and html form also.
you are not so far.
The variable $row is an array containing your data. Try this to see it's structure in your while call:
print_r($row);
Using this command you will see the name of each item of your array. Note it somewhere. Then you can do something like this:
...<td><?php echo $row['desired_column_name']; ?></td>...
If you receive data from your mysql query, this should do the trick.
Hope it helps,
Paul
Try This :
$result = mysql_query("select * from emp");
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td id=SrNo$cnt >".$row['eno']."</td>";
echo "<td id=ItemId$cnt >".$row['eId']."</td>";
echo "<td>". "<button name='Update' id='update' onclick='show(".$cnt.")'>UPDATE</button>"."</td>";
echo "<td>". "<button name='Report' id='show' onclick='Report(".$row['SrNo'].")'>REPORT</button>"."</td>";
echo "</tr>";
echo "<div id=show$cnt>";
echo "</div>";
$cnt++;
}

check email address used by others in database, if yes, pop up alert, if no, continue to next step

i am constructing a registration page for new members. after filling in the form, it will proceed to the confirm page. after confirmation, it will proceed to the reg_add.php to add the data from the form to the database.
reg_new.php --> reg_confirm.php --> reg_add.php
i am trying to edit the code in the confirm page (reg_confirm.php) to include the email availability check. it seems that it can automatically detect the duplicate email and after clicking the confirm button.
Duplicate entry 'shop#gmail.com' for key 'PRIMARY' will be shown.
yet, it is not success to have a pop up alert and stay in the confirm page. please help.
<?php
session_start();
$_SESSION['email'] = $_POST['email_reg'];
$_SESSION['password'] = $_POST['password_reg'];
$_SESSION['name_reg'] = $_POST['name_reg'];
$_SESSION['month'] = $_POST['month'];
$_SESSION['telephone_reg'] = $_POST['telephone_reg'];
$_SESSION['room_reg'] = $_POST['room_reg'];
$_SESSION['floor_reg'] = $_POST['floor_reg'];
$_SESSION['block_reg'] = $_POST['block_reg'];
$_SESSION['building_reg'] = $_POST['building_reg'];
$_SESSION['estate_reg'] = $_POST['estate_reg'];
$_SESSION['street_reg'] = $_POST['street_reg'];
$_SESSION['district_reg'] = $_POST['district_reg'];
$_SESSION['region_reg'] = $_POST['region_reg'];
$regemail = $_POST["email_reg"];
$connect = mysql_connect("127.0.0.1","root","") or die("not connecting");
mysql_select_db("shop") or die("no db :'(");
$numrows = mysql_query("SELECT membermail FROM member WHERE memberemail='$regemail'");
if ($numrows!=0)
{
echo "<script>alert('Email has been used by others!');window.location.href= 'reg_new.php';</script>";
}
?>
</head>
<body>
<div align="center" class="style1"><span class="style2">Registeration Confirm</span>
<p class="style2"> </p>
<form action="reg_add.php" method="post" enctype="multipart/form-data" name="form1">
<table width="602" height="180" border="1">
<tr>
<td colspan="3">Login Information</td>
</tr>
<tr>
<td width="160" class="style6">Email address: </td>
<td colspan="2"><span class="style7"><?php echo $_SESSION['email']?></span></td>
</tr>
<tr>
<td> </td>
<td colspan="2"> </td>
</tr>
<tr>
<td colspan="3">User information </td>
</tr>
<tr>
<td><span class="style6">Name:</span></td>
<td colspan="2"><?php echo ($_SESSION['name_reg'])?></td>
</tr>
<tr>
<td><span class="style6">Month of birth :</span></td>
<td colspan="2"><?php echo ($_SESSION['month'])?>
</tr>
<tr>
<td><span class="style6">Contact telephone:</span></td>
<td colspan="2"><?php echo ($_SESSION['telephone_reg'])?></td>
</tr>
<tr>
<td rowspan="8"><span class="style6">Contact address:</span></td>
<td width="153"><p class="style6">Room/ Flat no.: </p> </td>
<td width="267"><p class="style6"><?php echo ($_SESSION['room_reg'])?></p> </td>
</tr>
<tr>
<td><span class="style6">Floor: </span></td>
<td width="267"><span class="style6"><?php echo ($_SESSION['floor_reg'])?></span></td>
</tr>
<tr>
<td><span class="style6">Block/ Tower:</span></td>
<td width="267"><span class="style6"><?php echo ($_SESSION['block_reg'])?></span></td>
</tr>
<tr>
<td><span class="style6">Building:</span></td>
<td width="267"><span class="style6"><?php echo ($_SESSION['building_reg'])?></span></td>
</tr>
<tr>
<td><span class="style6">Estate: </span></td>
<td width="267"><span class="style6"><?php echo ($_SESSION['estate_reg'])?></span></td>
</tr>
<tr>
<td><span class="style6">Street:</span></td>
<td width="267"><span class="style6"><?php echo ($_SESSION['street_reg'])?></span></td>
</tr>
<tr>
<td><span class="style6">District:</span></td>
<td width="267"><span class="style6"><?php echo ($_SESSION['district_reg'])?></span></td>
</tr>
<tr>
<td><span class="style6">Region: </span></td>
<td width="267"><span class="style6"><?php echo ($_SESSION['region_reg'])?></span></td>
</tr>
<tr>
<td colspan="3" class="style6"> </td>
</tr>
</table>
<p>
<input name="confirm" type="submit" id="confirm" value="Confirm">
</p>
</form>
<p>
<input name="modifty" type="submit" id="modifty" value="Modify" onClick="history.go(-1)">
</p>
</div>
</body>
</html>
Your problem is how you are retrieving the number of rows from the database.
mysql_query() does not retrieve the number of rows. It retrieves a result set.
Instead of:
$numrows = mysql_query("SELECT membermail FROM member WHERE memberemail='$regemail'");
if ($numrows!=0)
{
echo "<script>alert('Email has been used by others!');window.location.href= 'reg_new.php';</script>";
}
Use:
$email = mysql_real_escape_string($_POST["email_reg"]);
$result = mysql_query("SELECT COUNT(memberemail) AS emailCount FROM member WHERE memberemail='{$email}'");
$row = mysql_fetch_row($result);
if($row[0]>0){
echo "<script>alert('Email has been used by others!');window.location.href= 'reg_new.php';</script>";
}
ALWAYS make sure you escape your data before using it in a database query.

update mysql table with list values

i have a form that extract data from mysql table into a form, each row has a menu to choose a value from and i want to update mysql with each value choosen for each row when the 'Apply To All' button is clicked but doesnt work at all.,here is my code.
<td><form id="main" name="main" method="post" action="setProjectStatus.php" onsubmit="return validateMain();">
<table width="100%" cellspacing="1" cellpadding="1">
<tr>
<td width="35%" rowspan="3"><img src="../img/project.jpg" alt="Comp Sci Stud" width="325" height="199" border="2" /></td>
<td width="65%" height="42" colspan="2"><table width="94%" cellpadding="1" cellspacing="1" class="main_table">
<tr class="table_title">
<td width="100%" class="table_title">Set Project Status. </td>
</tr>
<tr>
<td height="26"> </td>
</tr>
<tr>
<td height="26"><table width="100%" cellspacing="1" cellpadding="1">
<tr class="table_head">
<td width="2%" height="35"><div align="center"></div></td>
<td width="26%" height="35"><div align="center">Student Name</div></td>
<td colspan="2"><div align="center">Project</div></td>
<td width="19%"><div align="center">Status</div></td>
</tr>
<?php
session_start();
$username = $_SESSION['username'];
require_once("mysqlConnect.php");
//
$sql="SELECT * FROM spms_Student";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
echo "There are $count projects to be undertaken.";
while($rows=mysql_fetch_array($result)){
//
$query = "SELECT name FROM spms_systemUser WHERE userId = '".$rows[0]."'";
$result1 = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result1);
$name = $row[0];
?>
<tr>
<td height="25" align="center"> </td>
<td align="center"><?php echo $name; ?></td>
<td colspan="2" align="center"><?php echo $rows[1]; ?></td>
<td align="center"><label>
<select name="select" class="form_field_100px_select">
<option value="Pending" selected="selected">Pending</option>
<option value="Approved">Approved</option>
<option value="Disapproved">Disapproved</option>
</select>
</label></td>
</tr>
<?php
}
?>
<tr class="pager_bg">
<td height="35"> </td>
<td> </td>
<td width="37%" align="right"><input name="done" type="button" id="done" value="Done" onclick="window.location='../coordinatorMenu.html'" /></td>
<td width="16%"><label>
<input name="approveAll" type="submit" id="approveAll" value="Approve All" />
</label></td>
<td><input name="apply" type="submit" id="apply" value="Apply To All" /></td>
</tr>
<?php
mysql_close();
?>
</table></td>
</tr>
</table></td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td colspan="2"><label></label> <label></label></td>
</tr>
</table>
</form></td>
You're trying to reuse your MySQL connection while still holding on to the resultset from the first query. You need to create a second connection for the inner loop queries. Even better would probably be to rewrite your query using a join, but I can't say for sure without knowing your schema.

PHP MySQL populating values from database

lets say i retrieve all of the values where their position belongs to top8.I populate them out in a table and instead of displaying different kinds of values , it displays 3 tables with 3 different values, how is this so? any help so that different values belonging to certain values will all be displayed out? i only need one table with 3 different values.
<?
$facebookID = "top8";
mysql_connect("localhost","root","password") or die(mysql_error());
mysql_select_db("schoutweet") or ie(mysql_error());
$data= mysql_query("SELECT schInitial FROM matchTable WHERE position='".$facebookID."'")
or die(mysql_error());
while($row = mysql_fetch_array($data))
{
?>
<center>
<table border="0" cellspacing="0" cellpadding="0" class="tbl_bracket">
<tr>
<td class="brack_under cell_1"><a href="www.facebook.com"/>team 1.1><?= $row['schInitial']?><a/></td>
<td class="cell_2"> </td>
<td class="cell_3"> </td>
<td class="cell_4"> </td>
<td class="cell_5"> </td>
<td class="cell_6"> </td>
</tr>
<tr>
<td class="brack_under_right_up">team 1.2><?= $row['schInitial']?></</td>
<td class="brack_right"><!--1.2.1--></td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td class="brack_right"><!--2.1--></td>
<td class="brack_under"><!--3.1--></td>
<td><!--here?--></td>
<td><!--there?--></td>
<td><!--everywhere?--></td>
</tr>
</table>
</center>
<?
}
?>
</body>
That's because your <table> tag is within the loop! Place the <table> tag outside the while loop.
place your table tags outside the while loop
Because your writing the table tag inside the while loop. Everything inside the loop is done each loop cycle. If you only want to have one table in the output, you'll have to open and close the table outside of the loop, like this:
$data= mysql_query("SELECT schInitial FROM matchTable WHERE position='".$facebookID."'")
or die(mysql_error());
?>
<center>
<table border="0" cellspacing="0" cellpadding="0" class="tbl_bracket">
<?
while($row = mysql_fetch_array($data))
{
?>
<tr>
<td class="brack_under cell_1"><a href="www.facebook.com"/>team 1.1><?= $row['schInitial']?><a/></td>
<td class="cell_2"> </td>
<td class="cell_3"> </td>
<td class="cell_4"> </td>
<td class="cell_5"> </td>
<td class="cell_6"> </td>
</tr>
<tr>
<td class="brack_under_right_up">team 1.2><?= $row['schInitial']?></</td>
<td class="brack_right"><!--1.2.1--></td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td class="brack_right"><!--2.1--></td>
<td class="brack_under"><!--3.1--></td>
<td><!--here?--></td>
<td><!--there?--></td>
<td><!--everywhere?--></td>
</tr>
<?
}
?>
</table>
</center>
That will, however, print three rows per loop and therefore per record (but you have references to the table contents in two of them, so I suppose that's what you want?).
Also take care about some not well-formed HTML you have there (e.g. the > character in the expression team 1.1> / team 1.2>. If you want to print the > character to the browser, encode it as HTML entity (> for this case). You also have a probably superfluous </ in the first column of the second row (</</td>).
you need to echo the HTML part as well in the while loop like
echo '<table>';

Can php code execute while creation of pdf using tcpdf?

I m working on module in which i have to make pdf from php page. I m Using tcpdf for that but m facing one problem that file contain some mysql queries and php coding which is not executed by pdf page.
$prn_no = $_POST['prn_no'];
$current_sem = $_POST['current_sem'];
$qr_fetch_sem_res_id = mysql_query("SELECT * FROM table1 WHERE ((prn='$prn_no') AND (semisterName='$current_sem'))")or die(mysql_error());
$qr_fetch_sem_result_ans = mysql_fetch_array($qr_fetch_sem_res_id);
<tr>
<td colspan="11" align="left" valign="middle">Programme Name: <?php echo $qr_fetch_sem_result_ans['programme_name'];?></td>
</tr>
<tr>
<td colspan="11" align="center" valign="middle"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="27%">Seat No.: <?php echo $qr_fetch_sem_result_ans['seatNo'];?></td>
<td width="3%"> </td>
<td width="22%">PR No. : <?php echo $qr_fetch_sem_result_ans['prn'];?></td>
<td width="2%"> </td>
<td width="17%">Semester : <?php echo $qr_fetch_sem_result_ans['semisterName'];?></td>
<td width="1%"> </td>
<td width="25%">Month / Year Of Exam : <?php echo $qr_fetch_sem_result_ans['month_year_of_exam'];?> </td>
<td width="3%"> </td>
</tr>
<tr>
<td colspan="3">Name: <?php echo $qr_fetch_sem_result_ans['student_name'];?></td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td colspan="7">College / Institute: <?php echo $qr_fetch_sem_result_ans['institute_name'];?></td>
<td> </td>
</tr>
</table></td>
</tr>
I'm going to go out on a limb here and suggest that you run your queries fist and then build your pdf file. If you run the queries after you build the pdf then of course it will not have access to your data. If that doesn't help then I must not understand what you're asking.

Categories