How to search an HTML table that collects data from Mysqli [duplicate] - php

This question already has answers here:
How can I get an unknown username given an ID?
(2 answers)
Closed 1 year ago.
trying to do a search on an html table want to be able to type the users nickname and it gives me all the users details on the same table.
<table width="100%" class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<th>Nickname</th>
<th>State</th>
<th>city</th>
<th>address</th>
</tr>
</thead>
<tbody>
<?php
$sql = "SELECT * FROM houses";
$q=$conn->query($sql);
while ($row = mysqli_fetch_array($q)) {
?>
<tr>
<td><?php echo $row['nickname']; ?></td>
<td><?php echo $row['state']; ?></td>
<td><?php echo $row['city']; ?></td>
<td><?php echo $row['address']; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>

You need to use WHERE in your SQL query:
$sql = "SELECT * FROM houses WHERE nickname = '{$nickname}'";
you also need to use an isset in order to have the word from your HTML input.

Related

How to fix a table causing "502: Bad Gateway" error in PHP

I am creating a simple database that can do basic CRUD operations (create, read, update, delete) using php. I am able to complete the create, and able to see the results if I directly query the mySQL DB in the back end. But, I am having trouble getting the table to display on the webpage. It is instead displaying a "Bad Gateway" error if I attempt to display the database entries.
I tried removing the reference to the table, specifically
<?php while ($row = mysqli_fetch_array($results)) { ?>
<tr>
<td><?php echo $row['name']; ?></td>...
and the web page on front end works fine. Albeit can only see the data if I query the backend.
<?php include('php_code.php'); ?>
...
...
...
<?php $results = mysqli_query($db, "SELECT * FROM info"); ?>
<table>
<thead>
<tr>
<th>Name</th>
<th>Address</th>
<th>City</th>
<th colspan="2">Action</th>
</tr>
</thead>
<?php while ($row = mysqli_fetch_array($results)) { ?>
<tr>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['address']; ?></td>
<td><?php echo $row['city']; ?></td>
<td>
<a href="test.php?edit=<?php echo $row['id']; ?>"
class="edit_btn" >Edit</a>
</td>
<td>
<a href="php_code.php?del=<?php echo $row['id']; ?>"
class="del_btn">Delete</a>
</td>
</tr>
<?php } ?>
</table>
<!--in php_code.php-->
//to retrieve records
$select_query = "SELECT * FROM info";
$result = mysqli_query($db, $select_query);
I should be able to see the table with data containing name, address and city. But I am getting a 502 error instead.
Try this
<?php
include('php_code.php');
$results = mysqli_query($db, "SELECT * FROM `info` ");
$return = <<<HTML
<table>
<thead>
<tr>
<th>Name</th>
<th>Address</th>
<th>City</th>
<th colspan="2">Action</th>
</tr>
</thead>
<tbody>
HTML;
while ($row = mysqli_fetch_array($results)) {
$return .= <<<HTML
<tr>
<td>{$row['name']}</td>
<td>{$row['address']}</td>
<td>{$row['city']}</td>
<td><a href="test.php?edit={$row['id']}" class="edit_btn" >Edit</a></td>
<td>Delete</td>
</tr>
HTML;
}
$return .= <<<HTML
</tbody>
</table>
HTML;
echo $return;
?>
your php_code.php should really only have the database config...
you are closing the php statements so you cannot retrieve the result of your query. Don't split php parts and just echo html like this
<?php
echo " <table>
<thead>
<tr>
<th>Name</th>
<th>Address</th>
<th>City</th>
<th colspan='2'>Action</th>
</tr>
</thead> ";
while ($row = mysqli_fetch_array($results)) {
echo "<tr>
<td>$row['name']</td>
<td>$row['address']</td>
<td> $row['city']</td>
<td>
<a href='test.php?edit=$row['id']'
class='edit_btn' >Edit</a>
</td>
<td>
<a href='php_code.php?del=$row['id']'
class='del_btn'>Delete</a>
</td>
</tr>";
}
echo "</table>";
?>

Hide a column when fetch the all data in PHP

I want to list all users data from the database in PHP. there will be a user is logged-in at a single time. I want un-fetch or hide a single row who has active(logged-in). so, I'm asking you how can I do this...
$query=$conn->pdo->exec("SELECT * FROM usertable");
$row=$query->fetch();
<thead>
<tr>
<th>User Name</th>
<th>Name</th>
<th>Email</th>
<th>Role</th>
<th>Post</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php while($row=$query->fetch()){ ?>
<tr>
<td><?php echo $row['username']; ?>
</td>
<td><?php echo ucfirst($row['name']); ?>
</td>
<td><?php echo $row['username']; ?></td>
<td><?php echo ucfirst($row['user_type']); ?></td>
<td><?php echo $row['id']; ?></td>
</tr>
</tbody>
You can add new field in your usertable like logged with default value 0.when user login you should change 0 to 1.Now you change your sql query **select * from usertable where logged='0';**

Delete query not working to delete stock

i am implementing clothing shopping website in which admin manages stock for example add, update, delete stock. i am having problem with my delete query. stock is displayed in table and every row has its own delete button. when i click on delete button it always takes last row id and delete that and not that row which i want. Query is taking always last row id.
CODE:
<form action="" method="post" enctype="multipart/form-data" name="deleting" >
<table align="center" border="0" id="myTable" class="table table-striped table-bordered table-list">
<tr>
<th>Product Code</th>
<th>Brand</th>
<th>Price</th>
<th>Gender</th>
<th>Category</th>
<th>Material</th>
<th>Size</th>
<th>Description</th>
<th>Quantity</th>
<th>Delete Stock</th>
</tr>
<?php
$sql = "SELECT * FROM add_stock ORDER BY id DESC";
$rs_result = mysqli_query ($sql);
while ($result=mysqli_fetch_array($rs_result) )
{
?>
<tr>
<td><?php echo $result['id'];?></td>
<td><?php echo $result['brand_name'];?></td>
<td><?php echo $result['price'];?></td>
<td><?php echo $result['gender_name'];?></td>
<td><?php echo $result['category_name'];?></td>
<td><?php echo $result['material_name'];?></td>
<td><?php echo $result['size_name']; ?></td>
<td><?php echo $result['dress_description'];?></td>
<td><?php echo $result['dress_quantity'];?></td>
<td><input type="hidden" name="ID" value="<?php echo $result['id']; ?>"><input type="submit" name="delete" value="Delete" ></td>
</tr>
<?php
}
?>
</table>
</form>
<?php
if (isset($_POST['delete'])) {
$id=$_POST['ID']; //problem is here: it always takes last row id
$link=mysqli_connect("localhost","root","") or die("Cannot Connect to the database!");
mysqli_select_db("login",$link) or die ("Cannot select the database!");
$query="DELETE FROM add_stock WHERE id='".$id."'";
$result=mysqli_query($query,$link) or die(mysqli_error($link));
if($result)
{
echo '<script>confirm("Are you sure want to delete this record?")</script>';
echo '<script>alert("Record ".$id." removed successfully!")</script>';
}
else
{
die ("An unexpected error occured while <b>deleting</b> the record, Please try again!");
}
}
?>
I would rather suggest not to use <form> just for deleting purpose. You can use <a> tag to redirect it to other page for deleting purpose.
Here is my code.
index.php
<table align="center" border="0" id="myTable" class="table table-striped table-bordered table-list">
<tr>
<th>Product Code</th>
<th>Brand</th>
<th>Price</th>
<th>Gender</th>
<th>Category</th>
<th>Material</th>
<th>Size</th>
<th>Description</th>
<th>Quantity</th>
<th>Delete Stock</th>
</tr>
<?php
$sql = "SELECT * FROM add_stock ORDER BY id DESC";
$rs_result = mysqli_query($sql);
while ($result = mysqli_fetch_array($rs_result)) {?>
<tr>
<td><?php echo $result['id']; ?></td>
<td><?php echo $result['brand_name']; ?></td>
<td><?php echo $result['price']; ?></td>
<td><?php echo $result['gender_name']; ?></td>
<td><?php echo $result['category_name']; ?></td>
<td><?php echo $result['material_name']; ?></td>
<td><?php echo $result['size_name']; ?></td>
<td><?php echo $result['dress_description']; ?></td>
<td><?php echo $result['dress_quantity']; ?></td>
<td>
<a href="deleteStock.php?id=<?php echo $result['id'];?>">
<input type="button" value="Delete" >
</a>
</td>
</tr>
<?php }?>
</table>
<?php
if(isset($_GET['delete'])){
if($_GET['delete'] == "success"){
echo '<script>alert("Record removed successfully!")</script>';
}
if($_GET['delete'] == "fail"){
echo '<script>alert("An unexpected error occured while <b>deleting</b> the record, Please try again!")</script>';
}
}
?>
<script>
$(document).on('click', "#myTable a", function(e) {
if(confirm("Are you sure want to delete this record?")) {
return true;
} else {
return false;
}
});
</script>
deleteStock.php
<?php
if (isset($_GET['id'])) {
$id = $_GET['id'];
$link = mysqli_connect("localhost", "root", "") or die("Cannot Connect to the database!");
mysqli_select_db("login", $link) or die("Cannot select the database!");
$query = "DELETE FROM add_stock WHERE id = $id";
$result = mysqli_query($query, $link) or die(mysqli_error($link));
if($result){
header("location:index.php?delete=success");
} else {
header("location:index.php?delete=fail");
}
}?>
[Important: And, still you have not created separate file for DB Connection, which I have already mentioned in my answer of your question modal popup keep populating only first item data on all item buttons 1 Week before. It implies, you don't learn from your mistake or you don't need any suggestions.]
Your script taking last row in post always because you have single form for all your data and your last value will be overwrite all previous data. Intead of it remove form from out side of table add add it to td where you have specify hidden field and delete button. Like below:
<table align="center" border="0" id="myTable" class="table table-striped table-bordered table-list">
<tr>
<th>Product Code</th>
<th>Brand</th>
<th>Price</th>
<th>Gender</th>
<th>Category</th>
<th>Material</th>
<th>Size</th>
<th>Description</th>
<th>Quantity</th>
<th>Delete Stock</th>
</tr>
<?php
$sql = "SELECT * FROM add_stock ORDER BY id DESC";
$rs_result = mysqli_query ($sql);
while ($result=mysqli_fetch_array($rs_result) )
{
?>
<tr>
<td><?php echo $result['id'];?></td>
<td><?php echo $result['brand_name'];?></td>
<td><?php echo $result['price'];?></td>
<td><?php echo $result['gender_name'];?></td>
<td><?php echo $result['category_name'];?></td>
<td><?php echo $result['material_name'];?></td>
<td><?php echo $result['size_name']; ?></td>
<td><?php echo $result['dress_description'];?></td>
<td><?php echo $result['dress_quantity'];?></td>
<td><form action="" method="post" name="deleting" ><input type="hidden" name="ID" value="<?php echo $result['id']; ?>"><input type="submit" name="delete" value="Delete" ></form></td>
</tr>
<?php
}
?>
</table>
Also Remove enctype="multipart/form-data" from form its needed only if you have to upload file

Dynamicaly create table based on the column value

I want to create another table if the value of Semester and School Year is different from the current table.
From the picture below, There was a value of "2 semester" and "school year 2014" basically what i want is each semester and school year has an own table. I don't want to code multiple table because i'm not sure what is the current school year and semester.
here is my code
<div class="table-responsive">
<table class="table table-bordered">
<thead>
<tr>
<th>Subject Code</th>
<th>Decription</th>
<th>Grade</th>
<th>Units</th>
<th>Semester</th>
<th>School Year</th>
</tr>
</thead>
<tbody>
<?php
$sql ="SELECT * FROM grades WHERE stud_no ='$stud_no'";
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($result)){
?>
<tr>
<td><?php echo $row['subj_cd'];?></td>
<td><?php echo ucwords(strtolower($row['subj_descr']));?></td>
<td><?php echo $row['final_grade'];?></td>
<td><?php echo $row['units_lec'] + $row['units_lab'];?></td>
<td><?php echo $row['semester'];?></td>
<td><?php echo $row['sch_year'];?></td>
</tr>
</tbody>
<?php
}
?>
</table>
</div>
Try the code below. Hope it is helps. :)
<?php
//Order year asc then semester asc
$sql ="SELECT * FROM grades WHERE stud_no ='$stud_no' ORDER BY sch_year, semester";
$result = mysqli_query($con, $sql);
// Init 2 empty variable for sem and year
$prev_year = $prev_sem = '';
while($row = mysqli_fetch_array($result)){
?>
<tr>
<td><?php echo $row['subj_cd'];?></td>
<td><?php echo ucwords(strtolower($row['subj_descr']));?></td>
<td><?php echo $row['final_grade'];?></td>
<td><?php echo $row['units_lec'] + $row['units_lab'];?></td>
<td><?php echo $row['semester'];?></td>
<td><?php echo $row['sch_year'];?></td>
</tr>
<?php
// Check is $prev_sem is empty or not. If empty, assign it. As $prev_sem and $prev_year will be set at the same time, it can skip to check $prev_year
if (!empty($prev_sem)) {
$prev_sem = $row['semester'];
$prev_year = $row['sch_year'];
}
// Check if previous sem and year is not same, print the HTML code below.
if ($prev_sem != $row['semester'] || $prev_year != $row['sch_year']) {
print '
</tbody>
</table>
</div>
<div class="table-responsive">
<table class="table table-bordered">
<thead>
<tr>
<th>Subject Code</th>
<th>Decription</th>
<th>Grade</th>
<th>Units</th>
<th>Semester</th>
<th>School Year</th>
</tr>
</thead>
<tbody>
';
}
}
?>
</tbody>

Created a table with 4 columns and 3 rows. How do I set up a php script to pull each row by id to generate 3 tr from the dababase?

The table I am looking to pull from my database is com/bzkItsK. It currently pulls the first row in the database but I am unsure about how to set up a script that will pull all the rows (currently 4) by their id to the webpage.
Here is the html as I have set it:
<table class="table table-striped">
<thead>
<tr>
<th>user id</th>
<th>First Name</th>
<th>Last Name</th>
<th>Department</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row"><?php echo $rows[$user_id];?></th>
<td><?php echo $rows[$first_name];?></td>
<td><?php echo $rows[$last_name];?></td>
<td><?php echo $rows[$dept];?></td>
</tr>
</tbody>
</table>
mysql_query is as such.
mysql_connect("localhost","?","?");
mysql_select_db("?");
$sql = mysql_query("SELECT * FROM users ORDER BY id ASC");
$id = 'id';
$user_id = 'user_id';
$first_name = 'first_name';
$last_name = 'last_name';
$dept = 'dept';
$rows = mysql_fetch_assoc($sql);
?>
I am trying to pull all 4 rows by id to be auto generated by a single table script.
you must iterate over your results.
$rows = mysql_fetch_assoc($sql);
will only fetch the first entry.
You have do something like this:
<?php while($rows = mysql_fetch_assoc($sql)): ?>
<tr>
<th scope="row"><?php echo $rows[$user_id];?></th>
<td><?php echo $rows[$first_name];?></td>
<td><?php echo $rows[$last_name];?></td>
<td><?php echo $rows[$dept];?></td>
</tr>
<?php endwhile; ?>
EDIT:
And please do not use mysql, use mysqli instead.

Categories