New fetched row overwriting the previous row while writing to a table - php

<?php
session_start();
?>
<html>
<head>
<link rel="stylesheet" href="css/bootstrap.css">
<script>
function submit() {
document.getElementById("myform").submit();
}
window.onload = function() {
document.getElementById("textbox").focus();
};
</script>
</head>
<body>
<form id="myform" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<h1> AWB_NO</h1>
<input type=text id="textbox" name="excel" onchange="submit()">
</br>
</form>
<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
$vars = $_COOKIE['var1'];
if(!isset($_SESSION['items'])) {
$_SESSION['items'] = array();
}
if(!isset($_SESSION['arr2'])) {
$_SESSION['arr2'] = array();
}
$var='';
if(isset($_POST['excel']))
{
$host="localhost";
$user="root";
$password="";
$db="greenmobiles";
$table="manifest";
$var=$_POST['excel'];
$con=mysqli_connect("$host","$user","$password") or die("Cannot Connect");
mysqli_select_db($con,"$db") or die("Cannot select DB!");
if (in_array($var,$_SESSION['items']))
{
echo"The new value has been scanned!</br>";
echo "<hr>";
echo "The tracking id's of the currently scanned items are given below<br><hr>";
foreach ($_SESSION['items'] as $x => $value)
{
echo "$value";
echo "<br>";
}
echo "<script>alert('oops! This item has already been scanned!')</script>";
}
else
{
$sql = "SELECT * FROM manifest WHERE awb_no = '$var'";
$result = mysqli_query($con,$sql);
if(mysqli_num_rows($result) > 0)
{
echo "tracking id present in the manifest</br>";
}
else
{
echo "<script>alert('Tracking id is not present in the manifest!')</script>";
}
$sqli = "SELECT * FROM manifest WHERE (awb_no = '$var') AND (document_no='$vars')";
$result1 = mysqli_query($con,$sqli);
if (mysqli_num_rows($result1))
{
echo "The tracking id matches with the document no.";
}
else
{
echo "<script>alert('Tracking id does not belong to the document number entered.')</script>";
exit(0);
}
while ($row = mysqli_fetch_assoc($result))
{
$_SESSION['data'][] = $row['awb_no'];
$_SESSION['items'] = $_SESSION['data'];
}
mysqli_free_result($result);
echo"The new value has been scanned!</br>";
echo "<hr>";
echo "The tracking id's of the currently scanned items are given below<br><hr>";
echo '<div class="col-lg-10 col-lg-offset-1 panel">'."<table class='table'>
<tr>
<th>Document No</th>
<th>AWB NO</th>
<th>Order Id</th>
<th>Forms</th>
<th>Extras</th>
</tr>";
echo "<tr>";
$sqlq= "SELECT * FROM manifest WHERE awb_no = '$var'";
$result2 = mysqli_query($con,$sqlq);
if ($result2)
{
while ($row=mysqli_fetch_row($result2))
{
echo "<tr>";
echo "<td>" . $row[0] . "</td>";
echo "<td>" . $row[1] . "</td>";
echo "<td>" . $row[2] . "</td>";
echo "<td>" . $row[3] . "</td>";
echo "<td>" . $row[4] . "</td>";
echo "</tr>";
}
echo "</table></div>";
}
}
}
else
{
session_destroy();
}
?>
</body>
</html>
Hello, i am new to php and html. I am giving an input to a text box whose value is '$var' and retrieving data one row at a time and trying to print it in a html table via php. But each time i enter a value the previous row gets overwritten by the new row and i am not able to keep the previous rows and append the new ones to my table. Can someone please tell me where i am going wrong.

Related

PHP /MYSQL Search Form Displaying Empty Results

