Limiting Data in php mysql - php

I have made a slambook, it is still under contruction.
I have written the following code :
<?php
$n=$_REQUEST['n'];
$n=strtolower($n);
echo "<html><body alink=black vlink=black link=black><center><h1>";
$target_dir = "fu/uploads/";
$target_file = $target_dir . $n . ".jpg";
if (file_exists($target_file)) {
echo "<img src=\"http://slambook.esy.es/fu/uploads/".$n.".jpg\" width=85 height=85 alt=\"Profile Pic\">";
}
echo "Welcome to your wall, ".$n."</h1><br><br>";
echo "<button>Change Password</button><br><br>";
echo "<form action=\"visit.php\" method=\"get\">";
echo "<input type=\"hidden\" name=\"u\" value=\"".$n."\">";
echo "<input type=\"text\" name=\"n\" placeholder=\"Search A Nickname\">";
echo "<input type=submit value=\"Search By Nickname\"></form>";
echo "<form action=\"sbi.php\" method=\"post\">";
echo "<input type=\"hidden\" name=\"u\" value=\"".$n."\">";
echo "<input type=\"text\" name=\"id\" placeholder=\"Search A id\">";
echo "<input type=submit value=\"Search By Id\"></form>";
$post="http://slambook.esy.es/post.php?n=".$n;
echo "Fill Someone's Slambook<br><br><br>";
$servername = "myhost";
$username = "myusername";
$password = "mypass";
$dbname="mydatabase";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT i,f,e,p,m,d FROM ".$n." ORDER BY i DESC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<center><table border=2><tr><th>Date</th><th>From</th><th>Email</th><th>Phone</th><th>Message</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
$from=$row["f"];
echo "<tr><td>".$row["d"]."</td><td>".$from."</td><td>".$row["e"]."</td><td> ".$row["p"]."</td><td>".$row["m"]."</td></tr>";
}
echo "</table></center>";
} else {
echo "Your slambook has been filled by 0 people!";
}
$conn->close();
?>
But on running this wall.php, all records are printed on one page.
I wanted to limit the data per page and use the paging system.
What i wanted is at the end to the table,
a "previous post" link must be there which would show the next posts.
But i tries many times and what i came out with is this :
<?php
$n=$_REQUEST['n'];
$p=$_REQUEST['p'];
$n=strtolower($n);
if($p=="")
$p=2;
$ipp=2;
$os=($p-1)*$ipp;
echo "<html><body alink=black vlink=black link=black><center><h1>";
$target_dir = "fu/uploads/";
$target_file = $target_dir . $n . ".jpg";
if (file_exists($target_file)) {
echo "<img src=\"http://slambook.esy.es/fu/uploads/".$n.".jpg\" width=85 height=85 alt=\"Profile Pic\">";
}
echo "Welcome to your wall, ".$n."</h1><br><br>";
echo "<button>Change Password</button><br><br>";
echo "<form action=\"visit.php\" method=\"get\">";
echo "<input type=\"hidden\" name=\"u\" value=\"".$n."\">";
echo "<input type=\"text\" name=\"n\" placeholder=\"Search A Nickname\">";
echo "<input type=submit value=\"Search By Nickname\"></form>";
echo "<form action=\"sbi.php\" method=\"post\">";
echo "<input type=\"hidden\" name=\"u\" value=\"".$n."\">";
echo "<input type=\"text\" name=\"id\" placeholder=\"Search A id\">";
echo "<input type=submit value=\"Search By Id\"></form>";
$post="http://slambook.esy.es/post.php?n=".$n;
echo "Fill Someone's Slambook<br><br><br>";
$servername = "myhost";
$username = "myuser";
$password = "mypass";
$dbname="mydatabase";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM ".$n." ORDER BY id DESC LIMIT ".$os.",".$ipp."";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$page_count = (int)ceil($result / $ipp);
// double check that request page is in range
if($p > $page_count) {
// error to user, maybe set page to 1
$p = 1;
}
echo "<center><table border=2><tr><th>Date</th><th>From</th><th>Email</th><th>Phone</th><th>Message</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
$from=$row["f"];
echo "<tr><td>".$row["d"]."</td><td>".$from."</td><td>".$row["e"]."</td><td> ".$row["p"]."</td><td>".$row["m"]."</td></tr>";
}
echo "</table></center>";
echo "Previous Posts";
} else {
echo "No more Posts";
}
$conn->close();
?>
But in this, there were 5 records and records per page was set to 2 so only two pages are visible and the 5th record (should be on page 3) never turns up.
What should I do to fix this error ?

U can use Datatables for more easy pagination, check this, and tell me if work for u

