What did i miss out in my AJAX? - php

I would like to delete a picture using ajax, but im not sure where when wrong with my buttons.
this is my First page with the pictures.
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"> </script>
<script>
$(document).ready(function () {
$("#button").click(removecat);
});
function removecat() {
$("#resultDIV").load("removecat.php");
}
</script>
</head>
<body>
<div class="container">
<?php
require 'dbfunction.php';
require 'DBCategory.php';
$con = getDbConnect();
$categoryArr = getcategoryArrCon($con, "STATUS_ACTIVE");
foreach ($categoryArr as $Name => $InfoArr) {
?>
<div class="col-md-3">
<div class="title">
<h3><?php echo $Name; ?></h3>
</div>
<img src="<?php echo "img/" . $InfoArr['image'] ?>" class="img-rounded" width="250" height="220">
<a class="btn btn-danger" id="button" value="Delete" name="delete">Delete</a>
<br /><br />
<div id="resultDIV">
</div>
</div>
<?php } ?>
</div>
</body>
This is my removecat.php page,
<?php
require 'dbfunction.php';
$con = getDbConnect();
if (mysqli_connect_errno($con)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
} else {
mysqli_query($con, "UPDATE category SET status = 1 WHERE categoryid = '$Name'"); //$name from Firstpage
if (mysqli_affected_rows($con) > 0) {
echo "You have successfully remove $Name."; //$name from Firstpage
} else {
echo "NO changes were made.";
}
mysqli_close($con);
}
?>
I am not very sure about how to channel the $name from first page to the removecat.php page. Can someone help?

Related

MySQL on PHP need 2 reloads to update the values

I made a message deleter button, but I need 2 reloads to appear the changes...
(The rest of the code work, so it's normal that I don't show you the rest of the code...)
<?php while($r = $replies->fetch()) { ?>
<div class="message" id="<?= $r['id'] ?>">
<div class="profile">
<img class="avatar" src="members/avatars/<?php if(empty(get_avatar($r['id_author']))) { echo "default.png"; } else { echo get_avatar($r['id_author']); } ?>" width="150" height="150">
<h3 style="text-align: center;"><?= get_username($r['id_author']) ?></h3>
</div>
<div class="content">
<div class="date">
<?= date('d F Y - g:iA', strtotime($r['date_hour_post'])) ?>
</div>
<br><br>
<?= htmlspecialchars_decode($r['content']) ?>
<form method="POST"><button name="delete<?= $r['id'] ?>">Test</button></form>
<?php
$test = "delete".$r['id'];
if(isset($_POST[$test])) {
$delete = $db->prepare('DELETE FROM f_messages WHERE id = ?');
$delete->execute(array($r['id']));
$success = "Your message was successfully removed !";
}
?>
</div>
</div>
<br>
<?php } ?>
UPDATE:
I added the deleting code at the top of my php code, and it's working, thanks to Ray Andison
By the way thanks to keidakida too; he helped me to find a solution to my value problem. (And I think he don't know that)
Your form doesn't contain any data (the id to be deleted) or action (page to submit data to)?
<form method="POST" action="thispage.php">
<input id="test" name="test" type="hidden" value="<?= $r['id'] ?>">
<input type="submit">
</form>
UPDATED:
<?
if(isset($_POST[id])) {
$delete = $db->prepare('DELETE FROM f_messages WHERE id = ?');
$delete->execute(array($_POST[id]));
$success = "Your message was successfully removed !";
}
while($r = $replies->fetch()){
echo '
<div class="message" id="'.$r[id].'">
<div class="profile">
<img class="avatar" src="members/avatars/';
if(empty(get_avatar($r[id_author]))){
echo "default.png";
}else{
echo get_avatar($r[id_author]);
}
echo '
" width="150" height="150">
<h3 style="text-align:center;">
'.get_username($r[id_author]).'
</h3>
</div>
<div class="content">
<div class="date">
'.date('d F Y - g:iA', strtotime($r[date_hour_post])).'
</div>
<br>
<br>
'.htmlspecialchars_decode($r[content]).'
<form method="POST" action="thispage.php">
<input id="id" name="id" type="hidden" value="'.$r[id].'">
<input type="submit">
</form>
</div>
</div>';
}
?>
This is how I would code this, you need to change the action="thispage.php" to be the name of itself so it posts to itself, replace with the actual name of your php file
It is because the delete PHP code is at the bottom. Actions such as delete should be at the top of the HTML or while loops before presenting the data. SO try this:
<?php
if(isset($_POST["delete"])) {
$delete = $db->prepare('DELETE FROM f_messages WHERE id = ?');
$delete->execute(array($_POST['delete']));
$success = "Your message was successfully removed !";
}
while($r = $replies->fetch()) { ?>
<div class="message" id="<?= $r['id'] ?>">
<div class="profile">
<img class="avatar" src="members/avatars/<?php if(empty(get_avatar($r['id_author']))) { echo "default.png"; } else { echo get_avatar($r['id_author']); } ?>" width="150" height="150">
<h3 style="text-align: center;"><?= get_username($r['id_author']) ?></h3>
</div>
<div class="content">
<div class="date">
<?= date('d F Y - g:iA', strtotime($r['date_hour_post'])) ?>
</div>
<br><br>
<?= htmlspecialchars_decode($r['content']) ?>
<form method="POST">
<button type="button" name="delete" value="<?php echo $r['id']; ?>">Test</button>
</form>
</div>
</div>
<br>
<?php
}
?>
But you can do the same functionality without any page reload. Check AJAX PHP
Since it would be better and easier with AJAX, this is how it goes:
main.php
<?php
while ($r = $replies->fetch()) { ?>
<div class="message" id="<?= $r['id'] ?>">
<?php echo htmlspecialchars_decode($r['content']) ?>
<button onclick="delete('<?php echo $r['id']; ?>')">Delete</button>
</div>
<br>
<?php } ?>
<script>
function delete(id) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
alert("Delete successfully");
location.reload();
}
};
xmlhttp.open("POST", "delete.php", true);
// Mandatory for simple POST request
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
// Send id you want to delete
xmlhttp.send("id=" + id);
}
</script>
And make another PHP file name is delete.php like this:
<?php
include 'YOUR_DB_Connect.php';
if(isset($_POST["delete"])) {
$delete = $db->prepare('DELETE FROM f_messages WHERE id = ?');
$delete->execute(array($_POST['delete']));
}
?>

