php fetch column values from database and display in html table column - php

I have a table in database name Accounts in which i have many rows and many columns, i want to show a column Account (all values) in my html table. I have tried many method to show a specific column values without using index in html using php and I am using MySql.
$storeArray = Array();
while($rowval = mysql_fetch_array($whole, MYSQL_ASSOC))
{
$storeArray[] = $rowval['Account'];
$status= $rowval['status'];
$ph1= $rowval['Phone1'];
$ph2= $rowval['Phone2'];
}
by using <?php echo $storeArray[0]; ?> and <?php echo $storeArray[1]; ?> in <td> i got the solution. My question is there any way, it automatically show all values without providing any index?

$conn=new mysqli("localhost","root","","your_db");
$rows=$conn->query("select username from User");
echo "<table border='1'>";
echo "<tr><th>Username</th></tr>";
while(list($username)=$rows->fetch_row()){
echo "<tr><td>$username</td></tr>";
}
echo "</table>";

This is a pretty elaborate question. I think the best I can do is point you in the right direction. There is a good tutorial for this on w3schools.com. You should at least read these:
PHP - Connect to MySQL
PHP - Select Data From MySQL
and maybe
PHP - Limit Data Selections From MySQL

**file user.php**
<?php
$conn=new mysqli("localhost","root","","your_db");
$rows=$conn->query("select id,username from User");
echo "<table border='1'>";
echo "<tr><th>Username</th></tr>";
while(list($id,$username)=$rows->fetch_row()){
echo "<tr>";
echo "<td>";
echo "<form action='user_details.php' target='_blank' method='post'>";
echo "<input type='hidden' name='txtId' value='$id' />";
echo "$id - $username";
echo "<input type='submit' name='btnView' value='View' />";
echo "</form>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
?>
**file user_details.php**
<?php
if(isset($_POST["btnView"])){
$id=$_POST["txtId"];
$conn=new mysqli("localhost","root","","your_db");
$row=$conn->query("select id,username,email,phone from User where id='$id'");
list($id,$username,$email,$phone)=$row->fetch_row();
echo $id," ",$username," ",$email," ",$phone;
}
?>

if you want to fetch more than one database column in your html table column.
$conn=new mysqli("localhost","root","","your_db");
$rows=$conn->query("select col1,col2 from Tablename");
echo "<table border='1'>";
echo "<tr><th>col1</th><th>col2</th></tr>";
while(list($col1, $col2)=$rows->fetch_row())
{
echo "<tr><td>$col1</td><td>$col2</tr>";
}
echo "</table>";

Related

Why my jquery data table is not working when I add a table data with a link?

echo "<table id='table_id' class='display table table-hover table-inverse'>";
echo "</thead>";
echo "<tbody>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['last_update_time'] . "</td>";
When I add this row with a delete link, data table is not being applied to my table.
echo "<td>Delete</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
Without the particular line all works fine.
Please help.
TIA
can you please confirm that in your code, number of columns inside 'thead' are equal to number of columns inside 'tbody'. It should not mismatch the number of columns of both 'thead' and 'tbody'

php/sql : don't echo show data correctly

i have a problem with my php and sql code. I trying to make code that when i enter some data that related with the database, the output should show 'zero' if the row user_designid equal to 0 and if vice versa it will show 'one'.
but the problem is both of output only show the first anwser ('zero').
Php file (co.php is my config)
<?php
require "co.php";
$link = mysqli_connect($h,$u,$p,$db);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$q = "select * from tab_ss_user where User_name ='".$_POST['name']."'";
$result = mysqli_query($link,$q);
if($result){
echo "<table border='1' >
<tr>
<td align=center> <b>user Id No</b></td>
<td align=center><b>Name</b></td>
<td align=center><b>Password</b></td>
<td align=center><b>responsibility</b></td></td>";
while($data = mysqli_fetch_row($result))
{
echo "<tr>";
echo "<td align=center><option>$data[0]</option></td>";
echo "<td align=center>$data[1]</td>";
echo "<td align=center>$data[2]</td>";
//this is the problem
if($data['User_DesignID'] == 0 ){
echo "<td align=center> zero </td>";
}
else{
echo "<td align=center> one </td>";
}
echo "</tr>";
}
echo "</table>";
}
else{
echo ' Failed';
}
?>
that all thank you.
You are not referencing the field correctly See enter link description here
When using mysqli_fetch_row, the result is an associative array so $data['User_DesignID'] will not be referenced correctly.
please make sure your column name is "User_DesignID"
if($data['User_DesignID'] == 0 ){
(Posted on behalf of the OP).
Thanks to Jeffrey idea, I have found an answer (please refer to Jeffrey's answer on this page).
And also if you want to use column name instead please use mysqli_fetch_assoc instead of row.

PHP & SQL - Update query fails to update value

I'm trying to create a very easy stock managing system. I'm able to show all the items in my table 'parts' and i'm showing the amount in a textbox. However, when i change the value from, for example, 0 to 5 in the textbox and i press my submit button, it doesn't update the stock.
Below is my code, i don't have alot of experience with update querys but i've read about it on php.net, obviously.
<?php
echo "<table width=\"800\" class=\"nieuws\">";
$db=mysqli_connect("localhost","root","","lichtwinkel");
$p=mysqli_query($db, "SELECT * FROM parts WHERE product LIKE 1");
echo "<form method='post' action=''>";
echo "<tr><th></th><th>Onderdeel nummer</th><th>Eigenschappen</th><th>Prijs</th><th>Voorraad</th></tr>";
while ($row = mysqli_fetch_array($p)){
echo "<tr>";
echo "<td><img class='lamp' src='../css/images/".trim($row['partnr']).".png' alt='Geen afbeelding beschikbaar'></td>";
echo "<td>".$row['partnr']."</td>";
echo "<td>".$row['specs']."</td>";
echo "<td>€ ".$row['price']."</td>";
echo "<td><input type='text' id='aantal' name='aantal' value=$row[voorraad] /></td>";
echo "<td><input type='submit' id='update' name='update' value='Update' /></td>";
echo "</tr>";
}
echo "</table>";
if(isset($_POST['aantal']) && $_POST['update']) {
$y = $_POST['aantal'];
$p=mysqli_query($db, "UPDATE parts SET voorraad = '$y' WHERE partnr = $row[0]");
}
echo "</form>"
?>
Simply said, what i'm trying to achieve is the following:
Whenever i change the value displayed in the texbox, and i press my submit button, i want it to update the value in the database.
Does anyone know what i'm doing wrong? Any ideas? Articles i should read?
All help would be appreciated.
Thank you.
As i see, you were doing it wrong at all.
First you can't use form tag within more then one td element.
You were didn't close the form tag, only at end. (So if it loops 6 times, you will have 6 forms open, but one ended!).
At update, you're selecting row[0] - it's outside of loop with rows?
Even if you update it, it will show wrong results again. Update should be above selects! So it picks up newly updated value.
What to do:
First make one form for all updates.
Use your submit button to have value DATABASE_ID.
Make the name of "aantal" to "aantalDATABASE_ID".
At submit check for $_POST['update'], and use it's value (DATABASE_ID) to get input $_POST["aantal".$_POST['update']].
Do update, you have all you need.
Example:
<?php
echo "<form method='post' action=''>";
echo "<table width=\"800\" class=\"nieuws\">"
$db=mysqli_connect("localhost","root","","lichtwinkel");
if(isset($_POST['update']) && !empty($_POST['update'])) {
$y = $_POST['aantal'.$_POST['update']];
$p=mysqli_query($db, "UPDATE parts SET voorraad = '".$y."' WHERE partnr = '".$_POST['update']."'");
}
$p=mysqli_query($db, "SELECT * FROM parts WHERE product LIKE 1");
echo "<tr><th></th><th>Onderdeel nummer</th><th>Eigenschappen</th><th>Prijs</th><th>Voorraad</th></tr>";
while ($row = mysqli_fetch_array($p)){
echo "<tr>";
echo "<td><img class='lamp' src='../css/images/".trim($row['partnr']).".png' alt='Geen afbeelding beschikbaar'></td>";
echo "<td>".$row['partnr']."</td>";
echo "<td>".$row['specs']."</td>";
echo "<td>€ ".$row['price']."</td>";
echo "<td><input type='text' id='aantal' name='aantal".$row[0]."' value='".$row[voorraad]."' /></td>";
echo "<td><input type='submit' id='update' name='update' value='".$row[0]."' /></td>";
echo "</tr>";
}
echo "</table>";
echo '</form>';
?>
After all, take care about SQL Injections. "aantal" value is user input. As the submit value can be changed.

returning data from the database

The query is to choose the departure and the destination's catered for coaches and result in any combination for the data selected
This query is in the file: getPullman.php
include '../Includes/config.php';
$nomeArrivo=$_POST['nomeA'];
$nomePartenza=$_POST['nomeP'];
$sql="SELECT codl,fermatap,fermataa,ora
FROM partenza,arrivo,orari,
WHERE idp=codp,
AND codo=ido,
AND fermatap='$nomePartenza',
AND fermataa='$nomeArrivo'";
$result=$conn->query($sql);
echo "<table>";
while($row=$result->fetch_assoc()){
echo "<tr>";
echo "<td>".$row['codl']."</td>";
echo "<td>". $row['fermatap']."</td>";
echo "<td>".$row['fermataa']."</td>";
echo "<td>".$row['ora']."</td>";
echo "</tr>";
}
echo"</table>";
$conn->close();
Just write below like of code where you want result of this query.
include_once('getPullman.php');
Or you can check file path of "getPullman.php" and can write whole path.

MSSQL and PHP: Select specific row based on which button is clicked

I am making a website for a mock book database using MSSQL where users can search for different books and select particular books that they might like to add to a list of favorites under their account name. The problem I am having is that I have no idea how to differentiate which book selection they want to add to their favorites because I can't figure out how to set the ISBN of the book, which uniquely identifies it, to a php session variable. If anyone can shed some light on this I would appreciate it, have been trying to figure it out all day.
//Set up connection
$connection = mssql_connect("$hostName", "$sqlUsername", "$sqlPassword")
or die("ERROR: selecting database server failed.");
//Select database
mssql_select_db($databaseName, $connection)
or die("ERROR: Selecting database failed");
//Search to run if searching for book title
if(isset($_GET['searchBook'])){
$searchBook = $_GET['searchBook'];
$query = "SELECT BOOK.ISBN, Title, Author, Publisher, NumberOfPages, Language, LocationName, ListPrice FROM BOOK, PRICE, LOCATION WHERE Title LIKE '%$searchBook%' AND BOOK.ISBN = PRICE.ISBN AND PRICE.LocationID = LOCATION.LocationID";
}
//Search to run is searching for a book author
if(isset($_GET['searchAuthor'])){
$searchAuthor = $_GET['searchAuthor'];
$query = "SELECT BOOK.ISBN, Title, Author, Genre, Publisher, NumberOfPages, Language, LocationName, ListPrice FROM BOOK, PRICE, LOCATION WHERE Author LIKE '%$searchAuthor%' AND BOOK.ISBN = PRICE.ISBN AND PRICE.LocationID = LOCATION.LocationID";
}
//Store query result
$query_result = mssql_query($query, $connection)
or die( "ERROR: Query is wrong");
//Set up table to display search results
echo "<form action=\"addFavorite.php\" method=\"POST\" name=\"table\">";
echo "<table border=1 align=\"center\">";
echo "<tr>";
// fetch attribute names
while ($filed = mssql_fetch_field($query_result)) {
echo "<th>".$filed->name."</th>";
}
echo "<th>Favorite</th>";
echo "</tr>";
// fetch table records
while ($line = mssql_fetch_row($query_result)) {
echo "<tr>\n";
foreach ($line as $eachline) {
echo "<td> $eachline </td>";
}
echo "<td><input name=\"".$line['index']."\" type=\"submit\" value=\"Add To Favorites\"></td>";
echo "</tr>\n";
}
echo "</table>";
echo "</form>";
Not sure if this is relevant but the following code is my best attempt at getting the value of ISBN that corresponds to the row of the button being clicked, which doesn't exactly work like I had hope.
//Get the ISBN
$data = mssql_fetch_assoc($query_result);
$ISBN = $data['ISBN'];
echo $ISBN;
Here is the code for my addFavorite.php which is where the form action is set to. This is the file that needs to know what user is adding a book as a favorite AND what book they are adding to that list.
//Set up connection
$connection = mssql_connect("$hostName", "$sqlUsername", "$sqlPassword")
or die("ERROR: selecting database server failed.");
//Select database
mssql_select_db($databaseName, $connection)
or die("ERROR: Selecting database failed");
$User = $_SESSION['userID'];
//Set up query
$query = "INSERT INTO FAVORITES VALUES(\"$User\",\"**I NEED A SESSION VARIABLE OR SOMETHING TO GO HERE\")";
//Store query result
$query_result = mssql_query($query, $connection)
//or die( "ERROR: Query is wrong");
Any help would be much appreciated. I know it's alot of information and if there is anything that doesn't make sense or I have forgotten to provide please let me know. Thanks.
EDIT
I have tried using the BUTTON instead of using INPUT but the value of the button is not setting to anything for some reason.
echo "<form action=\"addFavorite.php\" method=\"POST\" name=\"table\">";
echo "<table border=1 align=\"center\">";
echo "<tr>";
// fetch attribute names
while ($filed = mssql_fetch_field($query_result)) {
echo "<th>".$filed->name."</th>";
}
echo "<th>Favorite</th>";
echo "</tr>";
// fetch table records **PROBLEM IN HERE since $line['ISBN'] returns nothing**
while ($line = mssql_fetch_row($query_result)) {
echo "<tr>\n";
foreach ($line as $eachline) {
echo "<td> $eachline </td>";
}
echo "<td><button name=\"FavoriteButton\" type=\"submit\" value=\"".$line['ISBN']."\">Add To Favorites</button></td>";
echo "</tr>\n";
}
echo "</table>";
echo "</form>";
EDIT 2
Finally got it working, thanks to everyone for helping! Partial code that was problematic posted below in working condition.
echo "<form action=\"addFavorite.php\" method=\"POST\" name=\"table\">";
echo "<table border=1 align=\"center\">";
echo "<tr>";
// fetch attribute names
while ($filed = mssql_fetch_field($query_result)) {
echo "<th>".$filed->name."</th>";
}
echo "<th>Favorite</th>";
echo "</tr>";
// fetch table records
while ($line = mssql_fetch_row($query_result)) {
echo "<tr>\n";
foreach ($line as $eachline) {
echo "<td> $eachline </td>";
}
echo "<td><button name=\"FavoriteButton\" type=\"submit\" value=\"".$line[0]."\">Add To Favorites</button></td>";
echo "</tr>\n";
}
echo "</table>";
echo "</form>";
Use a BUTTON-element instead of the INPUT-element. That way, you can use the 'value'-attribute of this element to pass the correct value.
echo "<td><button name=\"$line['index']\" value=\"$line['ISBN']\" type=\"submit\">Add to favorites</button></td>";
Although I would suggest using AJAX instead of the above approach for this: use the onclick event from a button to execute javascript that calls a seperate php-file and passes the correct ISBN-number. This is then added to the database and your original page should be refreshed or part of the page reloaded.

Categories