Related

Specific information on how to insert into html table

I am trying to use the results of a database and display them in an HTML table. I've searched around but can't find a specific answer for my situation as I am new to PHP and I had to copy this code from another source because I was unable to get it to work on my own. Please help.
<?php $server = "127.0.0.1";
$user = "admin";
$pass = "password";
$dbname = "hysteryalelogs";
// Create connection in mysqli
$connection = new mysqli($server, $user, $pass, $dbname);
//Check connection in mysqli
if($connection->connect_error){
die("Error on connection:" .$connection->connect_error);
}
//Display the informaion
$sql = "SELECT * FROM logs";
$res = $connection->query($sql);
if($res->num_rows > 0){
while($row = $res->fetch_assoc()){
echo "Sales Number: ". $row["Sales_Number"]. "<br/>";
echo "Quantity: ". $row["Quantity"]. "<br/>";
echo "Due Date: ". $row["Due_Date"]. "<br/>";
echo "Burnished Housing: ". $row["Burnished"]. "<br/>";
echo "Greased Bearings: ". $row["Grease"]. "<br/>";
echo "Air Cleaned: ". $row["Air"]. "<br/>";
echo "SS Screw: ". $row["Screw"]. "<br/>";
echo "Test Date: ". $row["Test_Date"]. "<br/>";
echo "Pass: ". $row["Pass"]. "<br/>";
echo "Fail: ". $row["Fail"]. "<br/>";
echo "Tester: ". $row["Tester"]. "<br/>";
echo "Final Check: ". $row["Final_Check"]. "<br/>";
echo "Green Dot: ". $row["Green_Dot"]. "<br/>";
echo "Green Dot Check: ". $row["Green_Dot_Check"]. "<br/>";
echo "Ship Date: ". $row["Ship_Date"]. "<br/>";
echo "Serial Number: ". $row["Serial_Number"]. "<br><br><hr><br/>";
}
} else {
echo "No Record Found!";
}
$connection->close();
?>
(clarification) The code works. I Just don't know how to format the table for it to display inside.
Hi :) Hope this helps :)
<?php
$server = "127.0.0.1";
$user = "admin";
$pass = "password";
$dbname = "hysteryalelogs";
// Create connection in mysqli
$connection = new mysqli($server, $user, $pass, $dbname);
//Check connection in mysqli
if($connection->connect_error){
die("Error on connection:" .$connection->connect_error);
}
//Display the informaion
$sql = "SELECT * FROM logs";
$res = $connection->query($sql);
if($res->num_rows > 0){
echo "<table border='3px' cellpadding='5px' cellspacing='5px' align='center' bgcolor='skyblue' ";
echo "<tr>";
echo "<td><center><strong>Sales Number</strong></center></td>";
echo "<td><center><strong>Quantity</strong></center></td>";
echo "<td><center><strong>Due Date</strong></center></td>";
echo "<td><center><strong>Burnished Housing</strong></center></td>";
echo "<td><center><strong>Greased Bearings</strong></center></td>";
echo "<td><center><strong>Air Cleaned</strong></center></td>";
echo "<td><center><strong>SS Screw</strong></center></td>";
echo "<td><center><strong>Test Date</strong></center></td>";
echo "<td><center><strong>Pass</strong></center></td>";
echo "<td><center><strong>Fail</strong></center></td>";
echo "<td><center><strong>Tester</strong></center></td>";
echo "<td><center><strong>Final Check</strong></center></td>";
echo "<td><center><strong>Green Dot</strong></center></td>";
echo "<td><center><strong>Green Dot Check</strong></center></td>";
echo "<td><center><strong>Ship Date</strong></center></td>";
echo "<td><center><strong>Serial Number</strong></center></td>";
echo "</tr>";
while($row = $res->fetch_assoc()){
echo "<tr>";
echo "<td>"."<center>"."<i>".$row["Sales_Number"]."</i>"."</center>"."</td>";
echo "<td>"."<center>"."<i>".$row["Quantity"]."</i>"."</center>"."</td>";
echo "<td>"."<center>"."<i>".$row["Due_Date"]."</i>"."</center>"."</td>";
echo "<td>"."<center>"."<i>".$row["Burnished"]."</i>"."</center>"."</td>";
echo "<td>"."<center>"."<i>".$row["Grease"]."</i>"."</center>"."</td>";
echo "<td>"."<center>"."<i>".$row["Air"]."</i>"."</center>"."</td>";
echo "<td>"."<center>"."<i>".$row["Screw"]."</i>"."</center>"."</td>";
echo "<td>"."<center>"."<i>".$row["Test_Date"]."</i>"."</center>"."</td>";
echo "<td>"."<center>"."<i>".$row["Pass"]."</i>"."</center>"."</td>";
echo "<td>"."<center>"."<i>".$row["Fail"]."</i>"."</center>"."</td>";
echo "<td>"."<center>"."<i>".$row["Tester"]."</i>"."</center>"."</td>";
echo "<td>"."<center>"."<i>".$row["Final_Check"]."</i>"."</center>"."</td>";
echo "<td>"."<center>"."<i>".$row["Green_Dot"]."</i>"."</center>"."</td>";
echo "<td>"."<center>"."<i>".$row["Green_Dot_Check"]."</i>"."</center>"."</td>";
echo "<td>"."<center>"."<i>".$row["Ship_Date"]."</i>"."</center>"."</td>";
echo "<td>"."<center>"."<i>".$row["Serial_Number"]."</i>"."</center>"."</td>";
echo "</tr>";
}
echo "</table>";
} else {
echo "No Record Found!";
}
$connection->close();
?>

