Pagination $_POST loading on one page? - php

My problem is to get the pagination to work correctly.
Since I have a $page = $POST_["zain"] for receiving the records there is a problem with the pagination that it shows only result of first page.
Here is my code
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="zain.php" method="post">
Topic: <input type="text" name="topic"><br />
<br />
Name: <input type="text" name="name"><br /><br />
Attendance: <input type="text" name="attendance"><br />
<br />
<input type="reset" name="reset">
<input type="submit" id = "go" name="submit" value="Go">
</form>
<?php
$user = 'root';
$password = 'zz224466';
$db = 'Zain';
// Create connection
$conn = mysqli_connect('localhost', $user, $password, $db);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "";
mysqli_select_db($conn, "zain");
//$sql = "CREATE TABLE Lectures(Topic varchar(20), Name varchar(20), Attendence int)";
$sqli = "INSERT INTO lectures(Topic , Name , Attendence) VALUES('$_POST[topic]','$_POST[name]','$_POST[attendance]')";
mysqli_query($conn, $sqli);
////////////////////////// For print DATABASE on the screen using PAGINATION ////////////////////////////////////////
mysqli_select_db($conn, "zain");
if(isset($_GET['page'])){
if($page=="" || $page=="1")
{
$page1 = 0;
}
else
{
$page1 = ($page*5)-5;
}
}
$dataBase = "SELECT * FROM lectures limit $page1,5";
$print = mysqli_query($conn, $dataBase);
while ($record = mysqli_fetch_array($print))
{
echo $record['Topic'];
echo "</br>";
}
$selectDatabase = "SELECT * FROM lectures";
$res1 = mysqli_query($conn, $selectDatabase);
$countRow = mysqli_num_rows($res1);
$a = $countRow/5;
$a = ceil($a);
echo "<br>" . $a;
echo "</br> </br>";
for($b=1; $b<=$a; $b++)
{
?><?php echo $b . " ";?><?php
}
mysqli_close($conn);
?>
</body>
</html>

on the top of your page.
$zain = !empty($_POST["zain"])?$_POST["zain"]:(!empty($_GET["zain"])?$_GET["zain"]:false);
if(!$zain) die("zain is empty");
then replace all other $_POST["zain"] to $zain
On the link part
for($b=1; $b<=$a; $b++)
{
?><?php echo $b . " ";?><?php
}

Related

Wordpress PHP FIle not running

I have a custom search form on my website with this code:
<center>
<form action="orderstatus.php" method="POST">
<input type="text" name="OrderLineNumber" value="">
<br>
<input type="submit" value="Submit">
</form>
</center>
And although I call orderstatus.php to action . When I press the submit button I get this Url : blablabla.de/action_page.php?OrderLineNumber=+23563623
This means instead of running orderstatus.php it's running action_page.php
My orderstatus.php is trying to print One Select Row from database like this :
<!DOCTYPE html>
<html>
<body>
<?php
$servername = "*******";
$username = "*******";
$password = "*******";
$dbname = "********";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM OrderStatus";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc() & fetch_assoc() = $_POST["name"] ) {
echo "<br> Order Line Number: ". $row["OrderLineNumber"]. " - Date Started: ". $row["Date Started"]. " " . $row["Status"] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
</body>
</html>

Upload file in a form containing multiple textfields using PHP

