How to capture the values in a row - php

I have a dynamic table and I want to make sure when the delete button is pressed, you have to remove the corresponding values ​​in the table of the database.

You must fetch the id using the code below and an anchor tag to refer to your_script.php that process the deletion associated with the variable id that contains the id of data
$query = mysqli_query($your_connection, "SELECT * FROM your_table") or exit(mysqli_error);
while($rows =mysqli_fetch_array($query)): //fetch data from the table
$id = $rows['id']; //get id of specific data
$product = $rows['product'];
$seller = $rows['seller'];
$quant = $rows['quantity'];
$price = $rows['price'];
echo "<tr>";
echo "<td>" . $product . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $seller . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $quant . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $price . "</td>";
echo "</tr>";
//anchor tag href to your_script.php with the variable $id attached to it then use javascript to confirm delete or not
echo "<tr>";
echo "<td>
Delete <td>";
echo </td> ";
echo "</tr>";
endwhile;

Related

Echoing a PHP SQL query to a webpage

I'm trying to get a total amount for a list of sales using an SQL statement in PHP, I already have the SQL statement but I'm not sure how to get the results of the query onto the webpage, I'm planning on putting it in the second PHP tag after the table.
$result = mysqli_query($dbc,$query);
echo "<p>Customers on file = " .mysqli_num_rows($result). "</p>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" .$row[0]. "</td>";
echo "<td>" .$row['last_name']. "</td>";
echo "<td>" .$row['Customer_id']. "</td>";
echo "<td>" .$row['3']. "</td>";
echo "<td>" .$row['4']. ", " .$row['5']. " " .$row['6']. "</td>";
echo "<td align='right'>$" .number_format($row['credit_limit']). "</td>";
echo "<td align='right'>" .$row['salescnt']. "</td>";
echo "<td align='right'>" .number_format($row['salestot'],2). "</td>";
echo "<tr>";
}
?>
</table>
<?php
$query = "SELECT sum(total) AS totsales FROM sales_order";
echo "<p>Total of all sales = " </p>" ?>
</body>
</html>

Retrieve data from database to textbox wordpress

I need to fetch data from PHP MyAdmin database to WordPress website page. Since I am new to WordPress I am not too sure which method I have to follow. I tried to edit the source code of the page by adding the new PHP code inside PHP tags. But it did not work. And I found a file named 'functions.php' in the local directory where my project located. Is that the file I need to use this code? I've already written the code and I just need to know where to use it.
<?php
$results = $wpdb->get_results( "SELECT * FROM $table_name"); // Query to fetch data from database table and storing in $results
if(!empty($results)) // Checking if $results have some values or not
{
echo "<table width='100%' border='0'>"; // Adding <table> and <tbody> tag outside foreach loop so that it wont create again and again
echo "<tbody>";
foreach($results as $row){
$userip = $row->user_ip; //putting the user_ip field value in variable to use it later in update query
echo "<tr>"; // Adding rows of table inside foreach loop
echo "<th>ID</th>" . "<td>" . $row->id . "</td>";
echo "</tr>";
echo "<td colspan='2'><hr size='1'></td>";
echo "<tr>";
echo "<th>User IP</th>" . "<td>" . $row->user_ip . "</td>"; //fetching data from user_ip field
echo "</tr>";
echo "<td colspan='2'><hr size='1'></td>";
echo "<tr>";
echo "<th>Post ID</th>" . "<td>" . $row->post_id . "</td>";
echo "</tr>";
echo "<td colspan='2'><hr size='1'></td>";
echo "<tr>";
echo "<th>Time</th>" . "<td>" . $row->time . "</td>";
echo "</tr>";
echo "<td colspan='2'><hr size='1'></td>";
}
echo "</tbody>";
echo "</table>";
}
?>

