Echo something into div MySQL PHP - php

I am trying to select some data from a database and display that in HTML.
I thought the code below would work but it doesn't.
What code should I use instead?
And does the CSS still work that way?
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="se.css">
<title>Lotto</title>
</head>
<body>
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "lotto";
$db = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
$query = "SELECT id, vraag, AntwA, AntwB, AntwC, AntwD,id FROM vraag1";
$rows = mysqli_query($db, $query);
while($row = mysqli_fetch_assoc($rows))
{
echo "<div id='Vraag'>$row["vraag"];</div>";
}
?>
<div id="headerbg"></div>
<center>
<h1>Vraag 1</h1>
</center>
<center>
<div>
<img src="antw.png" id="img6">
</div>
<div id="plaatje">
<img id="img1" src="wip.jpg">
</div>
<div id="BGantA">
<img id="img2" src="antw.png">
</div>
<div id="antA">
Antwoord A
</div>
<div>
<img src="antw.png" id="img3">
</div>
<div>
Antwoord B
</div>
<div>
<img src="antw.png" id="img4">
</div>
<div>
Antwoord C
</div>
<div>
<img src="antw.png" id="img5">
</div>
<div>
Antwoord D
</div>
</center>
</body>
</html>

use this instead of your echo code
echo "<div id='".$row['id']."'>".$row['vraag']."</div>";
Hope it's help you.

Your syntax is wrong.Change your code to this:
while($row = mysqli_fetch_assoc($rows))
{
echo "<div id='Vraag'>".$row["vraag"]."</div>";
}
Note: To get different id for each div use a counter(you can use your table id).

its a good practice to check if your query does return results.
this while($row = mysqli_fetch_assoc($rows)) { echo "<div id='Vraag'>$row["vraag"];</div>"; } will output many div's with the same ID and ID needs to be different, so you need to have your ` before you start the loop.
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="se.css">
<title>Lotto</title>
</head>
<body>
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "lotto";
$db = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
$query = "SELECT id, vraag, AntwA, AntwB, AntwC, AntwD,id FROM vraag1";
$results = mysqli_query($db, $query);
if (mysqli_num_rows($results) > 0) {
// output data of each row?>
<div id="Vraag">
<?php
while($row = mysqli_fetch_assoc($results)) {
echo $row["vraag"];
}
} else {
echo "no results found";
}?>
</div>
?>
<div id="headerbg"></div>
<center>
<h1>Vraag 1</h1>
</center>
<center>
<div>
<img src="antw.png" id="img6">
</div>
<div id="plaatje">
<img id="img1" src="wip.jpg">
</div>
<div id="BGantA">
<img id="img2" src="antw.png">
</div>
<div id="antA">
Antwoord A
</div>
<div>
<img src="antw.png" id="img3">
</div>
<div>
Antwoord B
</div>
<div>
<img src="antw.png" id="img4">
</div>
<div>
Antwoord C
</div>
<div>
<img src="antw.png" id="img5">
</div>
<div>
Antwoord D
</div>
</center>
</body>
</html>

You have concatenate problem .
Replace the line
while($row = mysqli_fetch_assoc($rows))
{
echo "<div id='Vraag'>$row["vraag"];</div>";
}
with
$counter = 1;
while($row = mysqli_fetch_assoc($rows))
{
echo "<div id='Vraag_".$counter."' class='VraagClass'>".$row['vraag']."</div>";
}
I have added a counter to prevent generating duplicate div with the same Id.

Related

php pagination has some error

