Add table row for each table row in database table - php

I couldn't really find another post about this problem.
I am trying to add in html a new table row for every new table row I have in phpmyadmin table with php script.
My code is:
<?php
if($check == true)
{
echo" <a href='php/logout.php' class='subpages'>Logout</a>
<tr>
<td>
FileName: <a href='../uploads/$filename'>$filename</a>
</td>
<td>";
$connect = mysql_connect("localhost","root","");
mysql_select_db("phplogin"); // select DB
$getcomment = mysql_query("SELECT comment FROM files WHERE name='$filename'");
while($row = mysql_fetch_assoc($getcomment))
{
echo"Comment: ".$row['comment']." </td>
</tr>";
}
}
?>
The check at the begging checks if a file is actualy uploaded if it is a connection is made to get the name of the file and the comment attached to it.
Then finaly when the info has been taken I want to make a table row and 2 colusm for FileName and Comment. The result of my code replaces the already created row with a new one.
Is there a way to create a new row for each new File and Comment that has been taken with PHP script only ?

Try this... please let us know is it working or not...
if($check == true)
{
echo" <a href='php/logout.php' class='subpages'>Logout</a>
<table><tbody><tr>
<th>File Name</th>
<th>Comments</th>
</tr>
<tr>
<td>
FileName: <a href='../uploads/$filename'>$filename</a>
</td>
</tr>
";
$connect = mysql_connect("localhost","root","");
mysql_select_db("phplogin"); // select DB
$getcomment = mysql_query("SELECT comment FROM files WHERE name='$filename'",$connect);
$res = mysql_fetch_row($getcomment);
$num_rows = mysql_num_rows($getcomment);
if ($num_row > 0){
for ($i=0;$i<$num_row;$i++){
echo "<tr><td><Comment: ".$res[$i]."</td></tr>";
}
}
}

Related

How to delete the entire row in the database from html table instead of deleting entire table in the database

I have added delete button to the html table on each row and when clicked on delete button the entire row should be deleted but instead whole table in the database is being deleted.
here is my code for admin.php
<div class="container mt-3 ml-3">
<table class="table">
<thead>
<tr>
<th>S.No</th>
<th>Name</th>
<th>Email</th>
<th>Rating</th>
<th>Review</th>
<th>Image</th>
<th>Suggestion</th>
<th>NPS</th>
<th>Delete</th>
</tr>
</thead>
<tbody class="table-warning">
<?php
include 'database_conn.php'; // makes db connection
$sql = "SELECT feedbackID, name, email, rating, review, image, suggestion, nps
FROM feedback
ORDER BY feedbackID Desc";
$queryResult = $dbConn->query($sql);
// Check for and handle query failure
if($queryResult === false) {
echo "<p>Query failed: ".$dbConn->error."</p>\n";
exit;
}
// Otherwise fetch all the rows returned by the query one by one
else {
if ($queryResult->num_rows > 0) {
while ($rowObj = $queryResult->fetch_object()) {
echo "<tr>
<td>{$rowObj->feedbackID}</td>
<td>{$rowObj->name}</td>
<td>{$rowObj->email}</td>
<td>{$rowObj->rating}</td>
<td>{$rowObj->review}</td>
<td>{$rowObj->image}</td>
<td>{$rowObj->suggestion}</td>
<td>{$rowObj->nps}</td>
<td><a id='delete' href=delete.php?id={$rowObj->feedbackID}>Delete</a></td>
";
}
}
}
?>
</tr>
</tbody>
</table>
</div>
And here my code for delete.php. I think there is something wrong in the sql query I made.
<?php
include 'database_conn.php'; // makes db connection
$sql = "DELETE FROM feedback WHERE feedbackID=feedbackID";
if ($dbConn->query($sql) === TRUE) {
echo "Record deleted successfully. Please go to Customer Feedback Page by clicking"; echo "<a href='http://unn-w18031735.newnumyspace.co.uk/feedback/admin.php'> here</a>";
} else {
echo "Error deleting record: " . $dbConn->error;
}
$dbConn->close();
?>
This is wrong:
DELETE FROM feedback WHERE feedbackID=feedbackID
it is always true as it will be equal to itself.
What you want to use is parameters here. $_GET['id'] is where the id is.
If you use PDO, something like
$stmt = $dbConn->prepare("DELETE FROM feedback WHERE feedbackID=:feedback_id");
$stmt->execute(['feedback_id' => $_GET['id']]);
For mysqli,
$stmt = $mysqli->prepare("DELETE FROM feedback WHERE feedbackID=?");
$stmt->bind_param("i",$_GET['id']);
$stmt->execute();
this solution in delete.php has worked.
$feedbackID = $_GET["id"];
$sql = ("DELETE FROM feedback WHERE feedbackID= '$feedbackID'");