Update whole table in one query

If there's duplicated topic - please, forgive me, but i haven't found a solution yet. First of all, I've got one php file (update.php). This file contains:
<form action='update.php' method='post'>
<table border='1'>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "exchange";
$conn = new mysqli($servername, $username, $password, $dbname);
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_set_charset($conn,"utf8");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT ID, name, symbol, buy, sell FROM exchangevalues";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td><input type='text' name='ID' value='".$row["ID"]."'>";
echo "<td>".$row["name"]."</td>";
echo "<td>Buy: <input type='text' name='buy' value='".$row['buy']."'></td>";
echo "<td>Sell: <input type='text' name='sell' value='".$row['sell']."'></td>";
echo "</tr>";
}
}
$conn->close();
?>
<table>
There's 14 rows and 5 columns in my db. How do i update every row in columns 'buy' and 'sell' on 'click' (isset submit button) with new values? I've tried with ID[], buy[], but I've got problems with loop and i cannot handle it. I'm running MySQL on localhost, also i know that only last row will be updated without running loop, but still...
I am assuming you have corresponding php code to process the input, here is a sample of what you'll need to do for the HTML when creating the form:
if ($result->num_rows > 0) {
$i=0;
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td><input type='text' name='ID[" . $i . "]' value='".$row["ID"]."'>";
echo "<td>".$row["name"]."</td>";
echo "</tr>";
}
}
On the php side to process the input, you can do something like this:
$id_array = $_REQUEST["ID"];
foreach($id_array as $id){
//do insert with $id
}

Calculate and print passed (%) for each application for each table

Right now I could list each application/(Internal Team Names) name in table. Now I want to find out passed (%) from each table for each application and print on html page. Each "aw..." table has all application/(Internal Team Names) names and respective result also.
<html>
<?php
$Show= htmlspecialchars($_GET["Show"]);
$team= htmlspecialchars($_GET["team"]);
$tablename = htmlspecialchars($_GET["tablename"]);
$servername="localhost";
$username="root";
$password="";
?>
<?php
// Create connection with mySQL
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed:" . mysqli_connect_error());
}
//echo "Connected to mysql successfully";
//select a database to work with
mysqli_query( $conn , "USE plm_dashboard" );
Echo "<Body bgcolor="."white".">" ;
//echo $Show;
Echo "<br>";
Echo "<br>";
Echo "Applied Filters Are <br> ";
//echo "Team = $team "; echo "TableName= $tablename ";
$Local_team= "Reports";
if ($Show == "failed"){
$Result_status = "passed";
} else {
// in this case the value of the $Result_status is 'all'
$Result_status = "";
}
echo "Result = $Show <br>";
//$result =mysqli_query($conn ,"SELECT * FROM aw34_0605_tc1122 where Application="."'".$team."'");
$result = mysqli_query($conn ,"SELECT * FROM teamnames");
$result1 =mysqli_query($conn ,"SELECT * FROM aw34_0530_tc1015 (Select count(id) from aw34_0530_tc1015 where result ='passed' and application='ace')/(Select count(id) from aw34_0530_tc1015 where application='ace')*100");
//$result =mysqli_query($conn ,"SELECT * FROM ". $tablename );
//********** determine number of rows result set **********/
// $row_cnt = $result->num_rows;
//mysqli_query()
//echo "Showing $row_cnt Results ";
//echo "<table border="."1".">
echo "<table border="."1"."align="."right".">
<tr>
<th>ID</th>
<th>Internal Team Names</th>
<th width="."10px"." >Passed % for aw34_0530_tc1015</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['TeamInternalNames'] . "</td>";
echo "<td>" . $row['TeamName'] . "</td>";
// if ($row['Result'] == "passed") {
// echo "<td style="."background-color:#33FF38".">P</td>";
// } else {
// echo "<td style="."background-color:#FF334C".">F</td>";
// }
echo "</tr>";
}
echo "</table>";
ECHO "BYE";
mysqli_close($conn);
Echo "<Body>";
?>
</html>

