My code is working as for my needs. But the only thing bugging me is
the "else" is not working. When i search for a correct record the
record will appear and it was running fine. But if i Incorrectly
search a record nothing will happen. i am expecting "Records not Found" will echo but nothing happen.
}else{
echo "Records not found";
}
This is the whole code.
<?php
$conn = mysqli_connect("localhost", "root", "", "my1stdb") or die("could not connect");
$set = $_POST['search'];
if ($set) {
$show = "SELECT * FROM users where email='$set'";
$result = mysqli_query($conn, $show);
while ($rows = mysqli_fetch_array($result)) {
echo "Registrant Found";
echo "<tr>";
echo "<td>";
echo $rows['username'];
echo "</td>";
echo "<td>";
echo $rows['fullname'];
echo "</td>";
echo "<td>";
echo $rows['password'];
echo "</td>";
echo "<td>";
echo $rows['email'];
echo "</td>";
echo "</tr>";
echo "<br/>";
}
} else {
echo "Records not found";
}
?>
</table>
You need to use mysqli_num_rows() along with mysqli_fetch_assoc():-
<?php
$conn=mysqli_connect("localhost","root","","my1stdb") or die("could not connect");
$set = $_POST['search'];
if($set) {
$show="SELECT * FROM users where email='$set'";
$result=mysqli_query($conn,$show) or die(mysqli_error($conn));
if(mysqli_num_rows($result)>0){ // check data present or not
while($rows=mysqli_fetch_assoc($result)){ // for lighter array due to associative indexes only
echo "Registrant Found";
echo "<tr>";
echo "<td>";
echo $rows['username'];
echo "</td>";
echo "<td>";
echo $rows['fullname'];
echo "</td>";
echo "<td>";
echo $rows['password'];
echo "</td>";
echo "<td>";
echo $rows['email'];
echo "</td>";
echo "</tr>";
echo "<br/>";
}
}else{
echo "Records not found";
}
}else{
echo "Please insert search term";
}
?>
</table>
Note:- Your code is wide-open for SQL INJECTION. to prevent from it use prepared statements
Reference:-
mysqli prepared statements
PDO prepared statements
You could count the number of results returned.
if($set) {
$show="SELECT * FROM users where email='$set'";
$result=mysqli_query($conn,$show);
$recordCount = 0;
while($rows=mysqli_fetch_array($result)){
$recordCount++;
echo "Registrant Found";
echo "<tr>";
echo "<td>";
echo $rows['username'];
echo "</td>";
echo "<td>";
echo $rows['fullname'];
echo "</td>";
echo "<td>";
echo $rows['password'];
echo "</td>";
echo "<td>";
echo $rows['email'];
echo "</td>";
echo "</tr>";
echo "<br/>";
}
if($recordCount==0){
echo "Records not found";
}
}
Related
I want to merge cells in a column like column A or B in the image below:
That table is a database. All I have now is:
$polaczenie = #new mysqli($host, $db_user, $db_password, $db_name);
$wynik = mysqli_query($polaczenie,'SELECT * FROM grafik');
echo "<table cellpadding=7 border=3>";
echo "<tr>";
echo "<td>"."DzieĆ"."</td>";
echo "<td>"."Zmiana"."</td>";
echo "<td>"."Stanowisko 1"."</td>";
echo "<td>"."a_1"."</td>";
echo "<td>"."a_2"."</td>";
echo "<td>"."a_3"."</td>";
echo "<td>"."a_4"."</td>";
echo "<td>"."Stanowisko 2"."</td>";
echo "<td>"."b_1"."</td>";
echo "<td>"."b_2"."</td>";
echo "<td>"."b_3"."</td>";
echo "<td>"."b_4"."</td>";
echo "</tr>";
while ($row = $wynik->fetch_assoc()) {
echo "<tr>";
echo "<td>".$row['date']."</td>";
echo "<td>".$row['shift']."</td>";
echo "<td>".$row['stanowisko_1']."</td>";
echo "<td>".$row['a_1']."</td>";
echo "<td>".$row['a_2']."</td>";
echo "<td>".$row['a_3']."</td>";
echo "<td>".$row['a_4']."</td>";
echo "<td>".$row['stanowisko_2']."</td>";
echo "<td>".$row['b_1']."</td>";
echo "<td>".$row['b_2']."</td>";
echo "<td>".$row['b_3']."</td>";
echo "<td>".$row['b_4']."</td>";
echo "</tr>";
It looks like this
instead of this
If it's the Data and Dzien columns a simple rowspan will do it:
while ($row = $wynik->fetch_assoc()) {
echo "<tr>";
echo "<td rowspan='8'>".$row['date']."</td>";
echo "<td rowspan='8'>".$row['shift']."</td>";
echo "<td>".$row['stanowisko_1']."</td>";
echo "<td>".$row['a_1']."</td>";
echo "<td>".$row['a_2']."</td>";
echo "<td>".$row['a_3']."</td>";
echo "<td>".$row['a_4']."</td>";
echo "<td>".$row['stanowisko_2']."</td>";
echo "<td>".$row['b_1']."</td>";
echo "<td>".$row['b_2']."</td>";
echo "<td>".$row['b_3']."</td>";
echo "<td>".$row['b_4']."</td>";
echo "</tr>";
Here is my Code
<?php
require '../db/dbcon.php';
$sql="SELECT ClientId,Email,FirstName,LastName,Password,PhoneNumber,AdressID from client;";
$sth = $dbh->prepare($sql);
$sth->execute();
foreach($sth as $row){
echo "<tr>";
echo "<td>".$row['FirstName']."</td>";
echo "<td>".$row['LastName']."</td>";
echo "<td>".$row['Password']."</td>";
echo "<td>".$row['Email']."</td>";
echo "<td>".$row['PhoneNumber']."</td>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
$clientid=$row['ClientId'];
echo "<form action=\"viewclients.php\" method=\"post\"> ";
echo "<td><input type=\"submit\" name=\"fshij\" value=\"Fshij\"></td>";
echo "</form>";
if(isset($_POST['fshij'])){
$sql = "DELETE FROM client WHERE ClientId=".$clientid.";";
$sth = $dbh->prepare($sql);
$sth->execute();
header('location:viewclients.php');
}
echo "</tr>";
}
?>
After the results are shown in the Webpage, if I click delete in any of these entries it will delete all of them.
Start by placing the value of the ClientId in the button value.
Then move the delete code up to the top of the script, and outside the loop.
<?php
require '../db/dbcon.php';
if(isset($_POST['fshij'])){
// use a parameterised query here as you are passing user data
// to this query
$sql = "DELETE FROM client WHERE ClientId=?";
$sth = $dbh->prepare($sql);
// if MYSQLI
$sth->bind_param("i", $_POST['fshij']);
$sth->execute();
// if PDO
$sth->execute(array($_POST['fshij']));
header('location:viewclients.php');
}
$sql="SELECT ClientId,Email,FirstName,LastName,
Password,PhoneNumber,AdressID
from client";
// you could have done this query using ->query() as there are no parameters to prepare
$sth = $dbh->prepare($sql);
$sth->execute();
foreach($sth as $row){
echo "<tr>";
echo "<td>".$row['FirstName']."</td>";
echo "<td>".$row['LastName']."</td>";
echo "<td>".$row['Password']."</td>";
echo "<td>".$row['Email']."</td>";
echo "<td>".$row['PhoneNumber']."</td>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "<td>";
echo "<form action=\"viewclients.php\" method=\"post\"> ";
echo "<input type='submit' name='fshij' value='{$row['ClientId']}'>";
echo "</form>";
echo "</td>";
echo "</tr>";
}
?>
I've got a table in Drupal with the following code:
$db = mysql_connect("localhost", "root", "moocow");
mysql_select_db("vedb", $db);
$result = mysql_query("SELECT NodeID,NodeDesc,NodeZone,Fusion,DSLID FROM `nodeidtable` WHERE DSLID != '' AND `NodeZone` = 'CLOSED' ORDER BY NodeID ASC");
$num_rows = mysql_num_rows($result);
echo "<table>";
echo "<tr>";
echo "<th>CLOSED SITES</th>";
echo "<th></th>";
echo "<th></th>";
echo "<th></th>";
echo "<th></th>";
echo "</tr>";
echo "<tr>";
echo "<th>Node ID</th>";
echo "<th>Node Address</th>";
echo "<th>Node Zone</th>";
echo "<th>Fusion Status</th>";
echo "<th>Service Number</th>";
echo "</tr>";
//display the data
while ($rows = mysql_fetch_array($result,MYSQL_ASSOC))
{
echo "<tr>";
foreach ($rows as $data)
{
echo "<td align='center'>". $data . "</td>";
}
}
echo "<br>";
echo "<tr>";
echo "</table>";
mysql_free_result($result);
mysql_close($db);
?>
Now I can change it renders the td to include the individual columns, but I really want to add a little edit button on the right-hand side which will let me edit that particular row fields.
Any ideas?
Replace your while loop with the following code:
while ($rows = mysql_fetch_array($result,MYSQL_ASSOC))
{
echo "<tr>";
foreach ($rows as $data)
{
echo "<td align='center'>". $data . "</td>";
}
//create link for current node edit
echo "<td align='center'>". l(t('Edit this node'), 'node/' . $row['NodeId'] . '/edit') ."</td>";
echo "</tr>";
}
Remove echo "<br>"; and echo "<tr>"; below to while loop. This will resolve the issue for you
I am trying to display a table from database using two nested foreach loops with some criteria. here is the code-
echo "<div class='container'>";
echo "<table class='table table-hover table-bordered table-condensed' style='width:95%' align='center'>";
echo "<tr>";
echo "<th>";
echo "Edit";
echo "</th>";
echo "<th>";
echo "Delete";
echo "</th>";
echo "<th>";
echo "Sl. No.";
echo "</th>";
echo "<th>";
echo "Group";
echo "</th>";
echo "<th>";
echo "Component";
echo "</th>";
echo "<th>";
echo "Quantity";
echo "</th>";
echo "</tr>";
if($rslt->rowCount() > 0)
{
foreach($rslt as $item)
{
foreach($rslt3 as $item3)
{
/*echo $item3['component'];
if($item3['component']===$item['component'])
{
if($Qty>=$item3['Qty'])
{
$item3[Qty]=$item3[Qty]-$item[Qty];
*/
//will implement this after the second loop starts working
$id = $item['entry_id'];
$Qty = $item['Qty'];
$group_ID = $item['group_ID'];
$component = $item['component'];
$vendor_ID = $item['vendor_ID'];
echo "<tr>";
echo "<td>";
echo "<a href='production_edit.php?id=$id&Qty=$Qty&group_ID=$group_ID&component=$component&vendor_ID=$vendor_ID'>Edit</a>";
echo "</td>";
echo "<td>";
echo "<a href='production_delete.php?id=$id&vendor_ID=$vendor_ID'>Delete</a>";
echo "</td>";
echo "<td>";
echo $item['entry_id'];
echo "</td>";
echo "<td>";
echo $item['group_ID'];
echo "</td>";
echo "<td>";
echo $item['component'];
echo "</td>";
echo "<td>";
echo $item['Qty'];
echo "</td>";
echo "</tr>";
}
}
}
echo "</table></div><br>";
}
Now the problem here is when I use the second foreach loop the table displays the first entry in each table row... I am curious where I am at fault with this second foreach loop.. Thanks in advance
I've put together the following code which creates a table upon a selection being made with a drop down menu.
echo "<table>";
$result=mysql_query($query);
while($rows=mysql_fetch_array($result)){
echo "<tr>";
echo "<th>Find Name:</th>";
echo "<th>Find Description:</th>";
echo "</tr>";
echo "<tr>";
echo "<td>".$rows['findname']."</td>";
echo "<td>".$rows['finddescription']."</td>";
echo "</tr>";
}
echo "</table>";
The problem I've got is that for each record that is returned the 'headers' are repeated. The live page can be found here. I just wonder whether someone could perhaps take a look at this please and tell me where I've gone wrong.
Apologies for the really simple question, but I've been looking at this for a while and I just can't find the answer. I think it just needs a fresh pair of eyes to look at it.
Try this, you just need to get headers out of while loop
echo "<table>";
echo "<tr>";
echo "<th>Find Name:</th>";
echo "<th>Find Description:</th>";
echo "</tr>";
$result=mysql_query($query);
while($rows=mysql_fetch_array($result)){
echo "<tr>";
echo "<td>".$rows['findname']."</td>";
echo "<td>".$rows['finddescription']."</td>";
echo "</tr>";
}
echo "</table>";
The answer is obvious, you are repeating the output of the headers in the loop. Move the
while($rows=mysql_fetch_array($result)){
after the first
echo "</tr>";
You need to put the headers outside of the while loop:
echo "<table>";
echo "<tr>";
echo "<th>Find Name:</th>";
echo "<th>Find Description:</th>";
echo "</tr>";
$result = mysql_query($query);
while ($rows = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $rows['findname'] . "</td>";
echo "<td>" . $rows['finddescription'] . "</td>";
echo "</tr>";
}
echo "</table>";
This should work:
echo "<table>";
echo "<tr>";
echo "<th>Find Name:</th>";
echo "<th>Find Description:</th>";
echo "</tr>";
$result=mysql_query($query);
while($rows=mysql_fetch_array($result)){
echo "<tr>";
echo "<td>".$rows['findname']."</td>";
echo "<td>".$rows['finddescription']."</td>";
echo "</tr>";
}
echo "</table>";
Your headers are being repeated because you are writing them inside the loop, for each row returned by the query. You simply need to move your headers outside the loop so they're only written once, before the rows returned by the query start printing:
echo "<table>";
echo "<tr>";
echo "<th>Find Name:</th>";
echo "<th>Find Description:</th>";
echo "</tr>";
$result=mysql_query($query);
while($rows=mysql_fetch_array($result)){
echo "<tr>";
echo "<td>".$rows['findname']."</td>";
echo "<td>".$rows['finddescription']."</td>";
echo "</tr>";
}
echo "</table>";
headers are repeating becasue they are in while loop, it should work well
echo "<table>";
echo "<tr>";
echo "<th>Find Name:</th>";
echo "<th>Find Description:</th>";
echo "</tr>";
$result=mysql_query($query);
while($rows=mysql_fetch_array($result)){
echo "<tr>";
echo "<td>".$rows['findname']."</td>";
echo "<td>".$rows['finddescription']."</td>";
echo "</tr>";
}
echo "</table>";
Change to:
echo "<tr>";
echo "<th>Find Name:</th>";
echo "<th>Find Description:</th>";
echo "</tr>";
echo "<tr>";
while($rows=mysql_fetch_array($result)){
echo "<td>".$rows['findname']."</td>";
echo "<td>".$rows['finddescription']."</td>";
echo "</tr>";
}
echo "</table>";