Query gives a blank page - php

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.

Related

php clicking on users name to display profile

So i have this code to display all the users in my database and to access them. That works fine but is there any way to get where it says click here just to display a variable in this case the leader name(aka user name)?
<?php
require_once "config.php";
$sql = "SELECT id , leader_name, nation_name, power FROM nation_info";
$result = $link->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$nationList = [];
$userid = $row['id'];
echo ' click here '; // the click here on this line
// echo '<a class="viewProfile" href="viewnation.php?id=' . $userid . '"><button>View Profile</button></a>'; old method of viewing profile
echo " Nation Name: " . $row["nation_name"]. " Leader Name " . $row["leader_name"]. " Power " . $row["power"];
echo "<br>";
}
} else {
echo "0 results";
}
$link->close();
?>
Are you talking about this? Just printing the name in place of click here?
<?php
require_once "config.php";
$sql = "SELECT id , leader_name, nation_name, power FROM nation_info";
$result = $link->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$nationList = [];
$userid = $row['id'];
echo ' ' . $row["leader_name"] . ' '; // the click here on this line
// echo '<a class="viewProfile" href="viewnation.php?id=' . $userid . '"><button>View Profile</button></a>'; old method of viewing profile
echo " Nation Name: " . $row["nation_name"]. " Leader Name " . $row["leader_name"]. " Power " . $row["power"];
echo "<br>";
}
} else {
echo "0 results";
}
$link->close();
?>

How to put mysqli while result into one variable and echo it in html

How do I put ALL the while result in mysqli select into one variable and echo into the html document?
Code are based on W3school
PHP
$sql = "SELECT firstname, lastname FROM MyGuests";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
//previously it was echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "
";
$parsedcontent = '<div class="col-md-4">'.$row["firstname"].'</div><div class="col-md-4">'.$row["lastname"].'</div>';
}
} else {
echo "0 results";
}
HTML
<div class="row">
<?php echo $parsedcontent; ?>
</div>
$data = array();
// output data of each row
while($row = $result->fetch_array(MYSQLI_ASSOC)) {
// push retrieved data to data array in order for return later
array_push($data, $row);
}

Select Data by Id PHP

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);
?>

Extracting from database to textarea

I'm comparing current data with updated data to check whether there are changes in information, and add the changes to a new table changes:
if (isset($_POST['submit']))
{
$sql = "SHOW COLUMNS FROM Employees";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result)){
$tempname = $row['Field'];
$sql2 = "UPDATE Employees SET ".$row['Field']."= '$_POST[$tempname]' WHERE AFNumber='".$_GET["af"]."'";
$result2 = mysqli_query($con,$sql2);
if ($con->query($sql2) === TRUE) {
} else {
echo "Error: " . $sql2 . "<br>" . $con->error;
echo '<script>swal("Error", "Something went wrong '.$con->error.'", "error");</script>';
}
$sqlOldData = "SELECT * FROM Employees WHERE AFNumber='".$_GET["af"]."' AND (".$row['Field']." NOT LIKE '".$_POST[$tempname]."')";
$result3 = $con->query($sqlOldData);
if ($result3->num_rows > 0) {
while($row3 = $result3->fetch_assoc()) {
$sql3 = "INSERT INTO Changes (Table, AFNumber, Attribute,DateChanged,HRUser,OldValue,NewValue)
VALUES ('Employees', '".$_GET["af"]."', '".$row["Field"]."', '".date('dd/m/Y HH:mm:ss')."', '$login_session', '.$row3[0]', '$_POST[$tempname]')";
if ($con->query($sql3) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql3 . "<br>" . $con->error;
}
}
} else {
echo "0 results";
}
}
Now i want to extract information about the changes such as date user ... And put them in a textarea tag in this form:
<textarea name="changes" rows="50" cols="59" disabled>
12/07/2015 - User:"Mike" Changed:"Actual Location" From: "blabla" to "bla"
</textarea>
But I'm not sure how to do this, any help please...
Without knowing much about your data (for example what AFNumber is) I would suggest simply querying everything from the Changes table, and displaying them in the desired form:
$changes = $con->query("SELECT * FROM Changes WHERE Table = 'Employees'");
if ($changes->num_rows > 0) {
echo '<textarea name="changes" rows="50" cols="59" disabled>' . "\n";
while ($row = $changes->fetch_assoc()) {
echo sprintf('%s - User:%s Chnaged:"%s" From: "%s" to "%s"',
$row['DateChanged'], $row['HRUser'], $row['Attribute'],
$row['OldValue'], $row['NewValue']) . "\n";
}
echo "</textarea>";
}
To display the data I simply use echo here, but using some template system shouldn't make much difference to the core concept of the solution.
From your comment, you have the following code :
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
// output data of each row
while($row = $result->fetch_assoc())
{
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
}
else
{
echo "0 results";
}
$conn->close();
To get the information into a text area in the way you are doing, use:
echo '<textarea name="changes" rows="50" cols="59" disabled>id:' . $row['id' . ' - Name: ' . $row['firstname'] . ' ' . $row['lastname'] . '</textarea>';

Running if database row number is less than 10

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");

Categories