I'm trying to figure out how to upload a file into the database where that form contains multiple textfields. I uploaded a BLOB field into the database. So as I try to search the field using the ID number, it will retrieve me the values associated with it. Which works fine, so I added the function of being able to upload a file into that specific id number. I get all sorts of errors and I would like to have an assistance with it. Anyone care to help out? Here are the codes:
<?php
$host = "localhost";
$user = "root";
$password ="";
$database = "ntmadb";
$id = "";
$firstname = "";
$lastname = "";
$username = "";
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
// connect to mysql database
try{
$connect = mysqli_connect($host, $user, $password, $database);
} catch (mysqli_sql_exception $ex) {
echo 'Error';
}
// get values from the form
function getPosts()
{
$posts = array();
$posts[0] = $_POST['id'];
$posts[1] = $_POST['firstname'];
$posts[2] = $_POST['lastname'];
$posts[3] = $_POST['username'];
return $posts;
}
// Search
if(isset($_POST['search']))
{
$data = getPosts();
$search_Query = "SELECT * FROM members WHERE id = $data[0]";
$search_Result = mysqli_query($connect, $search_Query);
if($search_Result)
{
if(mysqli_num_rows($search_Result))
{
while($row = mysqli_fetch_array($search_Result))
{
$id = $row['id'];
$firstname = $row['firstname'];
$lastname = $row['lastname'];
$username = $row['username'];
}
}else{
echo 'No Data For This Id';
}
}else{
echo 'Result Error';
}
}
// Edit
if(isset($_POST['update']))
{
$data = getPosts();
$update_Query = "UPDATE `members` SET `firstname`='$data[1]',`lastname`='$data[2]',`username`='$data[3]' WHERE `id` = $data[0]";
try{
$update_Result = mysqli_query($connect, $update_Query);
if($update_Result)
{
if(mysqli_affected_rows($connect) > 0)
{
echo 'Data Updated';
}else{
echo 'Data Not Updated';
}
}
} catch (Exception $ex) {
echo 'Error Update '.$ex->getMessage();
}
}
<!--UPLOADUPLOADUPLOADUPLOADUPLOADUPLOADUPLOADUPLOADUPLOADUPLOADUPLOADUPLOADUPLOAD -->
// Check if a file has been uploaded
if(isset($_FILES['uploaded_file'])) {
// Make sure the file was sent without errors
if($_FILES['uploaded_file']['error'] == 0) {
// Connect to the database
$dbLink = new mysqli('localhost', 'root', '', 'ntmadb');
if(mysqli_connect_errno()) {
die("MySQL connection failed: ". mysqli_connect_error());
}
// Gather all required data
$data = $dbLink->real_escape_string(file_get_contents($_FILES ['uploaded_file']['tmp_name']));
// Create the SQL query
$query = "
INSERT INTO `members` (
`data`
)
VALUES (
'{$data}' NOW()
)";
// Execute the query
$result = $dbLink->query($query);
// Check if it was successfull
if($result) {
echo 'Success! Your file was successfully added!';
}
else {
echo 'Error! Failed to insert the file'
. "<pre>{$dbLink->error}</pre>";
}
}
else {
echo 'An error accured while the file was being uploaded. '
. 'Error code: '. intval($_FILES['uploaded_file']['error']);
}
// Close the mysql connection
$dbLink->close();
}
else {
echo 'Error! A file was not sent!';
}
?>
and here is the html file:
<!DOCTYPE Html>
<html>
<head>
<title>PHP INSERT UPDATE DELETE SEARCH</title>
</head>
<body>
<form action="index4.php" method="post" enctype="multipart/form-data" >
<input type="number" name="id" placeholder="Id" value="<?php echo $id;?>"><br><br>
<input type="text" name="firstname" placeholder="First Name" value="<?php echo $firstname;?>"><br><br>
<input type="text" name="lastname" placeholder="Last Name" value="<?php echo $lastname;?>"><br><br>
<input type="text" name="username" placeholder="User Name" value="<?php echo $username;?>"><br><br>
<div>
<p>
<!-- Input For Edit Values -->
<input type="submit" name="update" value="Update">
<!-- Input For Find Values With The given ID -->
<input type="submit" name="search" value="Find">
</p>
<p>
<input type="file" name="uploaded_file">
<br>
<input type="submit" value="Upload file">
</p>
</div>
</form>
</body>
</html>
Thanks to anyone who can provide me with help. :)

find differenence in 2 mysql db from same server having same tables using php

