*Below is my code
When I click search, it only display one result from my database.
Is there other method to solve this problem?
Or how to save multiple variables in the session and then display it?
<?php
session_start();
?>
<?php
include("htmltable.php");
?>
<table align="center">
<table class="data-table">
<caption class="title">Donor Lists</caption>
<thead>
<tr>
<th>DonorID</th>
<th>Donor</th>
<th>gender</th>
<th>bloodgroup</th>
<th>State</th>
</tr>
</thead>
<tbody>
<td><?php echo("{$_SESSION['DonorID']}"."<br />");?></td>
<td><?php echo("{$_SESSION['FullName']}"."<br />");?></td>
<td><?php echo("{$_SESSION['gender']}"."<br />");?></td>
<td><?php echo("{$_SESSION['bloodgroup']}"."<br />");?></td>
<td><?php echo("{$_SESSION['State']}"."<br />");?></td>
</tbody>
</table>
</table>
This is the process, when user search, it will select from the database and then display it at the search donor page.
<?php
session_start();
include "dbh.inc.php";
if (isset($_POST['submit']))
{
$sql="SELECT * FROM donorregistration WHERE (bloodgroup='{$_POST["bloodgroup"]}' AND State='{$_POST["State"]}')";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_assoc($result))
{
$_SESSION['DonorID'] = $row['DonorID'];
$_SESSION['FullName'] = $row['FullName'];
$_SESSION['gender'] = $row['gender'];
$_SESSION['bloodgroup'] = $row['bloodgroup'];
$_SESSION['State'] = $row['State'];
header("Location: ../assignbloodbank/Searchdonor.php?search=success");
exit();
}
}
else
{
echo "<div class='alert alert-danger'><i class='fa fa-users'></i> No Donors Found</div>";
}
}
?>
When you use header("Location") inside of a while loop only the first redirect will work. And when you use exit() no further code will execute. Also, there's no need to set $_SESSION for every row (as that will overwrite the variable too, unless you use an array for each session value with $_SESSION['key'][]).
The easiest approach would be to simply craft your <table> inside of your if statement, and craft your table rows inside of your while loop:
<?php
if (mysqli_num_rows($result) > 0)
{
?>
<table>
<thead>
<tr>
<th>DonorID</th>
<th>Donor</th>
<th>gender</th>
<th>bloodgroup</th>
<th>State</th>
</tr>
</thead>
<tbody>
<?php
while($row = mysqli_fetch_assoc($result))
{
echo "<td>" . $row['DonorID'] . "</td>";
echo "<td>" . $row['FullName'] . "</td>";
echo "<td>" . $row['gender'] . "</td>";
echo "<td>" . $row['bloodgroup'] . "</td>";
echo "<td>" . $row['State'] . "</td>";
}
?>
</tbody>
</table>
<?php
}
?>
This way, the table itself only gets generated if there are rows returned from the database, meaning you'll never end up with a table with nothing in it.
You need to add new array in existing $_SESSION element rather than override the previous $_SESSION. Add this code in you while loop. if you want to print multiple records then you have to use foreach loop
$_SESSION['DonorID'][] = $row['DonorID'];
$_SESSION['FullName'][] = $row['FullName'];
$_SESSION['gender'][] = $row['gender'];
$_SESSION['bloodgroup'][] = $row['bloodgroup'];
$_SESSION['State'][] = $row['State'];
For printing multiple records use foreach loop
foreach ( $_SESSION as $row ) {
echo "<td>" . $row['DonorID'] . "</td>";
echo "<td>" . $row['FullName'] . "</td>";
echo "<td>" . $row['gender'] . "</td>";
echo "<td>" . $row['bloodgroup'] . "</td>";
echo "<td>" . $row['State'] . "</td>";
}
EDIT: The problem is due to you never place the session variable with array, hence your output from sql actually kept repeating overwrite inside the variable, that the reason why you able to acquire one result only.
What you can do is declare your session as array before store the data.
$_SESSION['DonorID'] = array();
$_SESSION['FullName'] = array();
$_SESSION['gender'] = array();
$_SESSION['bloodgroup'] = array();
$_SESSION['State'] = array();
Then retrieve your data using index. Make sure add your for new rows.
$counter=0;
$loopcounter=count($_SESSION['DonorID']);
while($counter < $loopcounter)
{
<tr><td><?php echo("{$_SESSION['DonorID'][$counter]}"."<br />");?></td>
.....</td></tr>
$counter++;
}
Reference: store mutiple values in php session
Related
I've been struggling for weeks just to analyze the problem of my code, the JQuery Pagination table didn't show the PHP value. I thought it would be PHP that causes the problem.
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>No.</th>
<th>Nama Mitra</th>
<th>Jenis Kelamin</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$i = 1;
//I think the problem starts here
while ($rowmitra = mysqli_fetch_array($mitra)) {
echo '<tr>';
echo "<th scope='row'>" . $i . "</th>";
echo "<td>" . $rowmitra['id_mitra'] . "</td>";
echo "<td>" . $i . "</td>";
echo "<td>" . $rowmitra['mitra'] . "</td>";
echo '<td><img width="200px" class="shadow-sm" src="image/logo/' . $rowmitra["logo"] . '"></td>';
echo '<td><button>EDIT</button></td>';
echo "</tr>";
$i = $i + 1;
}
?>
</tbody>
</table>
And here's what I do to fetch Mitra table
$mitra = $koneksi->query("SELECT * FROM `mitra`");
$rowmitra = mysqli_fetch_array($mitra);
I put it in a different file that I use include 'head.php'; command to merge both. When I fetch it in outside while command it works.
It didn't fetch an array of my PHP when I put it in while. Am I clumsy enough not to know where is the problem exactly? I've tried to match the variable and also search for alternative ways to fetch array. But it didn't work.
Thank you for your help.
You should not declare the result fetch array outside while loop. Remove this $rowmitra = mysqli_fetch_array($mitra); and try with mysqli_fetch_assoc to get database column name.
<?php
$mitra = $koneksi->query("SELECT * FROM `mitra`");
$i = 1;
//I think the problem starts here
if (mysqli_num_rows($mitra) > 0) {
while ($rowmitra = mysqli_fetch_assoc($mitra)) {
echo '<tr>';
echo "<th scope='row'>".$i."</th>";
echo "<td>".$rowmitra['id_mitra']."</td>";
echo "<td>".$i."</td>";
echo "<td>".$rowmitra['mitra']."</td>";
echo '<td><img width="200px" class="shadow-sm" src="image/logo/'.$rowmitra["logo"].'"></td>';
echo '<td><button>EDIT</button></td>';
echo "</tr>";
$i = $i + 1;
}
}
?>
I have echoed my database table onto my website and have tick option next to the table where you can tick it and delete any of the entries from the website, it should delete it from the database. For some reason its not working, I'm a beginner and any help would be greatly appreciated thanks.
<form method="" action="tester.php">
<?php
include 'connect_to_mysql.php';
$count=mysql_num_rows($result);
$result = mysql_query("SELECT * FROM booking ORDER BY ID ASC");
echo "<table border='1'>
<tr>
<th>DEL</th>
<th>Name</th>
<th>Email ID</th>
<th>Phone Number</th>
<th>Collection Address</th>
<th>Collection Date</th>
<th>Collection Time</th>
<th>Make & Model</th>
<th>Message</th>
</tr>";
while($row = mysql_fetch_array($result))
{
?>
<td align="center" bgcolor="#FFFFFF"><input name="delete_these[]" type="checkbox" id="checkbox[]" value="<?php echo $row['ID']; ?>"></td>
<?php
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['phonenumber'] . "</td>";
echo "<td>" . $row['collectionaddress'] . "</td>";
echo "<td>" . $row['collectiondate'] . "</td>";
echo "<td>" . $row['collectiontime'] . "</td>";
echo "<td>" . $row['makemodel'] . "</td>";
echo "<td>" . $row['message'] . "</td>";
echo "</tr>";
}
echo "</table>";
?> <br>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>
<?php
// Check if delete button active, start this
if(isset($_GET['delete'])) {
for($i=0;$i<$count;$i++){
$id = $checkbox[$i];
print_r($_GET['delete_these']);
$ids = implode(', ', $_POST['delete_these']);
$sql = "DELETE FROM booking WHERE id IN($ids)";
echo "<br />SQL: $sql<br />";
$result = mysql_query($sql);
}
// if successful redirect to delete_multiple.php
if($result){
}
}
mysql_close();
?>
</form>
you have add method in your form tag--
<form method="post" action="tester.php" >
and also replace if(isset($_GET['delete'])) to if(isset($_POST['delete']))
Replace your form tag by:
<form method="post" action="tester.php">
then these 3:
if(isset($_POST['delete']))
$id =(int)$_POST['delete_these'][$i];
$sql = "DELETE FROM booking WHERE id=$id";
at the place of these 3:
if(isset($_GET['delete']))
$id = $checkbox[$i];
$sql = "DELETE FROM booking WHERE id IN($ids)";
Your $id = $checkbox[$i]; works only if register_globals directive is on, which is now deprecated (and removed as of PHP 5.4) because of security issues it causes, and if it exists in the version of PHP you are using most hosting services turn it off.
You'll be sending the data through post so you access them through $_POST. the (int) before the $_POST is to cast anything to integer so if the user try to send a string it will become 0, this will protect you from sql injection.
SIDE NOTE you should stop using mysql_* functions as they are deprecated. Check out PDO or mysqli
As the title says, and this is not probably a question, more a hint how to do it.
I have table with four fields, id (auto+primary), firstname, lastname, age.
On my index page I select *, then I make the firstname to a link which goes to antoher page which I want to show all data for that record.
For the moment I have to do it manually "select * from " where id="2". (the other page)
How do I make the link to autodetect "the id from that record set" and only display data from that record.
You can see my curreny project here,
http://www.adamskymotorkylare.se/business/
as you can see, when you click the "firstname" it will always display data where id=2 ( paris hilton ), what I want it to do, it when I click "jack" it will select * from where id="1"
Thanks in advance, Jack
"index page"
$result = mysqli_query($con,"SELECT * FROM persons");
echo "<table width=100%>
<tr>
<th>ID</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Gender</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
$id = $row['id'];
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td> <a href='view_more.php?id= . $id .'>" . $row['firstname'] . "</a> </td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['gender'] . "</td>";
echo "</tr>";
}
echo "</table>";
"viewmore page"
$id = $_get['id'];
$result = mysqli_query($con,"SELECT * FROM persons
WHERE id='$id'");
echo "<table width=100%>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Gender</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td> <a href='#'>" . $row['firstname'] . "</a> </td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['gender'] . "</td>";
echo "</tr>";
}
echo "</table>";
Try this, on your page that indexes the users:
while($row = mysqli_fetch_array($result))
{
$id = $row['id'];
$firstName = $row['firstName'];
echo ('' . $firstName . '');
}
This means if someone clicks the first name they will be sent to user_account.php (can replace this obviously) and you will pass in the ID via the URL (user_account.php?id=123).
On the User Account page you want to do the following:
// GET ID FROM THE URL
$id = $_GET['id'];
$result = mysqli_query($con,"SELECT (WHATEVER YOU WANT) FROM (YOUR TABLE) WHERE id = $id");
Notes:
Replace variables and query with the details you need.
I hope that goes some way to helping.
UserFirstName
$id is user record id first column of your table so when you send it to view_more.php page you can get clicked id $_GET['id']; and get user records for that id and link is user name
and $id you get form data base the way you get first name
index.php
<?php
$sql =mysql_query("select * from `table`");
while($row = mysql_fetch_array($sql)){
?>
<?php $id = $row['id']; ?>
<?php echo ucwords($row['Firstname']); ?>
<?php
}
?>
on index.php anchor tag will send the user id to detail.php
detail.php
<?php
$id = $_REQUEST['cid'];
$sql =mysql_query("select * from `table` where id='".$id."'");
while($row = mysql_fetch_array($sql)){
?>
<tr>
<th>First Name:</th>
<th>Last Name:</th>
<th>Age Name:</th>
<th>Gender:</th>
</tr>
<tr>
<td><?=$row['firstname']?></td>
<td><?=$row['lastname']?></td>
<td><?=$row['age']?></td>
<td><?=$row['gender']?></td>
</tr>
<?php
}
?>
on detail.php $id get the user id from index.php. After that id helps in query and while loop which is get and show the user data from database.
Im building a web site that will return results from mysql and display them on a web page.
I have no problem with storing the data in mysql or retrieving the data
and displaying it on the web page.
When the data is returned to the page I want to store it in a table and run a loop so that for each record there will be a number incremented down the side like a list number.
Any help would be appreciated.
I keep running into errors with my code:
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("forloops", $con);
$result = mysql_query("SELECT * FROM userinfo ORDER BY age DESC");
echo "<table border='1'>
<tr>
<th>Position</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>";
$counter = 0;
$position= 0;
$row = mysql_fetch_array($result);
if($counter <=10){
echo "<tr>";
echo "<td>" . $position . "</td>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "</tr>";
$counter++;
}
echo "</table>";
mysql_close($con);
?>
The problem with your current code is that you're only fetching the first row from the query, take a look at how I've down it below:
echo "<table border='1'>
<tr>
<th>Position</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>";
$counter = 0;
$position= 0;
while ($row = mysql_fetch_array($result)) { // This is new code
if($counter <=10){
echo "<tr>";
echo "<td>" . $position . "</td>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "</tr>";
$counter++;
}
} // This is new code
echo "</table>";
mysql_close($con);
?>
If you notice, I've created a loop around the echoing of the table's rows <tr> and cells <td>.
This while loop will go through each row returned from the query.
If you have more then one result from database, you should use:
while ($row = mysql_fetch_array($result)) {
//process one row
}
Using your way, $row is always the same.
And secondly, if i saw right, you dont increase $position variable.
I have looked extensively through the site and have come to the conclusion that I am encountering a problem that many people do, yet none of the answers I have seen seem to work!
Basically, I am trying to populate an HTML table from the data stored in a mySQL table. The data is in a table called "categories". When I load the page, the table headers appear but no table data.
I have written and rewritten my code no less than 4 times - it works fine as an "ul" or when rendered as just plain text, but as soon as I put it into a "table" to seems to stop working!
Here's my code:
<?php
include("includes/dbconnect.php"); //This works fine as tested on the page
$sql = "SELECT * FROM products";
$myData = mysql_query($sql) or die (mysql_error());
$product = mysql_fetch_array($myData);
?>
<html>
<head>
<title></title>
</head>
<body>
//This plain text works
<?php do {
echo $product['title']." <br />";
} while ($product = mysql_fetch_array($myData));
//-----------------------------------------
//Table - doesnt work
echo "<table border=1>
<tr>
<th>Title</th>
<th>Category</th>
<th>Sport</th>
<th>Team</th>
<th>Price</th>
<th>Shipping</th>
</tr>";
while ($product = mysql_fetch_array($myData)) {
echo "<tr>";
echo "<td>".$product['title']."</td>";
echo "<td>" . $product['category'] . "</td>";
echo "<td>" . $product['sport'] . "</td>";
echo "<td>" . $product['team'] . "</td>";
echo "<td>" . $product['price'] . "</td>";
echo "<td>" . $product['shipping'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>
I really am at my wits end, Ive worked my way through countless YouTube tutorials and still for some reason the code wont work. Can anyone help?
You've already run mysql_fetch_array in a do while loop at the top of your code. You aren't able to pull a result row more than once. Instead, push all of the the returned rows on to an array:
$products = array();
while ($product = mysql_fetch_array($myData)) {
$products[] = $product;
}
You can then loop through $products as many times as you'd like to build out your page. So for your table, this would look like:
echo "<table border=1>
<tr>
<th>Title</th>
<th>Category</th>
<th>Sport</th>
<th>Team</th>
<th>Price</th>
<th>Shipping</th>
</tr>";
foreach($products as $item) {
echo "<tr>";
echo "<td>" . $item['title'] . "</td>";
echo "<td>" . $item['category'] . "</td>";
echo "<td>" . $item['sport'] . "</td>";
echo "<td>" . $item['team'] . "</td>";
echo "<td>" . $item['price'] . "</td>";
echo "<td>" . $item['shipping'] . "</td>";
echo "</tr>";
}
echo "</table>";
if that code really looks like this remove the do-while loop and everything where you iterate over your $mydata and it will work since you waste the internal mysql-counter
meaning in the second loop your $result is already at the end, either you query again OR you push all the results in an array for multiple use
You have already pulled your data once with this.....$product = mysql_fetch_array($myData);
so just use this array with foreach loop....
foreach($product as $sProduct){
// User $sProduct to access that single product.
}
Your code contains many bugs and implementation
try this if it works for you :
<?php
include("includes/dbconnect.php");
$sql = "SELECT * FROM products";
$myData = mysql_query($sql) or die (mysql_error());
?>
<html>
<head>
<title></title>
</head>
<body>
//This plain text works
<?php
if(mysql_num_rows ($myData) > 0)
{
$html = "<table border=1><tr>
<th>Title</th>
<th>Category</th>
<th>Sport</th>
<th>Team</th>
<th>Price</th>
<th>Shipping</th></tr>";
while ($product = mysql_fetch_array($myData))
{
$html .= "<tr>";
$html .= "<td>".$product['title']."</td>";
$html .= "<td>".$product['category']."</td>";
$html .= "<td>".$product['sport']."</td>";
$html .= "<td>".$product['team']."</td>";
$html .= "<td>" . $product['shipping'] . "</td>";
$html .= "</tr>";
}
$html .= "</table>";
echo $html;
}
else
{
echo "No Product Found";
}
?>
</body>
</html>
If this works for you, you can figure out the problems or I will explain :)