Ajax call works in stand alone, but fails in wordpress

This code is working in stand alone, but when it gets put into wordpress it gives a series of errors related to the ajax call. The first problem it had was that it couldn't read the jquery from the remote source so I downloaded the file and have it read internal to the server, but these errors are still left and I'm not sure how to correct this. Note that in wordpress, I use a plugin that allows me to use [php] and [/php] to use php within wordpress.
<?php
$page_to_load = $_POST[view];
switch($page_to_load) {
case '':
echo "<script src=\"../scripts/jquery-3.2.0.min.js\"></script>";
echo "<script type=\"application/javascript\" src=\"../scripts/raid.js\"></script>";
echo "<font size=\"+3\" color=\"#FFFFFF\">Who should I host?<br>Please wait while channel is selected<br></font>";
echo "<font size=\"+2\">";
echo "<br><br>";
echo "<img src=\"../_images/ajax_loader_blue_350.gif\">";
$servername = "localhost";
$username = "";
$password = "";
$dbname = "";
$chanarray[] = null;
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT `TwitchNames` FROM TK_Members WHERE Validated='1' AND RaidMe='1'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
array_push($chanarray, $row['TwitchNames']);
}
} else {
echo "0 results";
}
array_splice($chanarray, 0, 1);
$conn->close();
echo "<script type=\"application/javascript\">";
echo "var channels = ". json_encode($chanarray);
echo "</script>";
echo "</font>";
echo "<form id=\"form\" method=\"post\">";
echo "<input type=\"hidden\" name=\"view\" value=\"page2\">";
echo "</form>";
break;
case 'page2':
echo "<font size=\"+3\" color=\"#FFFFFF\">Who should I host?<br>";
echo "Your channel to host is:<br></font>";
echo "<font size=\"+2\">";
echo "<br><br>";
$chans[] = null;
$test = $_POST['outArray'];
foreach ($test as $chan) {
$temparray = array(rand(),$chan);
array_push($chans, $temparray);
}
array_splice($chans,0,1);
sort($chans);
echo "".$chans[0][1]."";
echo "<br><br><br>";
echo "See All Live Channels";
echo "</font>";
break;
}
?>
Here are the errors:
Change $(document).ready(function() { to jQuery(function($){ because WordPress runs jQuery in no-conflict mode.
Source: https://wordpress.org/support/topic/simple-jquery-not-working/

How to delete a Mysql-Statement with a specific ID in a form?

I want to delete a entry in a database. So I made a (working!) table with the entries shown. So I want to delete the one entry, which I push a button "delete".
I thought I have to make a second php-file, but I don't know how to catch the ID of the selected entry. Here is my code of the first data:
while ($datensatz = mysqli_fetch_assoc($query))
{
$id = $datensatz['id'];
echo "<form action='PHP/deleteditadmin.php'><tr>";
echo "<input type='hidden' name='iid' value='$id' />";
echo "<td name='id'>" . $id . "</td>";
echo "<td>" . $datensatz['name'] . "</td>";
echo "<td>" . $datensatz['server'] . "</td>";
echo "<td><input type='submit' name='deleteadmin' value='Löschen' /></td>";
echo "<td><input type='submit' name='editadmin' value='Edit' /></td>";
echo "</tr>";
}
And here is the deleteditadmin.php
<?php
$hostname = 'localhost';
$dbname = 'XX';
$username = 'XX';
$password = 'XX';
$con = mysqli_connect($hostname, $username, $password) or DIE('Connection to host isailed, perhaps the service is down!');
mysqli_select_db($con, $dbname) or DIE('Database name is not available!');
if($_POST['deleteadmin'])
{
$idwert = $_POST['$iid'];
$remove= "DELETE FROM admins WHERE id='$idwert'";
$removequery = mysqli_query($con, $remove);
}
mysqli_close($con);
header("location:../adminlist.php");
?>
So, I need the second submit button for another function in the same php-file. I just want to delete only the one datarow in which I clicked the "delete" button.
Thanks
This should do it:
if($_POST['deleteadmin'])
{
$idwert = $_POST['iid'];
$remove= "DELETE FROM admins WHERE id='".$idwert."'";
$removequery = mysqli_query($con, $remove);
}

Categories