PHP MYSQLI AJAX Load On Scroll Not Working

I have code for loading more content on button click. And I have another for loading on scroll. The code for the button loads fine. The code for scroll duplicates the new loaded content like 3 times. Both codes use the same php load file. All are listed below. What is wrong with the scroll code? All help is appreciated. FYI, I am not interested in Jquery infinite scroll, or any other plugin. Thanks!
LOAD ON CLICK WITH BUTTON
<!DOCTYPE html>
<html>
<head>
<title>Load More Button</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
</head>
<body>
<div class="container">
<div>
<h2 align="center">Load More Button</h2>
<div style="height:10px;"></div>
<div id="load-content">
<?php
$lastid = '';
include('connection.php');
$query = mysqli_query($connection, "select * from transactions order by id asc limit 5");
while ($row = mysqli_fetch_array($query)) {
?>
<div>
<div>
<?php echo $row['id']; ?>
<br>
<?php echo $row['description']; ?>
<br>
<?php echo $row['promorefnum']; ?>
<br><br>
</div>
</div>
<?php
$lastid = $row['id'];
}
?>
<div id="remove">
<div style="height:10px;"></div>
<div>
<div>
<div id="load-more" data-id="<?php echo $lastid; ?>">Load More</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script>
$(document).ready(function() {
$(document).on('click', '#load-more', function() {
var lastid = $(this).data('id');
$('#load-more').html('Loading...');
$.ajax({
url: "load-data.php",
method: "POST",
data: {
lastid: lastid,
},
success: function(data) {
if (data != '') {
$('#remove').remove();
$('#load-content').append(data);
} else {
$('#load-more').html('End Of Data');
}
}
});
});
});
</script>
</body>
</html>
LOAD MORE ON SCROLL
<!DOCTYPE html>
<html>
<head>
<title>Load More Scroll</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
</head>
<body>
<div class="container">
<div>
<h2 align="center">Load More Scroll</h2>
<div style="height:10px;"></div>
<div id="load-content">
<?php
$lastid = '';
include('connection.php');
$query = mysqli_query($connection, "select * from transactions order by id asc limit 11");
while ($row = mysqli_fetch_array($query)) {
?>
<div>
<div>
<?php echo $row['id']; ?>
<br>
<?php echo $row['description']; ?>
<br>
<?php echo $row['promorefnum']; ?>
<br><br>
</div>
</div>
<?php
$lastid = $row['id'];
}
?>
<div id="remove">
<div style="height:10px;"></div>
<div>
<div>
<div id="load-more" data-id="<?php echo $lastid; ?>"></div>
</div>
</div>
</div>
</div>
</div>
</div>
<script>
$(document).ready(function() {
window.onscroll = function() {
if ((window.innerHeight + window.pageYOffset) >= document.body.offsetHeight - 2) {
var lastid = $('#load-more').data('id');
$('#load-more').html('Loading...');
$.ajax({
url: "load-data.php",
method: "POST",
data: {
lastid: lastid,
},
success: function(data) {
if (data != '') {
$('#remove').remove();
$('#load-content').append(data);
} else {
$('#load-more').html('End Of Data');
}
}
});
}
}
});
</script>
</body>
</html>
PHP CODE / LOAD FILE
<?php
sleep(1);
include('connection.php');
if (isset($_POST['lastid'])) {
$lastid = $_POST['lastid'];
$query = mysqli_query($connection, "select * from transactions where id > '$lastid' order by id asc limit 10");
if (mysqli_num_rows($query) > 0) {
while ($row = mysqli_fetch_array($query)) {
?>
<div>
<?php echo $row['id']; ?>
<br>
<?php echo $row['description']; ?>
<br>
<?php echo $row['promorefnum']; ?>
<br><br>
</div>
<?php
$lastid = $row['id'];
}
?>
<div id="remove">
<div style="height:10px;"></div>
<div>
<div>
<div id="load-more" data-id="<?php echo $lastid; ?>">Load More</div>
</div>
</div>
</div>
<?php
}
}
?>
Try this check
<head>
<title>Load More Scroll</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
</head>
<body>
<div class="container">
<div>
<h2 align="center">Load More Scroll</h2>
<div style="height:10px;"></div>
<div id="load-content">
<?php
$lastid = '';
include('connection.php');
$query = mysqli_query($connection, "select * from transactions order by id asc limit 11");
while ($row = mysqli_fetch_array($query)) {
?>
<div>
<div>
<?php echo $row['id']; ?>
<br>
<?php echo $row['description']; ?>
<br>
<?php echo $row['promorefnum']; ?>
<br><br>
</div>
</div>
<?php
$lastid = $row['id'];
}
?>
<div id="remove">
<div style="height:10px;"></div>
<div>
<div>
<div id="load-more" data-id="<?php echo $lastid; ?>"></div>
</div>
</div>
</div>
<span id="loading">Loading...</span>
</div>
</div>
</div>
<script>
$(document).ready(function() {
$("#loading").hide();
window.onscroll = function() {
if ((window.innerHeight + window.pageYOffset) >= document.body.offsetHeight - 2) {
var lastid = $('#load-more').data('id');
if($("#loading").css('display') == 'none') {
$("#loading").show();
$.ajax({
url: "load-data.php",
method: "POST",
data: {
lastid: lastid,
},
success: function(data) {
if (data != '') {
$('#remove').remove();
$('#load-content').append(data);
$("#loading").hide();
} else {
$('#load-more').html('End Of Data');
}
}
});
}
}
}
});
</script>
</body>
</html>

