I created a table which is updated through a form and each row gets assigned a specific number.
When viewing this table, I want to click on that assigned number and get a page where all the details of that row are displayed.
If I do $sql = "SELECT * FROM clients WHERE nif_id='114522';"; - where the nif_id is the assigned number - I get the values for that number, but I need it to change with every number in the table.
Any ideas?
UPDATE
This is the table code:
<div class="card card-body">
<table class="table">
<thead>
<tr>
<th>NIF</th>
<th>Nome</th>
<th>Apelido</th>
<th>Telemóvel</th>
<th>E-mail</th>
</tr>
</thead>
<tbody>
<?php
include_once '../includes/db.inc.php';
$sql = "SELECT * FROM clients ORDER BY nif_id ASC;";
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$first = $row["prm_nome"];
$last = $row["apelido"];
$phone = $row['nmr_tlm'];
$email = $row['mail'];
$nif = $row['nif_id'];
echo '<tr>';
echo '<td>'.$nif.'</td>';
echo '<td>'.$first.'</td>';
echo '<td>'.$last.'</td>';
echo '<td>'.$phone.'</td>';
echo '<td>'.$email.'</td>';
echo '</tr>';
}
}
?>
</tbody>
</table>
</div>
You can use the get request parameters.
ex: www.myapp.com/table?id=3920393
add functionality in your PHP file as follows
if(isset($_GET["id"])){
$id = $_GET["id"];
$sql = "SELECT * FROM clients WHERE nif_id='".$id."';";
//make db call & display HTML
}
This is a very simple implementation and does not implement any security or SQL injection security. This was more of a conceptual answer as to how you can tackle your problem.
This is quite a common scenario for web-based systems.
<div class="card card-body">
<table class="table">
<thead>
<tr>
<th>NIF</th>
<th>Nome</th>
<th>Apelido</th>
<th>Telemóvel</th>
<th>E-mail</th>
</tr>
</thead>
<tbody>
<?php
include_once '../includes/db.inc.php';
$sql = "SELECT * FROM clients ORDER BY nif_id ASC;";
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$first = $row["prm_nome"];
$last = $row["apelido"];
$phone = $row['nmr_tlm'];
$email = $row['mail'];
$nif = $row['nif_id'];
echo '<tr>';
echo '<td>'.$nif.'</td>';
echo '<td>'.$first.'</td>';
echo '<td>'.$last.'</td>';
echo '<td>'.$phone.'</td>';
echo '<td>'.$email.'</td>';
echo '</tr>';
}
}
?>
</tbody>
</table>
</div>
where the detail.php is another page to query specific details regarding the query nifid.
As a reminder, if the data type of the column is INT, there is no need to use single quotes to surround the value in the SQL statement.
Sample detail.php:
<?php
if(!isset($_GET['nifid']) || (int)$_GET['nifid'] <= 0) {
// Invalid or missing NIFID
header('Location: table.php');
}
include_once '../includes/db.inc.php';
$id = (int)$_GET['nifid'];
$sql = "SELECT * FROM clients WHERE nif_id=$id";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
// TODO: display the result in whatever way you like
?>
Related
I don't know why it doesn't show up anything, I already tested my query and it is working in my phpmyadmin, but in my php code it does not work upon adding the AS keyword. My goal for this is to place a value to a variable coming from the SUM() keyword.
<?php
require_once "user-connect.php";
$user = $_SESSION['id'];
$sql = "SELECT SUM(total) AS sumz FROM cart WHERE userID = $user AND month(orderDate) = month(now()) AND day(orderDate) = day(now())";
$result = $link->query($sql);
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
echo $row['sumz'];
}
if (mysqli_query($link, $sql)) {
} else {
echo "Error: " . $sql . "" . mysqli_error($link);
} ?>
<table cellspacing="0">
<tbody>
<tr class="cart-subtotal">
<th>Cart Subtotal</th>
<td><span class="amount"><?php echo $row['sumz']; ?></span></td>
</tr>
You can use:
$sql = "SELECT SUM(total) FROM cart WHERE..."
And in HTML:
<td><span class="amount"><?php echo $row['SUM(total)']; ?></span></td>
Take a look at PHP fetch assoc guide.
while ($row = $result->fetch_assoc()) {
echo $row['sumz'];
}
An example of getting the SUM value
<?php
$sql="SELECT sum(amount) as total FROM table";
$result = mysqli_query($sql);
while ($row = mysqli_fetch_assoc($result)) {
echo $row['total'];
}
mysqli_close($con);
?>
I currently have this code set up:
$sql = "SELECT * FROM homework WHERE class = '$class'";
$result = mysqli_query($conn, $sql);
$data_exist = false;
if (mysqli_num_rows($result) > 0) {
// output data of each row
$data_exist = true;
while($row = mysqli_fetch_assoc($result)) {
$id = $row["id"];
$teacher_set = $row["teacher_set"];
$class = $row["class"];
$name = $row["name"];
$description = $row["description"];
}
}
And then:
<?php if ($data_exist){?>
<p><?php echo $id ?></p>
<p><?php echo $teacher_set?></p>
<p><?php echo $name?></p>
<p><?php echo $description?></p>
<?php
}?>
However, the issue is if there is multiple results in the database it only outputs one of them, how can I prevent this from happening and output two?
I want to make it so every row has their own section, like this: http://prntscr.com/hcgtqn so if there is only one result, one one will show etc.
You have to echo data in a loop. Right now you are reassigning values in while($row = mysqli_fetch_assoc($result)) iterations and printing just the last one.
You need to print each time you read a row from the database.
about the styles, you can represent it in many ways. In the code below I present it in a table.
<table>
<thead>
<tr>
<th>id</th>
<th>teacher set</th>
<th>name</th>
<th>description</th>
</tr>
</thead>
<tbody>
<?php
$sql = "SELECT * FROM homework WHERE class = '$class'";
$result = mysqli_query($conn, $sql);
$data_exist = false;
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_array($result)) {
$id = $row["id"];
$teacher_set = $row["teacher_set"];
$class = $row["class"];
$name = $row["name"];
$description = $row["description"];
// you need to print the output now otherwise you will miss the row!
// now printing
echo "
<tr>
<td>".$id."</td>
<td>".$teacher_set."</td>
<td>".$name."</td>
<td>".$description."</td>
</tr>";
}
}
else // no records in the database
{
echo "not found!";
}
?>
</tbody>
</table>
</body>
</html>
Firstly, the headings are stored in h1 tags. This is taken from a separate table named "menu_type". Which is linked through a "menu" table.
I am trying to display data on the base like this:
HEADING
Table Data
2nd Heading
Second Data
--- In a loop until it is all completed ---
Here is a like page of what it is doing
I believe I have the methods correct and can see what it is doing, it is printing the first heading, then a blank table, then the second heading and then the data from the first table.
See my code:
<?php
$query = "SELECT * FROM menu_type";
$result = mysqli_query($connect, $query);
$result_array = array();
$numRows = mysqli_num_rows($result); // returns the num of rows from the query above, allowing for dynamic returns
while($row = mysqli_fetch_assoc($result)){
$menuType = $row['type'];
$result_array[] = $menuType; // This array holds each of the menu_types from DB
}
$countArray = count($result_array);
for($i = 0; $i < $numRows; $i++){
$query = "SELECT * FROM menu WHERE type_id='$result_array[$i]'";
$result = mysqli_query($connect, $query) or die(mysqli_error($connect));
echo "
<div id='hide'>
<h1 id='wines' class='head-font text-center head'>$result_array[$i]</h1>
<table class='table table-hover table-responsive'>
<thead>
<tr>
<th>
Item
</th>
<th class='text-right'>
Price
</th>
</tr>
</thead>
<tbody>
<tr>
";
$menuQuery = "SELECT * FROM menu, menu_type WHERE type_id='$i' AND menu.type_id = menu_type.id";
$menuResult = mysqli_query($connect, $menuQuery) or die(mysqli_error($connect));
while ($row = mysqli_fetch_assoc($menuResult)) {
$name = $row['name'];
$description = $row['description'];
$price = $row['price'];
echo "
<td>$name - <small>$description</small></td>
<td class='text-right'>£$price </td>
";
}
echo
"
</tr>
</tbody>
</table>
";
}
// print_r($result_array[2]);
?>
You don't need these anymore, it's like you're repeating the query. It looks incorrect also.
$menuQuery = "SELECT * FROM menu, menu_type WHERE type_id='$i' AND menu.type_id = menu_type.id";
$menuResult = mysqli_query($connect, $menuQuery) or die(mysqli_error($connect));
The menu items are already in this query, you just have to loop through it;
$query = "SELECT * FROM menu WHERE type_id='$result_array[$i]'";
$result = mysqli_query($connect, $query) or die(mysqli_error($connect));
UPDATE 1: this code is incorrect, it doesn't insert the value to the array. I updated the code (after "try this").
$result_array[] = $menuType;
UPDATE 2: the $result is repeatedly used in mysqli functions, the index is being moved. What I did is copied the initial $result to $resultCopy. Try code again, haha
Use array_push($array,$value_you_insert) function, for inserting elements to an array.
Try this;
<?php
$query = "SELECT * FROM menu_type";
$result = mysqli_query($connect, $query);
$resultCopy = $result;
$result_array = array();
$numRows = mysqli_num_rows($result); // returns the num of rows from the query above, allowing for dynamic returns
while($row = mysqli_fetch_assoc($resultCopy)){
$menuType = $row['type'];
array_push($result_array,$menuType);
}
for($i = 0; $i < $numRows; $i++){
echo "
<div id='hide'>
<h1 id='wines' class='head-font text-center head'>".$result_array[$i]."</h1>
<table class='table table-hover table-responsive'>
<thead>
<tr>
<th>Item</th>
<th class='text-right'>Price</th>
</tr>
</thead>
<tbody>
";
$query = "SELECT * FROM menu WHERE type_id='$result_array[$i]'";
$result = mysqli_query($connect, $query) or die(mysqli_error($connect));
while ($row = mysqli_fetch_assoc($result)) {
$name = $row['name'];
$description = $row['description'];
$price = $row['price'];
echo"
<tr>
<td>".$name." - <small>".$description."</small></td>
<td class='text-right'>£".$price."</td>
</tr>
";
}
echo
"
</tbody>
</table>
";
}
?>
enter image description herei am working on project CMS with php and one of my while loops doesn't work and i cant find why.
i am echo on (td) inside of loop and nothing showed on the page but when i echo outside of while loop it work very well.
i have commentet the loop to see you.
Can you give me some help where i have the problem?
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>Id</th>
<th>Author</th>
<th>Comment</th>
<th>Email</th>
<th>Status</th>
<th>In Response to</th>
<th>Date</th>
<th>Approve</th>
<th>Unapprove</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php
$query = "SELECT * FROM comments ";
$select_comments = mysqli_query($connection, $query);
while($row = mysqli_fetch_assoc($select_comments)) {
$comment_id = $row['comment_id'];
$comment_post_id = $row['comment_post_id'];
$comment_author = $row['comment_author'];
$comment_content = $row['comment_content'];
$comment_email = $row['comment_email'];
$comment_status = $row['comment_status'];
$comment_date = $row['comment_date'];
echo "<tr>";
echo "<td>$comment_id</td>";
echo "<td>$comment_author</td>";
echo "<td>$comment_content</td>";
/
echo "<td>$comment_email</td>";
echo "<td>$comment_status</td>";
//THIS IS THE LOOP DOESN'T WORK
$query = "SELECT * FROM posts WHERE post_id = $comment_post_id";
$select_post_id_query = mysqli_query($connection, $query);
while($row = mysqli_fetch_assoc($select_post_id_query)) {
$post_id = $row['post_id'];
$post_title = $row['post_title'];
echo "<td><a href='../post.php?p_id=$post_id'>$post_title</a></td>";
}
echo "<td>$comment_date</td>";
echo "<td><a href='comment.php?approve=$comment_id'>Approve</a></td>";
echo "<td><a href='comment.php?unapprove=$comment_id'>Unapprove</a></td>";
echo "<td><a href='comment.php?delete=$comment_id'>Delete</a></td>";
echo "</tr>";
}
?>
</tbody>
</table>
<?php
//approve posts
if(isset($_GET['approve'])){
$the_comment_id = $_GET['approve'];
$query = "UPDATE comments SET comment_status = 'approved' WHERE comment_id = $the_comment_id ";
$approve_comment_query = mysqli_query($connection, $query);
header("Location: comment.php");
}
//unapprove posts
if(isset($_GET['unapprove'])){
$the_comment_id = $_GET['unapprove'];
$query = "UPDATE comments SET comment_status = 'unapproved' WHERE comment_id = $the_comment_id ";
$unapprove_comment_query = mysqli_query($connection, $query);
header("Location: comment.php");
}
//delete posts
if(isset($_GET['delete'])){
$the_comment_id = $_GET['delete'];
$query = "DELETE FROM comments WHERE comment_id = {$the_comment_id} ";
$delete_query = mysqli_query($connection, $query);
header("Location: comment.php");
}
?>
You are overwriting variable $row in your second loop. You should change it eg. to $subRow to avoid such situation.
i have find the missing part on of my db with the connection.
THNX all you friends :D
I've made this code using a tutorial that allows me to upload users to the database. The whole thing works great, but the only problem is that it starts to show 2 of the same user over and over, the list starts expanding 5 every user i add... What could be the problem causing this?
Item in index that lays out the whole list:
<h2>Names:</h2>
<table border='1'>
<tr>
<th>ID</th>
<th>Username</th>
</tr>
<?php
$sql_list = "SELECT * FROM names ORDER BY username ASC";
$results = mysqli_query($db, $sql_list) or die(mysql_error());
$names = "";
if(mysqli_num_rows($results) > 0) {
while($row = mysqli_fetch_assoc($results)) {
$id = $row['id'];
$user = $row['username'];
$names .= "<tr><td>$user</td></tr>";
echo $names;
}
} else {
echo "No Users Found";
}
?>
</table>
Either output the one record per iteration; or build the whole HTML block, then output the block. I think the simplest would be:
while($row = mysqli_fetch_assoc($results)) {
$id = $row['id'];
$user = $row['username'];
echo "<tr><td>$user</td></tr>";
}
... alternative approach
<h2>Names:</h2>
<table border='1'>
<tr>
<th>ID</th>
<th>Username</th>
</tr>
<?php
$sql_list = "SELECT * FROM names ORDER BY username ASC";
$results = mysqli_query($db, $sql_list) or die(mysqli_error($db));
$names = "";
if(mysqli_num_rows($results) > 0) {
while($row = mysqli_fetch_assoc($results)) {
$id = $row['id'];
$user = $row['username'];
$names .= "<tr><td>$user</td></tr>";
}
} else {
$names = "No Users Found";
}
echo $names;
?>
</table>
Also you can't use mysql_* functions with mysqli_*. See http://php.net/manual/en/mysqli.error.php.
Simplest example of the issue: https://eval.in/627250