MySQL, PHP: Display MySQL table in HTML table

sorry, I'm still very novice with server-side (back-end) development, thank you for helping in advance.
Recently, I've been developing a web page which you can choose which table you want to display from the mySQL server, then it will create an editable table in HTML using the mySQL table data.
So far, I'm only able to fetch which table to get from mySQL and display it using in the form of an array, which doesn't really look that great.
I wonder if you can display the array in a form of an editable table without knowing the how many columns, column names as it is different for every different table.
Later on, I have to upload the cell that is updated back to mySQL database and I've no idea how to do that as well.
Sorry for the trouble, this website had been a great help for me and the community is great. Thanks a lot!
Image of what I have so far:
Code I have so far:
SelectTable:
<div class="table-responsive">
<b>List of Tables</b>
<table class="table table-condensed selection_table">
<tbody>
<tr>
<?php
if ($tableResult = mysqli_query($conn,"show tables")){
while($table = mysqli_fetch_array($tableResult)) {
echo("<tr> <td>". "<a class = 'list_tables' href = ?clickedTable=$table[0]>". $table[0] . "</a>" ."</td> </tr>");
}
}else{
die("<b>"."No Table in Database!"."</b>");
}
if (isset($_GET['clickedTable'])){
$selectedTable = $_GET["clickedTable"];
}
?>
</tr>
</tbody>
</table>
</div>
Fetch array from selected table:
<?php
if (isset($_GET['clickedTable'])){
echo("<b> Current Table is: </b> ".$selectedTable. "<br/>");
$query = "SELECT * FROM $selectedTable";
if ($result = mysqli_query($conn , $query)) {
while ($row = mysqli_fetch_array($result)){
print_r($row);
}
}
}else{
echo("Please select a table");
}
?>

MySQL and PHP: Only the first field in the table is displayed

I am working with PHP and MySQL. I need to retrieve all the rows from the database table and display on the browser in tabular form, but for some reason only the First field in the table gets displayed and others fields don't show up or are not pulled from the database.
Here is my PHP code.
<?php
include("DBConn.php");
$result = mysql_query("SELECT * FROM new_table");
if(!$result) {
die("Database query failed: " . mysql_error());
}
echo "<table>
<caption>Signal Data:</caption>
<thead>
<tr>
<th scope=\"col\">TagName:</th>
<th scope=\"col\">Enabled</th>
<th scope=\"col\">EU Value</th>
</tr>
</thead>
<tbody>";
while ($row = mysql_fetch_array($result)) {
$tag = $row["TagName"];
$status = $row["Enabled"];
$Ev = $row["EUValue"];
echo "<tr>
<th scope=\"row\">$tag</th>
<td>$status</td>
<td>$EV</td>
</tr>";
}
echo "</tbody></table>";
?>
What is wrong with this PHP code? The code itself executes fine without any error.
The third row can be corrected by matching the case of the variable names. You are assigning the value to a variable named $Ev (lowercase 'v'):
$Ev = $row["EUValue"];
but attempting to use a variable named $EV (uppercase 'V'):
<td>$EV</td>
For both the second and third fields, ensure that Enabled and EUValue exactly match the names of the fields in your database, including case and spelling.
Finally, check the data itself to ensure that the fields have data in your database, and that the data is displayable in your HTML output.

Display MYSQL results in html table