How Do I Create a Vanity URL After Submit

Hello so I have a site setup to where users can submit projects and it adds it to a upload folder and the database for user download anyway I want to have it so once a user submits a new project it creates a URL like “sitename.com/projects.html?projectname” or something like that. My code is below.
P.S Everything on my site works just need to learn how to create the url.
projects.html: Mainly used to display the recent projects.
<?php include("includes/header.php"); ?>
<?php
include_once 'dbconnect.php';
// fetch files
$sql = "select filename, title, description from tbl_files LIMIT 4";
$result = mysqli_query($con, $sql);
?>
<div id="container">
<div class="wrapper">
<div id="content">
<h2>Recent Projects <button style="float: right;">New Project</button></h2>
<p><table class="table table-striped table-hover">
<thead>
<tr>
<th>#</th>
<th>File Name</th>
<th>Description</th>
<th>Download</th>
</tr>
</thead>
<tbody>
<?php
$i = 1;
while($row = mysqli_fetch_array($result)) { ?>
<tr>
<td><?php echo $i++; ?></td>
<td><?php echo $row['title']; ?></td>
<td><?php echo $row['description']; ?></td>
<td><a href="uploads/<?php echo $row['filename']; ?>" download>Download</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</p>
<div id="column">
<div class="holder">
<h2>Welcome!</h2>
<ul id="latestnews">
<li class="last"> <p><?php
session_start();
include_once "vendor/autoload.php";
$page = new membership\Page(1);
if ($page->isValid() == true){
echo "Hello " . $_SESSION["username"] . "!<br /><br />
<a href='logout.html'>Logout</a>\n";
} elseif ($page->isValid() == false) { echo "<center>Please <a href='login.php'>Log in</a> to share projects.<br /> <a href='register.php'>Need A Account?</a></center>";}
?><br />
</p>
<br /></p>
</li>
</ul>
</div>
</div>
<br class="clear" />
</div>
</div>
<?php
error_reporting(E_ALL & ~E_NOTICE);
include('includes/memberlistconfig.php');
// call this file only after database connection
require_once 'functions.php';
?>
<div id="container">
<div class="wrapper">
<div id="content">
<h2>Categories</h2>
<p>
<div class="height20"></div>
<?php echo $emsg; ?>
<article>
Click on one of the categories to see what’s inside.
<ul>
<?php
$res = fetchCategoryTreeList();
foreach ($res as $r) {
echo $r;
}
?>
</ul>
</article>
</div></p>
<br class="clear" />
</div>
</div>
<?php include("includes/footer.php"); ?>
new-project.html: Allows used to upload a new project.
<?php
include_once('includes/header.php'); ?>
<?php
include_once 'dbconnect.php';
// fetch files
$sql = "select filename from tbl_files";
$result = mysqli_query($con, $sql);
?>
<?php
session_start();
include_once "vendor/autoload.php";
$page = new membership\Page(1);
if ($page->isValid() == true) {
?>
<div id="container">
<div class="wrapper">
<div id="content">
<h2>New Project</h2>
<p><center>
<form action='upload.php' method='post' enctype='multipart/form-data'>
<legend>Select File to Upload:</legend>
<div class='form-group'>
Title: <br /><input type='text' name='title' maxlength="255"/><br /><br />
Description: <br /><textarea type='text' name='description' maxlength="2000"></textarea><br /><br />
<input type='file' name='file1' />
</div>
<div class='form-group'><br />
<input type='submit' name='submit' value='Upload' class='btn btn-info'/>
</div>
<?php if (isset($_GET['st'])) { ?>
<div class='alert alert-danger text-center'>
<?php
if ($_GET['st'] == "success") {
echo "File Uploaded Successfully!";
} else {
echo 'Invalid File Extension!';
}
?>
</div>
<?php } ?>
</form></center>
</p><?php } ?>
<br /></div>
</p>
<div id="column">
<div class="holder">
<h2>Project Upload Rules</h2>
<ul id="latestnews">
This is this rules you must follow for uploading a project.<br /><br />
- You must own the project / script.<br />
- Must be 100% clean / safe.<br />
- Code must be easy to read.<br />
- No outdated code.<br />
<br />
If you don’t follow the rules your account who be banned.
<br />
</p>
<br /></p>
</li>
</ul>
</div>
</div>
<br class="clear" />
</div>
</div>
<?php include_once('includes/footer.php'); ?>
upload.php: This file uploads the info to the database.
<?php include('dbconnect.php'); ?>
<?php
//check if form is submitted
if (isset($_POST['submit']))
{
$filename = $_FILES['file1']['name'];
//upload file
if($filename != '')
{
$ext = pathinfo($filename, PATHINFO_EXTENSION);
$allowed = ['zip', 'rar', 'php', 'html', 'sql'];
//check if file type is valid
if (in_array($ext, $allowed))
{
// get last record id
$sql = 'select max(id) as id from tbl_files';
$result = mysqli_query($con, $sql);
if (count($result) > 0)
{
$row = mysqli_fetch_array($result);
$filename = ($row['id']+1) . '-' . $filename;
}
else
$filename = '1' . '-' . $filename;
//set target directory
$path = 'uploads/';
$created = #date('Y-m-d H:i:s');
move_uploaded_file($_FILES['file1']['tmp_name'],($path . $filename));
$title = '';
if(!empty($_POST['title']))
{
$title = mysqli_real_escape_string($con, $_POST['title']);
}
$description = '';
if(!empty($_POST['description']))
{
$description = mysqli_real_escape_string($con, $_POST['description']);
}
// insert file details into database
$sql = "INSERT INTO tbl_files(filename, created, title, description) VALUES('$filename', '$created', '$title', '$description')";
mysqli_query($con, $sql);
header("Location: new-project.html?st=success");
}
else
{
header("Location: new-project.html?st=error");
}
}
else
header("Location: new-project.html");
}
?>

echo the whole post from a PHP page using AJAX

I've created a search using Ajax PHP..Anything is going good but when i try to echo the whole post in my search page from PHP script.it doesn't work.Inclucding the PHP script in the page showing all the posts without clicking the specific.
Is there any way to echo using ajax without including the PHP script ?
This is my search.php:
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#search').keyup(function(){
var sea = $('#search').val();
$.get('test.php',{getsearch:sea},function(data){
$('#result').show().html(data);
});
});
$('#search').blur(function(){
$('#result').hide();
});
});
</script>
</head>
<body>
<div class="cover">
<div class="btns-holder">
<input type="search" name="s" placeholder="Search for something.." id="search" autocomplete="off" /><input type="submit" name="sub" value="" id="sub" />
</div>
</div>
<div id="result">
</div>
<?php
$post = #$_GET['p'];
$que = "SELECT * FROM `search` WHERE `id`='$post'";
$run = mysqli_query($con,$que);
while($row=mysqli_fetch_array($con,$que)):
?>
<img src="<?php echo $row['image']; ?>" widrh="500px" height="450px" style="float: left;"></img>
<h2 style="font-family: sans-serif;"><?php echo $row['title']; ?></h2>
<?php endwhile; ?>
</body>
This is test.php from where i want to echo the data:
<?php
$host = "localhost";
$user = "root";
$pass = "";
$db = "ajax_search";
$con = mysqli_connect($host, $user, $pass, $db);
if(mysqli_connect_errno())
{
echo "connection failed".mysqli_connect_error();
}
$search = #$_GET['getsearch'];
$que = "SELECT * FROM `search` WHERE `title` LIKE '%$search%'";
$run = mysqli_query($con,$que);
while($row=mysqli_fetch_assoc($run)):
?>
<a href="search.php?p=<?php echo $row['id']; ?>">
<div class="container">
<img src="<?php echo $row['image']; ?>" width="50px" height="45px"></img>
<h3><?php echo $row['title']; ?></h3></br>
</div>
</a>
<?php endwhile; ?>