Data from Database into the table (can't figure out how to do layout)

This is my code (horrible one):
<?php
include 'connect/con.php';
$result = mysqli_query($con,"SELECT id, vidTitle FROM newsvid");
$result1 = mysqli_query($con,"SELECT imgCover, vidSD FROM newsvid");
$result2 = mysqli_query($con,"SELECT published FROM newsvid");
echo "<table width=\"600\" border=\"1\"><tbody>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo '<td width=\"10%\">'.$row['id'].'</td>';
echo "<td width=\"90%\">" . $row['vidTitle'] . "</td>";
echo "</tr>";
}
echo "</tbody></table>";
echo "<table width=\"600\" border=\"1\"><tbody>";
while($row = mysqli_fetch_array($result1)) {
echo "<tr>";
echo "<td width=\"40%\">" . $row['imgCover'] . "</td>";
echo "<td width=\"60%\">" . $row['vidSD'] . "</td>";
echo "</tr>";
}
echo "</tbody></table>";
echo "<table width=\"600\" border=\"1\"><tbody>";
while($row = mysqli_fetch_array($result2)) {
echo "<tr>";
echo "<td >" . $row['published'] . "</td>";
echo "</tr>";
}
echo "</tbody></table>";
mysqli_close($con);
?>
</body>
</html>
The question is how to show data from database in this layout:
--------------------------------
-id----------vidTitle-----------
--------------------------------
-imgCover------vidSD------------
--------------------------------
----------published-------------
So every time I will add more data , another block like I showed before will add up under existing one.
........................................................................................
There's no need to write 3 queries. You could do that with only one select, and then put all the echos inside a while. That way you're writing, it would run all the ids and titles first, then it would put a table after the table, with cover and vidSD.
Try to make a single query:
SELECT id, vidTitle, imgCover, vidSD, published FROM newsvid
That way you will have, on each row returned from database, all the information about the same row.
Now, running a while is the same as you're doing, just adapting some HTML:
echo "<table width='600' border='1'><tbody>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo '<td width=\"10%\">'.$row['id'].'</td>';
echo "<td width=\"90%\">" . $row['vidTitle'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td width=\"40%\">" . $row['imgCover'] . "</td>";
echo "<td width=\"60%\">" . $row['vidSD'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='2'>" . $row['published'] . "</td>";
echo "</tr>";
}
echo "</tbody></table>";
You may want to order it too. Adding ORDER BY id DESC, would do that.

Table creation with php and mysqli

i'm trying to do a table with php which takes the data from my database and build's the table im html but despite my while loop , only the 1st row is getting into the table. If i change the while loop before the
echo "<table border='1' cellpadding='2' cellspacing='2'";
I get all of them but in 3 tables.
What's wrong with the code ?
<?php
require 'connect.php';
$query = $link->query("SELECT * FROM fornecedor");
echo "Fornecedores";
echo "<table border='1' cellpadding='2' cellspacing='2'";
echo "<tr>
<td>Nome</td>
<td>NIF</td>
<td>Cidade</td>
<td>Rua</td>
<td>NrPorta</td>
<td>Website</td>
<td>email</td>
</tr>";
while ($row = mysqli_fetch_array($query)) {
echo "<tr>";
echo "<td>" . $row["Nome"] . "</td>";
echo "<td>" . $row["NIF"] . "</td>";
echo "<td>" . $row["Cidade"] . "</td>";
echo "<td>" . $row["Rua"] . "</td>";
echo "<td>" . $row["NrPorta"] . "</td>";
echo "<td>" . $row["Website"] . "</td>";
echo "<td>" . $row["Email"] . "</td>";
echo "</tr>";
echo "</table>";
};
?>
Move echo "</table>"; out of the while loop-
....
while(){
.....
// don't close the table tag here
}
echo "</table>";
For each row you are adding the closing </table> tag which should not be like that.
echo "</table>"; code must be out of the while loop. It must be written once only.

How do i delete the records from the table?

I am using a form to get the input of username and password to store the value into the database, once the form is submitted i have defined a table to show all the values from the users table, it have 3 fields (id, name, pass) i want to delete each record by it's id .
i am fetching the data from the users table by using the following code:
while($row = mysql_fetch_assoc($result_select)) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['pass'] . "</td>";
echo "</tr>"; }
i want to add the delete hyperlink to delete the particular records by id.
i tried using the following code and i couldnt achieve it.
if(mysql_num_rows($result_select) > 0) {
if(isset($_POST['id'])) {
$query_delete = "DELETE FROM users WHERE id =" .$_POST['id'];
$result_delete = mysql_query($query_delete);
}
echo "<table cellpadding=10 border=1>";
while($row = mysql_fetch_assoc($result_select)) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['pass'] . "</td>";
echo "<td>Delete</td>";
echo "</tr>";
}
echo "</table>";
}
I am a newbie to programming, i would appreciate if someone explain me in simple words.. thank you :)
First of all put your delete code in your page like this:
if(isset($_POST['submit'])) {
for($i = 0; $i < count($_POST['del']); $i++)
{
// check which records to delete
if (isset($_POST['del'][$i]))
{
$query_delete = "DELETE FROM users WHERE id = " . (int) $_POST['del'][$i];
$result_delete = mysql_query($query_delete) or die(mysql_error());
}
}
echo 'Record Deleted !!' . '<br /><br />';
}
Later put your select code and modify it like this:
echo '<form action="" method="POST">';
while($row = mysql_fetch_assoc($result_select)) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['pass'] . "</td>";
echo "<td><input type=\"checkbox\" name=\"del[]\"></td>";
echo "</tr>";
}
echo '<input type="submit" name="submit">';
echo '</form>';
You need to change $_POST to $_GET, so this should work.
if(isset($_GET['id'])) {
$query_delete = "DELETE FROM users WHERE id =" .$_GET['id'];
$result_delete = mysql_query($query_delete);
and also put values of attributes inside double quotes, like this
echo '<td>Delete</td>';

Categories