Im not sure if a similar question has been asked before, but here goes anyway. (I did do a search and found nothing relating to my question).
I am developing a website in which videos are played using the HTML5 video player. I have a connection to my database, a "watch" page that pulls all the correct data using a variable linked to the id (watch.php?v=1). I would like to have an index page where the most recent videos are pulled. They are ordered by the column "id" and everything works when I try and pull one result from the query. How would I go about getting multiple values? Here is my php code (server details hidden):
<?php
$mysqli = new mysqli("HIDDEN", "HIDDEN", "HIDDEN", "HIDDEN");
$sql = "
SELECT id, title, imgsrc, description
FROM videos
ORDER BY id DESC
LIMIT 2
";
$result = $mysqli->query($sql);
$video = mysqli_fetch_array($result, MYSQLI_ASSOC);
mysqli_close($mysqli);
?>
And here is my HTML code for the table.
<table>
<tr>
<td><h2><? echo $video['title']; ?></h2></td>
</tr>
</table>
That isn't the full code, but once I know the procedure I can apply it where needed!
I'm quite new to php and mysql, I can connect to databases but that's about it so a full walkthrough about what does what would be great!
Many Thanks,
James Wassall
You can iterate in for or while loop by calling mysqli_fetch_array($result, MYSQLI_ASSOC):
<table>
<?php
$mysqli = new mysqli("HIDDEN", "HIDDEN", "HIDDEN", "HIDDEN");
$sql = "
SELECT id, title, imgsrc, description
FROM videos
ORDER BY id DESC
LIMIT 2
";
$result = $mysqli->query($sql);
while($video = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
?>
<tr>
<td><h2><? echo $video['title']; ?></h2></td>
</tr>
<?php
}
mysqli_close($mysqli);
?>
</table>
Note that you should consider checking error statements, null controls etc.
try this
while($video = $result->fetch_array(MYSQLI_ASSOC))
{
?>
<tr>
<td><h2><?php echo $video['title']; ?></h2></td>
</tr>
<?php
}
?>
Each time you call mysqli_fetch_array, only one row is fetched.
You need to do something like
while ($video = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
// output $video['title']
}
(according to: http://www.php.net/manual/en/mysqli-result.fetch-array.php)
Try this,
<table>
<?php while($row = $result->fetch_array(MYSQLI_ASSOC)){ ?>
<tr>
<td><h2><? echo $row['title']; ?></h2></td>
<td><h2><? echo $row['description']; ?></h2></td>
</tr>
<?php } ?>
</table>

Hyperlinks from rows in Mysql Table

The thing is that I have made application form which saves in db. And I want on other page to display just some rows of the mysql table in php table where the first column is hyperlink to the single application as it is in the form. My question is how it can automatically make hyperlinks and pages for each form?
This is my code for now
mysql_connect("host", "user", "password") or die(mysql_error());
mysql_select_db("dbname") or die(mysql_error());
$data = mysql_query("SELECT * FROM applications ") or die(mysql_error());
echo ' <table width="760" border=1>
<tr>
<th>Заявление<br>От дата:</th>
<th>От/До</th>
<th>Статус</th>
</tr>';
while($info = mysql_fetch_array( $data ))
{
echo '
<tr>
<td>'.$info['today'] .'</td>
<td>От '.$info['data1data'] .'.'.$info['data1mesec'] .'.'.$info['data1god'] .' до '.$info['data2data'] .'.'.$info['data2mesec'] .'.'.$info['data2god'] .' </td>
<td>';
if($info['status'] == 1) {
echo '<img src="Images/approved.jpg" />';
}
else {
echo '<img src="Images/declined.jpg" />';
}
echo ' </td>
</tr> ';
}
echo '</table>';
The result I am trying to get is a table with 3 columns and rows for every application and from the first column of the application I get redirected to a single webpage for that application form and this link to be auto made by code something like "applications.php?id=[application id]"
I don't understand so much but:
<?php
$query = mysql_query("SELECT * FROM applications");
while($t = mysql_fetch_array($query)){
echo ''.$t['name'].'<br/>';
}
?>
Don't know what's the problem.

Categories