Why the query runs again when i reload the page and can not click on the post button to insert any data

I am making a medical related site in php. When a patient logged in and he did not post any thing but he refreshes the page the query retrieve the old records again which is already inserted kindly help
This is my code for
Patient_dashboard.php
<?php
session_start();
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$ufn = "";
$uln = "";
$q2 = "";
$post="";
$query;
$query2="";
$connection = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $connection )
{
die('Could not connect: ' . mysql_error());
}
$database = mysqli_select_db($connection,"medical_network");
if (!$database) {
die("Database selection failed: " . mysql_error());
}
if(!isset($_SESSION['username'])) {
header('Location: index.php');
}
require_once("Functions/functions.php");
$posts = get_user_posts($_SESSION['id']);
$uploadDir = 'Reports Images/'; //Image Upload Folder
if(isset($_POST['submit'])&& !empty($_POST['submit'])){
$fileName = $_FILES['image']['name'];
$tmpName = $_FILES['image']['tmp_name'];
$fileSize = $_FILES['image']['size'];
$fileType = $_FILES['image']['type'];
$filePath = $uploadDir . $fileName;
$filename = $fileName;
$ext = pathinfo($filename, PATHINFO_EXTENSION);
$date = date_create();
$fileName= date_timestamp_get($date).".".$ext;
$filePath = $uploadDir . $fileName;
$result = move_uploaded_file($tmpName, $filePath);
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}
$post = mysqli_real_escape_string($connection,($_POST['np_text']));
//$date = date("Y/m/d");
date_default_timezone_set('Asia/Karachi');
$date = date('Y-m-d H:i:s');
//echo $date;
$query = "INSERT INTO post ";
$query .= "(p_pic,p_content,p_date,u_id) VALUES (";
$query .= "'{$filePath}','{$post}','{$date}','{$_SESSION['id']}');";
//echo $query . " " .$date;
$result_set = mysqli_query($connection,$query);
if (!$result_set) {
die("Database query failed: " . mysqli_error($connection));
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Medical Network</title>
<link href="css/custom.css" rel="stylesheet" />
<script>
function performClick(node) {
var evt = document.createEvent("MouseEvents");
evt.initEvent("click", true, false);
node.dispatchEvent(evt);
}
</script>
</head>
<body>
<div class="topmenu">
<div class="search">
<form>
<input type="text" placeholder="Search" value="">
</form>
</div>
<div id='cssmenu'>
<ul>
<li><a href='#'><span>Home</span></a></li>
<li><a href='#'><span>Profile</span></a></li>
<li><a href='logout.php'><span>Logout</span></a></li>
</ul>
</div>
</div>
<div class="main">
<div class="left-colum">
<div class="profileinfo">
<div class="profilepic">
<?php echo "<img class='getPic' src='{$_SESSION['pic']}' />" ?>
</div>
<div class="personal-info">
<ul>
<li><a href='#'><span><?php echo $_SESSION['fname']." ".$_SESSION['lname']; ?></span></a></li>
<li><a href='#'><span>Edit Profile</span></a></li>
</ul>
</div>
</div>
<div class="importantlinks">
<h4>Important Links</h4>
<ul>
<li><a href='#'><span>Make Appointment</span></a></li>
<li><a href='#'><span>Search Doctor</span></a></li>
<li><a href='logout.php'><span>Logout</span></a></li>
</ul>
</div>
<div class="pages">
<h4>Pages</h4>
</div>
</div>
<div class="middle-colum">
<div id="feeds">
<div class="new-post">
<ul>
<form method="post" action="patient-dashboard.php" enctype="multipart/form-data">
<li><label for="newpostfield"><img src="images/status.png" width="14" height="14"> Update Status</label></li>
<li> <img src="images/photo.png" width="14" height="14"> Add Photo
</li>
<input type="file" id="myFile" name="image" size="4
000000" accept="image/gif, image/jpeg, image/x-ms-bmp, image/x-png" />
</ul>
<br />
<textarea rows="1" cols="40" id="newpostfield" name="np_text" placeholder="Whats on your mind ?" required="required" ></textarea>
<input type="submit" value="Post" name="submit" id="postbtn"/>
</form>
</div><!--End of feed-item -->
<br />
<div class="posts-feed">
<h3 id="postsheading" class="post-heading">Posts</h3>
<?php while($post = mysqli_fetch_array($posts)){
$counter = 0;
?>
<div class="post">
<div class="poster-pic">
<?php echo "<img src='{$_SESSION['pic']}' height='60' width='60' />" ?>
</div><!--End of poster-pic -->
<div class="post-content">
<div id="poster-name"><?php echo $_SESSION['fname']." ".$_SESSION['lname']; ?><span></span></div>
<div id="content">
<p id="post-text">
<?php
echo $post['p_content']."<br/>";
$img = $post['p_pic'];
echo "<a href='#'> <img src='{$post['p_pic']}' height='300' width='300' >"
?>
</p>
</div><br />
<div id="post-responses" class="top-border">
Comment
</div>
</div><!--End of post-content -->
</div><!--End of post -->
<?php $counter++;
} ?>
</div><!--End of feeds -->
</div>
<div class="right-colum">
<div class="heading">
<h4>Recommended Pages</h4>
</div>
</div>
</body>
</html>
This is function.php where i am retrieving the records.
<?php
function get_user_posts($id) {
global $connection;
$query2 = "SELECT p_pic, p_content,u_id ";
$query2 .= "FROM post ";
$query2 .= "WHERE u_id= " . $id ." ";
$query2 .= "ORDER BY p_id DESC ";
$query2 .= "LIMIT 5";
$result_set1 = mysqli_query( $connection,$query2);
if (!$result_set1) {
die("Database query failed: " . mysqli_error($connection));
}
// REMEMBER:
// if no rows are returned, fetch_array will return false
if ($result_set1) {
return $result_set1;
} else {
return NULL;
die("query faild..... in get post");
}
}
?>
You are calling $posts = get_user_posts($_SESSION['id']); outside the if statement so it is showing the records as on every reload it is fetching the data. if you want to display the records only if it is posted then put it inside the if statement
if(isset($_POST['submit'])&& !empty($_POST['submit'])){
//your code
$posts = get_user_posts($_SESSION['id']);
}
and please do check the if the variables are available or not.
Please change input name attribute
it should be:
use any name Except submit
also change
if(isset($_POST['submit'])&& !empty($_POST['submit'])){

Categories