program is to find difference in 2 db from same server having same tables and to display the difference in tables using php. Here I am finding difficulty to select dbnames ,i was trying to get dbnames through form method .
<html>
<head>
<title>Connecting db</title>
<style>
table, th, td {
border: 1px solid black;
}
</style>
</head>
<body>
<form method="post" action="" >
<label>db1 :</label>
<input type="text" name="db1 name">
<input type="submit" name="submit" value="submit">
<br><br>
<label>db2 :</label>
<input type="text" name="db2 name">
<input type="submit" name="submit" value="submit">
</form>
<?php
if(isset($_POST["submit"])){$servername = "servername";
$username = "username";
$password = "password";
$dbname = "db";
$db1 = $_POST["db1 name"];
$db2 = $_POST["db2 name"];
// Create connection
$conn = new mysqli($servername, $username, $password,$dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT application_id,application_name\n"
. "FROM "($db1)".applications\n"
. "WHERE(\n"
. " application_id NOT IN \n"
. " (SELECT application_id FROM "($db2)".applications)\n"
. " )\n"
. "GROUP BY application_id LIMIT 0, 30 ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table><tr><th>APPLICATION ID</th><th>APPLICATION Name</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>".$row["application_id"]."</td><td>".$row["application_name"].."</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
}
?>
</body>
</html>
Try this kind of connection
$conn = mysqli_connect("hostname","username","password");
mysqli_select_db("db1",$conn);
mysqli_select_db("db2",$conn);

how to display array values in php?

im performing search from the database and displaying the result in the checkbox format in which when i select the checkbox the respectively values should be inserted in the another table which is in (a.php) since the search result have multiple values im storing the values in array but when im trying print the array with echo $_POST['friend']; it showing result as "Array" anyone help me how can i display the variables stored in array
<form method="post">
<div class="form-group">
Name
<br/>
<input type="text" class="form-control" name="name" />
</div>
<div class="form-group">
Email <br/>
<input type="text" class="form-control"name ="email" />
</div>
<div class="form-group">
Qualification<br/>
<input type="text" class="form-control" name ="qualify" />
</div>
<input type="submit" value="Search" />
</form>
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$name=$_POST['name'];
$email=$_POST['email'];
$qualification=$_POST['qualify'];
$sql = "SELECT * FROM form WHERE Name ='$name' OR EmailAddress = '$email' OR Qualification = '$qualification' ";
$result=$conn->query($sql);
if(!empty($_POST)) {
if($result->num_rows === 0)
{
echo '<p style="margin-left:340px">no records</p>';
}
}
while($row = $result->fetch_assoc())
{
//$_SESSION["snum"]=$row['sno'];
//$_SESSION["nam"]=$row['Name'];
//$_SESSION["quali"]=$row['Qualification'];
// $_SESSION["emai"]=$row['EmailAddress'];
echo '<br>';
echo '<form name="friend" action="a.php" method="post">';
echo '<input style="margin-left:340px;padding-bottom:10px" type="checkbox" name="friend[]"> user Details</input>';
echo '<br>';
echo '<br>';
echo '<div class="container" style="border-style:solid; border-width:medium;width: 550px;">';
echo '<br>';
echo 'Name: '.$row['Name'];
echo '<br /> EmailAddress: ' .$row['EmailAddress'];
echo '<br /> Qualification: '.$row['Qualification'];
echo '<br /> DOB: '.$row['DOB'];
echo '<br/>';
echo '<br/>';
echo '<br/>';
echo '<br/>';
echo '</div>';
echo '<br/>';
}
echo '<button type ="submit">invite</button>';
echo '</form>';
$conn->close();
?>
Try this:
1.The print_r(variable); function is used to print human-readable information about a variable.
2.The var_dump(variable); function is used to display structured information (type and value) about one or more variables.
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$count=count($_POST['friend']);
for($i=0; $i<$count; $i++)
{
$a=$_POST['friend'][$i];
//echo $a;
$sql = "SELECT Name,EmailAddress,Qualification FROM form WHERE sno='$a'";
$result=$conn->query($sql);
while($row = $result->fetch_assoc()){
$ab=$row['Name'];
$bc=$row['EmailAddress'];
$ca=$row['Qualification'];
echo $ab;
echo'<br/>';
echo $bc;
echo'<br/>';
echo $ca;
echo'<br/>';
$sql1="INSERT INTO arun ". "VALUES('$ab')";
$result1=$conn->query($sql1);
}
}
if (!$result) {
//$_SESSION['message10'] = '<p style="color:green;margin-left: 250px";>Your request has been send </p>';
//header("Location:send.php");
echo "not send";
}
else {
// $_SESSION['message11'] = '<p style="color:red;margin-left: 250px";>you have already send request to the user</p>';
//header("Location:new.php");
echo "send";
}
$conn->close();
?>

Displaying html form inputs on browser permanently after fetched from MySQL database?

I have used an isset() function on 'submit' to store and retrieve my html form inputs. However all my SQL data in my table only gets displayed after I clicked the submit function on the browser as I programmed it that way.
I would like to make it now so that even when I refresh the browser the html form input remains permanently and I do not have to click submit to fetch the entire table and display (it rather displays itself straight away).
Please show me how I can go about this?
below is my code:
<?php
require_once "connection.php";
if(isset($_POST['submit'])) {
$question = $_POST['question'];
$description = $_POST['description'];
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME );
if($conn->connect_error) {
die("connection error: " . $conn->connect_error);
} else {
echo "Submit button connected to database!";
}
}
if(isset($_POST['question']) && $_POST['description']) {
$sql = " INSERT INTO `ask` (question_id, question, description) VALUES
(NULL, '{$question}', '{$description}' ) ";
if($conn->query($sql)) {
echo "it worked";
} else {
echo "error: " . $conn->error;
exit();
}
$query = "SELECT * FROM `ask` ";
$result = $conn->query($query);
while($row = $result->fetch_assoc()) {
echo "<p> {$row['question']}</p>";
echo "<p> {$row['description']}</p>";
}
$conn->close();
}
?>
<!DOCTYPE html>
<html>
<head></head>
<body>
<div id="submitQuestion">
<form action="" method="post">
<input type="text" name="question"/>
<textarea name="description" rows="10" cols="20"></textarea>
<input type="submit" name="submit" value="ASK"/>
</form>
</div>
</body>
</html>
If I've correctly understood the question, you would like to always display all entries and add new data only when it's required. For this goal, you can connect and retrieve data independently from form submission, and insert new data only when required.
<?php
require_once "connection.php";
// Connect to database
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME );
if($conn->connect_error) {
die("connection error: " . $conn->connect_error);
}
// Insert new data if required
if(isset($_POST['submit']) && isset($_POST['question']) && isset($_POST['description']))
{
$question = $_POST['question'];
$description = $_POST['description'];
$sql = " INSERT INTO `ask` (question_id, question, description) VALUES
(NULL, '{$question}', '{$description}' ) ";
if(!$conn->query($sql)) {
echo "error during insert: " . $conn->error;
exit();
}
}
// Display data
$query = "SELECT * FROM `ask` ";
$result = $conn->query($query);
while($row = $result->fetch_assoc()) {
echo "<p> {$row['question']}</p>";
echo "<p> {$row['description']}</p>";
}
// Close database connection
$conn->close();
?>
<!DOCTYPE html>
<html>
<head></head>
<body>
<div id="submitQuestion">
<form action="" method="post">
<input type="text" name="question"/>
<textarea name="description" rows="10" cols="20"></textarea>
<input type="submit" name="submit" value="ASK"/>
</form>
</div>
</body>
</html>
<?php
require_once "connection.php";
if(isset($_POST['submit'])) {
$question = $_POST['question'];
$description = $_POST['description'];
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME );
if($conn->connect_error) {
die("connection error: " . $conn->connect_error);
} else {
echo "Submit button connected to database!";
}
}
if(isset($_POST['question']) && $_POST['description']) {
$sql = " INSERT INTO `ask` (question_id, question, description) VALUES
(NULL, '{$question}', '{$description}' ) ";
if($conn->query($sql)) {
echo "it worked";
} else {
echo "error: " . $conn->error;
exit();
}
}
$query = "SELECT * FROM `ask` ";
$result = $conn->query($query);
while($row = $result->fetch_assoc()) {
echo "<p> {$row['question']}</p>";
echo "<p> {$row['description']}</p>";
}
$conn->close();
?>
<!DOCTYPE html>
<html>
<head></head>
<body>
<div id="submitQuestion">
<form action="" method="post">
<input type="text" name="question"/>
<textarea name="description" rows="10" cols="20"></textarea>
<input type="submit" name="submit" value="ASK"/>
</form>
</div>
As I told you in the other question..

Categories