I'm trying to materialize pagination. I want to fetch like
$sql2 = "select * from phptest.memo order by num desc limit $start,$start+$scale";
and i used while paragraph to show memo and ripple (ripple database has parent field that what number of memo and it's order num primary field) but I think it seems like typed wrong code what should i do? It's image that I want make
Expected result:
Error:
<?php
session_start();
// echo "<a href='memo.php?page=$i'> $i </a>";
$scale=5; // page per writing
// start recored number newest writing is up
// $page 값에 다른 시작 레코드 넘버값, 가장 높은 글번호(최신글 부터 밑으로)
$start = ($page - 1) * $scale; //0,5,10 ...
if(!isset($_REQUEST["page"]))
{
$page = 1; // initialize seeing page
}else{
$page = $_REQUEST["page"];
}
require_once '../lib/dbconn.php';
$pdo = db_connect();
if(isset($_SESSION["userid"])){
$userid = $_SESSION["userid"];
}else{
$userid = "";
}
// start recored number newest writing is up
try{
//$sql = "select * from phptest.memo order by num desc";
$sql2 = "select * from phptest.memo order by num desc limit $start,$start+$scale";
$stmh = $pdo->query($sql2);
} catch (PDOException $ex) {`enter code here`
print "오류: ".$ex->getMessage();
}
$total_record = $stmh->rowCount();
$number = $total_record - $start;
// 전체 페이지 수 계산..
if ($total_record % $scale == 0)
$total_page = floor($total_record/$scale);
else
$total_page = floor($total_record/$scale) + 1;
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta charset="utf-8">
<link href="../css/common.css" rel="stylesheet" type="text/css" media="all">
<link href="../css/memo.css" rel="stylesheet" type="text/css" media="all">
</head>
<body>
<div id="wrap">
<div id="header">
<?php include "../lib/top_login2.php"; ?>
</div> <!-- end of header -->
<div id="menu">
<?php include "../lib/top_menu2.php"; ?>
</div> <!-- end of menu -->
<div id="content">
<div id="col1">
<div id="left_menu">
<?php
include "../lib/left_menu.php";
?>
</div>
</div>
<div id="col2">
<div id="title">
<img src="../img/title_memo.gif">
</div>
<?php
// if(isset($_SESSION["userid"])){
?>
<div id="memo_row1">
<form name="memo_form" method="post" action="insert.php">
<?php if(isset($_SESSION['nick'])){ ?>
<div id="memo_writer"><span >▷ <?=$_SESSION['nick'] ?> </span></div>
<?php }?>
<div id="memo1"><textarea rows="6" cols="95" name="content"></textarea></div>
<div id="memo2"><input type="image" src="../img/memo_button.gif"></div>
</form>
</div> <!-- end of memo_row1 -->
<?php // }
while ($row = $stmh->fetch(PDO::FETCH_ASSOC))
{
// $stmh = $pdo->query($sql2);
//$row[$i] = $row;
// $row = $stmh2->fetch(PDO::FETCH_ASSOC);
$memo_id = $row['id'];
$memo_num = $row['num'];
$memo_date = $row['regist_day'];
$memo_nick = $row['nick'];
$memo_content = $row['content'];
$memo_content = str_replace("\n", "<br>", $row['content']);
$memo_content = str_replace(" ", " ", $memo_content);
?>
<div id="memo_writer_title">
<ul>
<li id="writer_title1"><?= $memo_num ?></li> <!--article number -->
<li id="writer_title2"><?= $memo_nick ?></li>
<li id="writer_title3"><?= $memo_date ?></li>
<li id="writer_title4">
<?php
if($userid=="admin" || $userid==$memo_id)
echo "<a href='delete.php?num=$memo_num'>[삭제]</a>";
?>
</li>
</ul>
</div>
<div id="memo_content"><?= $memo_content ?>
</div>
<div id="ripple">
<div id="ripple1">덧글</div>
<div id="ripple2">
<?php
$sql3 = "select * from phptest.memo_ripple where parent=$memo_num";
$stmh3 = $pdo->query($sql3);
while ($row_ripple = $stmh3->fetch(PDO::FETCH_ASSOC))
{
$ripple_num = $row_ripple["num"];
$ripple_id = $row_ripple["id"];
$ripple_nick = $row_ripple["nick"];
$ripple_content = str_replace("\n", "<br>", $row_ripple["content"]);
$ripple_content = str_replace(" ", " ", $ripple_content);
$ripple_date = $row_ripple["regist_day"];
?>
<div id="ripple_title">
<ul>
<li><?= $ripple_nick ?> <?= $ripple_date ?></li>
<li id="mdi_del">
<?php
if($userid=="admin" || $userid==$ripple_id)
echo "<a href='delete_ripple.php?num=$ripple_num'>삭제</a>";
?>
</li>
</ul>
</div>
<div id="ripple_content"> <?= $ripple_content ?></div>
<?php
}
if(isset($_SESSION["userid"])){
?>
<form name="ripple_form" method="post" action="insert_ripple.php">
<input type="hidden" name="num" value="<?= $memo_num ?>">
<div id="ripple_insert">
<div id="ripple_textarea">
<textarea rows="3" cols="80" name="ripple_content"></textarea>
</div>
<div id="ripple_button"><input type="image" src="../img/memo_ripple_button.png"></div>
</div>
</form>
<?php } ?>
</div> <!-- end of ripple2 -->
<div class="clear"></div>
<div class="linespace_10"></div>
<?php
//$number--;
}
?>
<div id="page_num"> ◀ 이전
<?php
// page link num
// 게시판 목록 하단에 페이지 링크 번호 출력
for ($i=1; $i<=$total_page; $i++)
{
if ($page == $i) // ���� ������ ��ȣ ��ũ ����
{
echo "<b> $i </b>";
}
else
{
echo "<a href='memo.php?page=$i'> $i </a>";
}
}
?>
다음 ▶</div>
</div> <!-- end of ripple -->
</div> <!-- end of col2 -->
</div> <!-- end of content -->
</div> <!-- end of wrap -->
</body>
</html>
The SQL doesn't like doing maths in the limit clause. Do it first in PHP:
$sql2 = "select * from phptest.memo order by num desc limit $start," . ($start+$scale);

displaying results from a mysql database

Working on a project to display a list of cars
$username="username";
$password="password";
$database="listofcars";
$mysqli = new mysqli("localhost", $username, $password, $database);
#mysql_select_db($database) or die( "Unable to select database");
$query2="SELECT * FROM cars";
$result=$mysqli->query($query2);
$num=$mysqli->mysqli_num_rows($result);
$mysqli->close();
echo "
<div class="item">
<div class="container">
<div class="imgcontainer"><img alt="Cars for sale" src="$carimage" width="380" height="380" /></div>
<div class="details">
<a href="$internallink" target="_blank">
<h3 class="title"> $carname <br />
<span> $cartype </span></h3>
<p>
$cardesc
</p>
<div class="button"><span data-hover="Order Car">Order Car</span></div>
</a>
</div>
</div>
</div>
I need it to loop the results
database name: listofcars
tablename: cars
Field names
carsid
carname
carimage
cartype
internallink
cardesc
Try this to see if it works.
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "listofcars";
// Create connection
$mysqli = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$mysqli) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM cars";
$result = mysqli_query($mysqli, $sql);
?>
<?php while($row = mysqli_fetch_assoc($result)) { ?>
<div class="item">
<div class="container">
<div class="imgcontainer">
<img alt="Cars for sale" src="<?php echo $row['carimage']; ?>" width="380" height="380">
</div>
<div class="details">
<a href="<?php echo $row['internallink']; ?>" target="_blank">
<h3 class="title"><?php echo $row['carname']; ?><br><span><?php echo $row['cartype']; ?></span></h3>
<p><?php echo $row['cardesc']; ?></p>
<div class="button">
<span data-hover="Order Car">Order Car</span>
</div>
</a>
</div>
</div>
</div>
<?php } ?>
mysqli_close($mysqli);
You need to leave the connection open until you are done and loop through it after running the query.
Something like
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "listofcars";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
//Query
$sql = "SELECT * FROM cars";
$result = mysqli_query ($conn, $sql);
while($row = mysqli_fetch_array($result)) {
//Whatever you want to do for each row...
echo "
<div class='item'>
<div class='container'>
<div class='imgcontainer'><img alt='Cars for sale' src='".$row[carimage]."' width='380' height='380' /></div>
<div class='details'>
<a href='".$row[internallink]."' target='_blank'>
<h3 class='title'> ".$row[carname]." <br />
<span> ".$row[cartype]." </span></h3>
<p>
".$row[cardesc]."
</p>
<div class='button'><span data-hover='Order Car'>Order Car</span></div>
</a>
</div>
</div>
</div>";
}
$conn->close();

