I have trouble to select a set of specific data using ID from the database. For example, employee one has a unique id of e000000001, when I click the view button in the index will lead to employee detail page which shows the detail of that particular employee instead of all the employees' detail. Thank you.
//from index.php page
<?php
require_once 'db/dbEmpList.php';
$sqlStr = "SELECT * FROM employees;";
$result = $connection->query($sqlStr);
if ($result->num_rows > 0) {
echo "<table class='table table-sm'><thread><tr><th>Full Name</th><th>Employee ID</th><th>Position</th><th>View Employee's Details</th></tr>";
while ($row = $result->fetch_assoc()) {
echo "<tr><td>"
. $row["empName"]. "</td><td>"
. $row["empID"]. "</td><td>"
. $row["position"]. "</td>"
. "<td> <a href='employeedetail.php?id={$row["empID"]}'>View</a>"
. "</td></tr>";
}
}
// from employee page
require_once 'db/dbEmpDetail.php';
$sql = "SELECT * FROM employees where empID = '{$row["empID"]}' ";
$result = mysqli_query($connection, $sql);
if (mysqli_num_rows($result)) {
while ($row = mysqli_fetch_assoc($result)) {
echo '<tr>' .'<td>' .$row["empName"].'</td>'.'<td>'. $row["position"].'</td>' .'<td>'.$row["empNRIC"].'</td>' .'<td>'.$row["empID"].'</td>' .'<td>'.$row["empEmail"].'</td>' .'<td>'.$row["empPwd"].'</td>' . "</tr>";
}
} else {
echo "0 results";
}
mysqli_close($connection);
?>
// FROM EMPLOYEE PAGE
The way you retrieve URL query string is wrong. You should be using $_GET to get the query string from URL. In your case it should be $_GET['id']. See the code below:
require_once 'db/dbEmpDetail.php';
$employeeid = trim(mysqli_real_escape_string($_GET['id']));
$sql = "SELECT * FROM employees where empID = '".$employeeid."' ";
$result = mysqli_query($connection, $sql);
if (mysqli_num_rows($result)) {
while ($row = mysqli_fetch_assoc($result)) {
echo '<tr>' .'<td>' .$row["empName"].'</td>'.'<td>'. $row["position"].'</td>' .'<td>'.$row["empNRIC"].'</td>' .'<td>'.$row["empID"].'</td>' .'<td>'.$row["empEmail"].'</td>' .'<td>'.$row["empPwd"].'</td>' . "</tr>";
}
}
else {
echo "0 results";
}
mysqli_close($connection);
?>
Related
I'm trying to count two different tables but it's not working, and I cannot find out where the problem is, and how to implement it with PHP.
$sql = "SELECT COUNT(users.user_id) AS totalUsers FROM users, SELECT COUNT(bikes.bike_id) AS totalBikes FROM bikes";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "<td><td>" . $row["totalUsers"] . "</td></tr>";
echo "<td><td>" . $row["totalBikes"] . "</td></tr>";
}
} else {
echo "0 results";
}
$conn->close();
Get the amounts from each table separately. Each SELECT is a unique resultset from your query, listing them with commas won't retrieve all data as you are expecting.
$result = $conn->query("SELECT COUNT(users.user_id) AS totalUsers FROM users");
$totalUsers = 0;
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
$totalUsers = $row["totalUsers"];
}
$result = $conn->query("SELECT COUNT(bikes.bike_id) AS totalBikes FROM bikes");
$totalBikes = 0;
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
$totalBikes = $row["totalBikes"];
}
$conn->close();
Then you can work with the values stored in $totalUsers and $totalBikes:
echo "<tr><td>" . $totalUsers . "</td></tr>";
echo "<tr><td>" . $totalBikes . "</td></tr>";
I want to select name, phone from approval(tableB) using condition in users(tableA), I got the below code
$email = 'email';
$sql = "SELECT phone, name FROM approval RIGHT JOIN users ON users.email=email.$assigned";
$result = $conn->query($sql);
if ($result) { // output data of each row
while($row = $result->fetch_assoc()) {
echo "<center><table class='table table-bordered'><thead><tbody><tr><td>Name</td><td>" . $row["name"]. " </td></tr><tr><td>Phone</td><td> " . $row["phone"]. " </td></tr><tr><td></td><td> ".file_get_contents("jquery.php")."</td></tr></thead></tbody> </center>";
}
echo "</table>";
} else {
echo "<center>!Sorry you have not been paired.</center>";
}
}
$conn->close();
?>
on running it, it gives me a blank page.
I am having a huge problem. I have displayed data on a single events page the events being displayed are the rows of data in the database.
Here's the code.
<?php
$sql = "SELECT * FROM events ORDER BY `startdate` DESC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<div class='event col-sm-3'>
<h1>". $row['eventname'] . "</h1>
<p><b>Regular:</b> " . $row['regular'] . "Ksh</p>
<p><b>VIP:</b> " . $row['vip'] . "Ksh</p><br>
<p><b>" . $row['startdate'] . "</b></p>
<p><b>Tickets remaining:<b>" . $row['tickets'] . "</p><br><br>
<button class='button'>book</button>
</div>";
}
}
?>
Here are the events being displayed
Now I would want when I click the button that says book the event the fields(items) in the specific row that I have clicked(using the book button) be displayed in a different page and get stored in variables.
If I understand what you are trying to do, you might want to use an 'a' tag and not a button.
You should pass the 'id' value of the ticket to the page where you want to have an independent query (individual_ticket.php)
Your code should look like this
<?php
$sql = "SELECT * FROM events ORDER BY `startdate` DESC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<div class='event col-sm-3'>
<h1>". $row['eventname'] . "</h1>
<p><b>Regular:</b> " . $row['regular'] . "Ksh</p>
<p><b>VIP:</b> " . $row['vip'] . "Ksh</p><br>
<p><b>" . $row['startdate'] . "</b></p>
<p><b>Tickets remaining:<b>" . $row['tickets'] . "</p><br><br>
Book
</div>";
}
}
?>
And inside the 'individual_ticket.php'.
Do something like.
<?php
if(isset($_GET['id']))
{
$id = filter_var($_GET['id'], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH);
$sql = "SELECT * FROM events WHERE id = '$id'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
//you can start assigning variables here
}
}
}
?>
I'm pretty new at PHP/MySQL, so please be patient with me.
I am trying to get a list of members in a table to show up on a page. Right now it's showing the first member about 10 times and not displaying anyone else's name. I DID have it working, but I don't know what happened. I just want it to display everyone's name once. Here is my code:
<?php $select = mysql_query("SELECT * FROM `member_staff` WHERE `username`='$_SESSION[USR_LOGIN]' AND `status`='Active'");
$row = mysql_fetch_array($select);
$rows = mysql_num_rows($select);
$teaching = $row[teaching];
if ($rows==0){echo "Sorry, you don't appear to be a professor.";}
else { ?>
<?php }
$select2 = mysql_query("SELECT * FROM `classes_enrolled` WHERE `course`='" . $teaching . "' ORDER BY `student_name`") or die(mysql_error());
$count = mysql_num_rows($select2);
$row2 = mysql_fetch_array($select2);
$student=$row2[student_name];
if($count==NULL) {
echo "<table width=\"80%\">\n";
echo "<tr><td><center>Nobody has registered for your class yet!</center></td></tr>\n";
echo "</table>\n";
echo "<br /><br />\n\n";
}
else {
echo "<center><font size=\"3\"><b>YEAR 1, TERM 2</b></font></center>";
echo "<table width=\"80%\" class=\"table-stripes\">\n";
echo "<tr><td width=\"50%\"><b>STUDENT</b></td></tr>\n";
$select3 = mysql_query("SELECT * FROM `members` WHERE `username`='" . $student . "'") or die(mysql_error());
$row3 = mysql_fetch_array($select3);
while($row2 = mysql_fetch_array($select2)) {
$house=$row3[house];
echo "<tr><td><strong class=\"$house\">$student</strong></td></tr>";
}
echo "</table>"; }
?>
I miss look on your code, since it is mess, but disregard the mysqli and mysql thing, you want to show how many student in the teacher's classes.
<?php $select = mysql_query("SELECT * FROM `member_staff` WHERE `username`='$_SESSION[USR_LOGIN]' AND `status`='Active'");
$row = mysql_fetch_array($select);
$rows = mysql_num_rows($select);
$teaching = $row[teaching]; <--- This only get first row of the course, if you want multiple course under same username, you need to loop it.
if ($rows==0){echo "Sorry, you don't appear to be a professor.";}
else { ?>
<?php }
$select2 = mysql_query("SELECT * FROM `classes_enrolled` WHERE `course`='" . $teaching . "' ORDER BY `student_name`") or die(mysql_error());
$count = mysql_num_rows($select2);
$row2 = mysql_fetch_array($select2);
$student=$row2[student_name]; <----- This only get the first row of the student name, if you want multiple student under a course, you need to loop it.
if($count==NULL) {
echo "<table width=\"80%\">\n";
echo "<tr><td><center>Nobody has registered for your class yet!</center></td></tr>\n";
echo "</table>\n";
echo "<br /><br />\n\n";
}
else {
echo "<center><font size=\"3\"><b>YEAR 1, TERM 2</b></font></center>";
echo "<table width=\"80%\" class=\"table-stripes\">\n";
echo "<tr><td width=\"50%\"><b>STUDENT</b></td></tr>\n";
$select3 = mysql_query("SELECT * FROM `members` WHERE `username`='" . $student . "'") or die(mysql_error());
$row3 = mysql_fetch_array($select3);
while($row2 = mysql_fetch_array($select2)) {
$house=$row3[house]; <----This only show the first row of $house under same student, so you need to loop it too.
echo "<tr><td><strong class=\"$house\">$student</strong></td></tr>";
}
echo "</table>"; }
?>
So what you really want to do is
<?php
$select = mysql_query("SELECT * FROM `member_staff` WHERE `username`='$_SESSION[USR_LOGIN]' AND `status`='Active'");
$rows = mysql_num_rows($select);
if ($rows==0){echo "Sorry, you don't appear to be a professor.";}
else { ?>
<?php }
while( $row = mysqli_fetch_array( $select ) ) {
$teaching = $row[teaching];
$select2 = mysql_query("SELECT * FROM `classes_enrolled` WHERE `course`='" . $teaching . "' ORDER BY `student_name`") or die(mysql_error());
$count = mysql_num_rows($select2);
if($count==NULL) {
echo "<table width=\"80%\">\n";
echo "<tr><td><center>Nobody has registered for your class yet!</center></td></tr>\n";
echo "</table>\n";
echo "<br /><br />\n\n";
} else {
while( $row2 = mysql_fetch_array($select2) ) {
$student=$row2[student_name];
echo "<center><font size=\"3\"><b>YEAR 1, TERM 2</b></font></center>";
echo "<table width=\"80%\" class=\"table-stripes\">\n";
echo "<tr><td width=\"50%\"><b>STUDENT</b></td></tr>\n";
$select3 = mysql_query("SELECT * FROM `members` WHERE `username`='" . $student . "'") or die(mysql_error());
while($row3 = mysql_fetch_array($select3)) {
$house=$row3[house];
echo "<tr><td><strong class=\"$house\">$student</strong></td></tr>";
}
echo "</table>";
}
} // END ELSE
}
} // END ELSE
?>
I'm trying to make a function work only if the number in the row 'click' in my datbase is less than 10. This is what I have right now: I put the echo in there just to see if the condition is working or not.
<?php
include'connect.php';
$result = mysqli_query($con,"SELECT id, link_name, click FROM clicks");
while($row = mysqli_fetch_array($result))
{
if ($row['click'] < 10) {
echo $row['id'] . " " . $row['link_name']. " " .$row['click'];
echo "<br>";
}
}
mysqli_close($con);
?>
try;
$result = mysqli_query($con,"SELECT id, link_name, click FROM clicks WHERE click<10");
while($row = mysqli_fetch_array($result))
{
echo $row['id'] . " " . $row['link_name']. " " .$row['click'];
echo "<br>";
}
mysqli_close($con);
?>
Why don't you use a where clause in your query?
$result = mysqli_query($con,"SELECT id, link_name, click FROM clicks
where click <10");