Please Guys what must be wrong with this my code, I have tried to fetch the search data from my Database as follows
Students DATA As follow
Reg Number: Full Name: Faculty: Program: Level: Group.
on the HTML Search Page
<html>
<h2> Enter your matric number to connect to others studying your course </h2>
<form action="demo.php" method="post">
<b> ReG </b><input type="text" Name="find">
<input type="submit" value="Submit" />
</form>
</html>
PHP SIDE
<table border="1">
<tr>
<th>Student Full Name</th>
<th> Faculty</th>
<th> Program</th>
<th> Entry Year</th>
<th> Study Group</th>
<th> Group Members Contact</th>
<th> Group Leader Contacts</th>
</tr>
<?php
$conn=mysqli_connect("localhost", "root", "", "student");
// Check connection
if($conn=== false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$q = $_POST['find'];
if ($q == "")
{
echo "<p>You forgot to enter a search term!!!";
exit;
}
$sql = "SELECT * FROM study_circle WHERE matric LIKE $q ";
$result = mysqli_query($conn, $sql);
if ($result)
{
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>";
echo $row['full_name'];
echo "</td>";
echo "<td>";
echo $row['faculty'];
echo "</td>";
echo "<td>";
echo $row['program'];
echo "</td>";
echo "<td>";
echo $row['entry_year'];
echo "</td>";
echo "<td>";
echo $row['study_group'];
echo "</td>";
echo "<td>";
echo $row['group_members'];
echo "</td>";
echo "<td>";
echo $row['group_leader'];
echo "</td>";
echo "</tr>";
echo "<br/>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
Each time I try it it's bringing Empty results even though I have populated the database
Right now it's on my Localhost system, please anyone good person to help, am just new to Programming. I will be happy to fix this
Your code is misinterpreting what a false value means here:
if ($result)
{
//...
} else {
echo "0 results";
}
A false value in $result doesn't mean the search didn't find anything, it means the query failed with an error. To get the error, use mysqli_error:
if ($result)
{
//...
} else {
echo "There was an error: " . mysqli_error($conn);
}
In this case the error is probably a syntax error in your SQL code because you're directly concatenating user input to SQL code. This is also called a SQL injection vulnerability. There is some good information to get you started on correcting that here, including specifically how to use query parameters with the LIKE operator here. At its simplest you will want to use a prepared statement with a query parameter instead of using string interpolation like you do now.
Try adding wildcard characters to the start and end of the search string:
$sql = "SELECT * FROM study_circle WHERE matric LIKE '%$q%'";
You willl need to get the rowcount of the search data. That will gurantee if the record is there or not
Eg.
$rowcount = mysqli_num_rows($result);
and then perform if statement with it.
see code below.
<?php
$conn=mysqli_connect("localhost", "root", "", "student");
// Check connection
if($conn=== false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$q = $_POST['find'];
if ($q == "")
{
echo "<p>You forgot to enter a search term!!!";
exit;
}
$sql = "SELECT * FROM study_circle WHERE matric LIKE $q ";
$result = mysqli_query($conn, $sql);
$rowcount = mysqli_num_rows($result);
if( $rowcount ){
$row = mysql_fetch_array($result);
echo "<tr>";
echo "<td>";
echo $row['full_name'];
echo "</td>";
echo "<td>";
echo $row['faculty'];
echo "</td>";
echo "<td>";
echo $row['program'];
echo "</td>";
echo "<td>";
echo $row['entry_year'];
echo "</td>";
echo "<td>";
echo $row['study_group'];
echo "</td>";
echo "<td>";
echo $row['group_members'];
echo "</td>";
echo "<td>";
echo $row['group_leader'];
echo "</td>";
echo "</tr>";
echo "<br/>";
exit;
}else{
echo "0 results";
}
}
mysqli_close($conn);
?>

Execute MySQL Delete on Button Click

In my code, I am showing a table within my database called staff in a HTML table and I want to add a delete button to each row in the HTML table that when clicked, it will delete the record its associated with.
Based on searching other solutions, my code looks like this:
staff.php:
require_once('../connection.php');
//delete row on button click
if(isset($_GET["del"])){
$idc = $_GET["del"];
if($VisitorManagement->query("DELETE FROM staff WHERE id=$idc")){
header('Location: delete-thankyou.php');
} else {
echo "Failed to delete staff member.";
}
}
$result = mysqli_query($VisitorManagement, "SELECT * FROM staff ORDER BY fullName");
echo "<table id='staff'>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th></th>
</tr>
</thead>";
while($row = mysqli_fetch_array($result))
{
echo "<tbody>";
echo "<tr>";
echo "<td>" . $row['fullName'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td><a class='button alert' href='staff.php?del=".$row["idc"]."'>Delete</a></td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
connection.php:
$hostname_VisitorManagement = "localhost";
$database_VisitorManagement = "visitor-management";
$username_VisitorManagement = "***";
$password_VisitorManagement = "***";
$VisitorManagement = mysqli_connect($hostname_VisitorManagement, $username_VisitorManagement, $password_VisitorManagement, $database_VisitorManagement);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
date_default_timezone_set('America/New_York');
Unfortunately, when I go to the click one of the buttons, it fails to delete the record and echoes the error message pre-defined in staff.php. Am I missing something to get this to work?
I was able to fix it by changing all instances of idc to id.
New code is:
require_once('../connection.php');
//delete row on button click
if(isset($_GET["del"])){
$id = $_GET["del"];
if($VisitorManagement->query("DELETE FROM staff WHERE id=$id")){
header('Location: delete-thankyou.php');
} else {
echo "Failed to delete staff member.";
}
}
$result = mysqli_query($VisitorManagement, "SELECT * FROM staff ORDER BY fullName");
echo "<table id='staff'>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th></th>
</tr>
</thead>";
while($row = mysqli_fetch_array($result))
{
echo "<tbody>";
echo "<tr>";
echo "<td>" . $row['fullName'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td><a class='button alert' href='staff.php?del=".$row["id"]."'>Delete</a></td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";

How to retrieve data from the first row from database in php code

I wrote this code to retrieve some data from data base But the code do not display the first value in the table which is started from second row.
How I can make this code retrieve the data from first row.
<html>
<head>
<title>hello</title>
</head>
<body>
<?php
$con = mysqli_connect('localhost', 'root', '');
mysqli_select_db($con,"uoh");
$q = " SELECT * FROM student_record WHERE id =201102887;";
$result = mysqli_query($con , $q ) ;
if($row = mysqli_fetch_array($result)) {
echo "<table border=\"1\" style=\"width:500\">";
echo "<tr>";
echo "<th>courses</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row["grade"]. "</td>";
echo "</tr>";
}
echo "</table>";
}
?>
</body>
</html>
Change
if($row = mysqli_fetch_array($result)) {
to
if(mysqli_num_rows($result) > 0) {
Updated Code
<html>
<head>
<title>hello</title>
</head>
<body>
<?php
$con = mysqli_connect('localhost', 'root', '');
mysqli_select_db($con,"uoh");
$q = " SELECT * FROM student_record WHERE id =201102887;";
$result = mysqli_query($con , $q ) ;
if(mysqli_num_rows($result)>0) {
echo "<table border=\"1\" style=\"width:500\">";
echo "<tr>";
echo "<th>courses</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row["grade"]. "</td>";
echo "</tr>";
}
echo "</table>";
}
?>
</body>
</html>
as far as i know when you call this twice,
if($row = mysqli_fetch_array($result)) {
then first row will skip when you call it again in loop, what point you create if($row = mysqli_fetch_array($result)) { ? if you just want check query return you can just use if($result) {
You are calling mysqli_fetch_array two times second call is going to the second row. Try like this
<html>
<head>
<title>hello</title>
</head>
<body>
<?php
$con = mysqli_connect('localhost', 'root', '');
mysqli_select_db($con,"uoh");
$q = " SELECT * FROM student_record WHERE id =201102887;";
$result = mysqli_query($con , $q ) ;
$rows = array();
while($row = mysqli_fetch_array($result))
{
$rows[] = $row;
}
if(count($rows) > 0) {
echo "<table border=\"1\" style=\"width:500\">";
echo "<tr>";
echo "<th>courses</th>";
echo "</tr>";
foreach($rows as $row)
{
echo "<tr>";
echo "<td>" . $row["grade"]. "</td>";
echo "</tr>";
}
echo "</table>";
}
?>
</body>
</html>

checkbox in table in php

I want to have a table that contains words and their meaning from database, in another column i want to have checkbox for each row, that user will check them and show what words he/she know.
i have two question in this case:
1- how can i hide the meaning in the first and after clicking in show meaning visible them?
2- how can i set checkboxes?
i have this code until now but it doesn't work
please help me if you can
<script type="text/javascript">
function ShowMeanings(){
document.getElementsByClassName('hiding').item(0).style.visiblility = 'visible';
}
</script>
<?php
$con = mysql_connect("localhost", "root", "")
or die(mysql_error());
if (!$con) {
die('Could not connect to MySQL: ' . mysql_error());
}
mysql_select_db("project", $con)
or die(mysql_error());
$result = mysql_query("select * from words");
echo "<table border='1'>
<tr>
<th>word</th>
<th>meaning</th>
<th>check</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['word'] . "</td>";
echo "<td>";
echo "<div";
echo "class='hiding' style='visibility:hidden'>" . $row['meaninig'];
echo "</div>";
echo "</td>";
echo "<td>";
echo "<input";
echo "type= 'checkbox' name = 'checkbox' id = 'checkbox' value = '' />";
echo "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
</div>
<button onclick="ShowMeanings()">showmeaning</button>
getElementByClassName is an inexistant function. You mean getElementsByClassName, which will however return a list of the elements, so you need to select one.
document.getElementsByClassName('hiding').item(0).style.visibility = 'visible';
For hide a suggestion:
echo "class='hiding' style='display:none'>" . $row['meaninig'];
To show the meaning:
//for Jquery
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript">
function ShowMeanings(){
$('.hiding').shoW();
}
</script>
Or
//for plain old javascript
<script type="text/javascript">
function ShowMeanings(){
document.getElementsByClassName('hiding').style.visibility = 'visible';
}
</script>
Your code Edited:
<html><head>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript">
function ShowMeanings(){
$('.hiding').shoW();
}
</script>
</head>
<body>
<?php
$con = mysql_connect("localhost", "root", "")
or die(mysql_error());
if (!$con) {
die('Could not connect to MySQL: ' . mysql_error());
}
mysql_select_db("project", $con)
or die(mysql_error());
$result = mysql_query("select * from words");
echo "<table border='1'>
<tr>
<th>word</th>
<th>meaning</th>
<th>check</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['word'] . "</td>";
echo "<td>";
echo "<div";
echo "class='hiding' style='display:none'>" . $row['meaninig'];
echo "</div>";
echo "</td>";
echo "<td>";
echo "<input";
echo "type= 'checkbox' name = 'checkbox' id = 'checkbox' value = '' />";
echo "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
</div>
<button onclick="ShowMeanings()">showmeaning</button>
</body>

click row to fill textboxes in php - array vs ajax

I have a table coded in php which pulls in data from mysql database and displays it in a html table . Now when i click on a row I need certain textboxes to be filled.
What is the best approach for this: is it using an array or ajax,html and php,
<?php
$default = "<img src='http://localhost/on.png' width='24' height='24'/>";
$default1 = "<img src='http://localhost/of.png' width='24' height='24'/>";
$con = mysql_connect("*****","******","******");
if (!$con){
die("Can not connect: " . mysql_error());
}
mysql_select_db ("*****",$con);
$sql= "select act.*
from audit_activity as act
inner join (
select user_id, max(timestamp) as max_ts
from activity
group by user_id) as a on act.user_id=a.user_id and act.timestamp=a.max_ts";
$mydata = mysql_query($sql,$con);
echo "<table id='tfhover',table border=0>
<tr>
<th>Users</th>
<th>Status<th>
</tr>";
while($record = mysql_fetch_array($mydata)){
echo "<tr>";
echo "<td>" . $record['user_id'] . "</td>";
if (strtolower(trim($record['activity']))!=strtolower('LOGIN')){
echo "<td>" . $default1 . "</td>";
}else{
echo "<td>" . $default . "</td>";
}
echo "</tr>";
}
echo "</table>";
;
mysql_close($con);
?>
<html>
<script type="text/javascript">
window.onload=function(){
var tfrow = document.getElementById('tfhover').rows.length;
var tbRow=[];
for (var i=1;i<tfrow;i++) {
tbRow[i]=document.getElementById('tfhover').rows[i];
tbRow[i].onmouseover = function(){
this.style.backgroundColor = '#ffffff';
};
tbRow[i].onmouseout = function() {
this.style.backgroundColor = '#d4e3e5';
};
}
};
</script>
<head>
</head>
<body>
Total exams taken : <br>
<input type="text" name="fname"/>
</body>
</html>

Categories