MySQL Database row in a Button

I have a database for my ToDo App which has following cloumns:
| ID | ShortDescription | Description | Date | Status |
I already can add a Task to the Datatable and can see it in phphmyadmin.
I have following code till now:
$id = mysql_real_escape_string($_GET['id']);
$out = 'SELECT * FROM ToDo1 WHERE `id` = '.$id.' LIMIT 1';
$result = mysqli_query($link, $out);
$row= mysqli_fetch_array($result);
?>
<div id= "OutShortDescription">
<?php
echo $row['ShortDescription'];
?>
</div>
<div id= "OutDescription">
<?php
echo $row['Description'];
?>
</div>
<div id= "OutDate">
<?php
echo $row['Date'];
?>
</div>
<div id= "OutStatus">
<?php
echo $row['Status'];
?>
</div>
Now I want to put every ID row on a own Site.
For that I want to make a table of Buttons (Buttonnumber=ID).
On this Button should only be shown the ShortDescription and when I click it I want to go to a the Site which matches to the Button.
Can someone help me?
EDIT
okay thanks now I have this code but it wont work:
<?php
$dbname= 'Groups';
$dsn = 'mysql:host=localhost;dbname='.$dbname;
$user = 'root';
$pass = '';
$db = new PDO($dsn, $user,$pass);
$query = "SELECT * FROM groups2 WHERE id = :id LIMIT 1";
$ps = $db->prepare($query);
$ps->bindParam(':id', $id);
$ps->execute();
$row = $ps->fetch(PDO::FETCH_ASSOC);
?>
<div class="searchwindow">
<?php
$data = $link->query('SELECT * FROM Groups2');
foreach($data as $row) {
echo '<p><input type="button" onclick="window.location = All_Groups.php?id=' . $row['ID'] . ' value='.$row['ShortDescription'].' /></p>';
}
I have now following code
<div data-role="page" id="SearchPage" data-title="SearchPage">
<div data-role="header">
<h1>Search</h1>
</div>
<div data-role="content">
<div data-role="header">
<form>
<fieldset data-role="controlgroup" data-type="horizontal" data-mini="true" align="center" id="selectMenu">
<select name="selectStatus" id="selectStatus">
<option value="0">Status</option>
<option value="1">Done</option>
<option value="2">In Progress</option>
</select>
</fieldset>
</form>
</div>
<?php
$dbname= 'Groups';
$dsn = 'mysql:host=localhost;dbname='.$dbname;
$user = 'root';
$pass = '';
$db = new PDO($dsn, $user,$pass);
$query = "SELECT * FROM groups2 WHERE id = :id LIMIT 1";
$ps = $db->prepare($query);
$ps->bindParam(':id', $id);
$ps->execute();
$row = $ps->fetch(PDO::FETCH_ASSOC);
?>
<div class="searchwindow">
<?php
$data = $link->query('SELECT * FROM Groups2');
foreach($data as $row) {
$path = $row['ID'];
$description = $row['ShortDescription'];
echo ("<form action='All_Groups.php?id=$path'><button type='submit' value='$description'/>$description</form>" );
}
?>
</div>
</div>
<div data-role="footer" data-position="fixed">
<div data-role="navbar">
<ul>
<li>Search</li>
<li>New</li>
<li>More</li>
</ul>
</div><!-- Ende navbar -->
</div><!-- Ende footer -->
</div>
And this is my All_groups.php
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Unbenanntes Dokument</title>
</head>
<body>
<?php
$servername ="localhost";
$username = "root";
$password = "";
$dbname = "Groups";
$link = mysqli_connect($servername, $username, $password, $dbname);
if (!$link) {
die('Verbindung nicht möglich : ' . mysqli_error($link) );
}
?>
<?php
$row="";
$Date="";
$Status="";
$ShortDescription="";
$Description="";
mysqli_select_db($link, "groups");
?>
</div>
<?php
$id = mysql_real_escape_string($_GET['id']);
$out = "SELECT * FROM groups2 WHERE ID = '$id' ";
$result = mysqli_query($link, $out);
$id = mysqli_fetch_array($result);
?>
<div id= "OutShortDescription">
<?php
echo $id['ShortDescription'];
?>
</div>
<div id= "OutDescription">
<?php
echo $id['Description'];
?>
</div>
<div id= "OutStatus">
<?php
echo $id['Status'];
?>
</div>
<div id= "OutDate">
<?php
echo $id['Date'];
?>
<div data-role="footer" data-position="fixed">
<div data-role="navbar">
<ul>
<li>Search</li>
<li>New</li>
<li>More</li>
</ul>
</div><!-- Ende navbar -->
</div>
</body>
</body>
</html>
First of all, don't use the mysql_* methods! Use PDO or mysqli_* instead.
Below, I'm pasting your example query, using PDO:
$dsn = 'mysql:host=localhost;dbname='.$dbname;//$dbName is the name of your database
$user = 'root';
$pass = '123';//use your login information here
$db = new PDO($dsn, $user,$pass);
$query = "SELECT * FROM ToDo1 WHERE id = :id LIMIT 1";
$ps = $db->prepare($query);
$ps->bindParam(':id', $id)
$ps->execute();
$row = $ps->fetch(PDO::FETCH_ASSOC);
Now, to get your button, you don't need to use jquery:
<?php
$path = $row['ID'];
$description = $row['ShortDescription'];
echo "<form action='your/site/$path'><button type='submit' value='$description'/>$description</form>"
?>
Another option is use the onclick:
<?php
$path = $row['ID'];
$description = $row['ShortDescription'];
echo "<input type=\"button\" onclick=\"location.href='your/site/$path'\" value=\"$description\" />";
?>
The \ before " is a escape, so PHP will print the character " and not interpret it as the end of your string.
Advice: Try to avoid mix HTML and PHP, in general this is a bad practice.

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