Okay, so I have a members database script where it displays all the members and as soon as you go to the page, it suddently changes your session['id'] to a random one. Not sure why its doing this but here is the code and I have tested it to find that it is only when this script is loaded that it does it.
Am I just stupid and can't spot the bug?
<?php
$link = mysqli_connect("localhost", "lunar_lunar", "", "lunar_users");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$result = mysqli_query($link, "SELECT * FROM users ORDER BY username");
while($row = mysqli_fetch_assoc($result)) {
$id=$row['id'];
$username=$row['username'];
$email=$row['email'];
$firstname=$row['firstname'];
$lastname=$row['lastname'];
$motto=$row['motto'];
$bio=$row['bio'];
$result4 = mysqli_query($link, "SELECT * FROM photo where id='$id'");
$row4 = mysqli_fetch_assoc($result4);
$image=$row4['filename'];
$src = (empty($image)) ? "upload/your-photo.jpg" : "site_images/$id/$image";
$motto = (empty($motto)) ? "No motto" : $motto;
$bio = (empty($bio)) ? "No biography" : $bio;
echo "<div class='panel panel-default'>
<div class='panel-heading'>
<h3 class='panel-title'><a href='public.php?id=".$id."'>".$username."</h3></a>
</div>
<div class='panel-body'>
<div class='gravatar span3' style='padding:0px;margin:0px;'>
<img src='
".$src."' alt='' width='85' height='85'>
</div>
<br />
<div class='page-header'>
<br />
</div>
<p style='margin-right:450px;'>
".$bio."
</p>
</div>
<div class='panel-footer'>".$motto."</div>
</div>";
}
?>
Top of document:
<?php
session_start();
if(!isset($_SESSION['id'])) {
header("Location: index.php");
} else {
}
?>
It basically checks if you are signed in, I use this same script to check on every page so I know that works.
Sorry, this was my mistake. I was overwriting $id, when I changed $id to $memid it fixed.
Related
I'm fairly new to php so please don't roast me :)
My current code only shows the current logged in user and their work with session management
however I want to display all users from my database (member) who have uploaded their work and have a hyperlink at the bottom of the page so people can click on their name and view all the pictures they have uploaded. ( << not to sure how to do that ). Is there a way to show only users that have uploaded a file to the database and not just registered users?
<?php
session_start();
$page_title="Gallery Home";
include("header.inc");
include("nav.inc");
include("categorys.inc");
?>
<!-- start of main content -->
<div id="main-content">
<?php
$db = mysqli_connect("localhost", "root","", "artworks") or die(mysqli_error($db));
$q = "select * from artwork";
$results = mysqli_query($db, $q) or die(mysqli_error($db));
while($row=mysqli_fetch_array($results))
{
print "<a href='artwork.php?artwork_id={$row['artwork_id']}'</a><img src='uploads/{$row['filename']}'height= '300' width='333.33' >";
}
?>
</div>
<!-- end of main content -->
<?php
//gets username of current user
$username = $_SESSION['username'];
//this is the link that shows all pictures uploaded by user
print "<td><a href='member.php?$username='$username'>$username</a></td>\n";
?>
<?php
include("footer.inc");
?>
Thanks Heaps !
Hope this helps :) Goodluck!
<!-- start of main content -->
<div id="main-content">
<?php
$db = mysqli_connect("localhost", "root","","artworks");
$q = "SELECT * FROM artwork GROUP BY member_id"; //artwork table must have member id, this id will be used matching for table member member id
$results = mysqli_query($db, $q) or die(mysqli_error($db));
if(mysqli_num_rows($results) > 0){ //if there is file/member id from table artwork matched for table member member id
while($row = mysqli_fetch_array($results)){
$member_id1 = $row['member_id'];
$query_member = mysqli_query($db,"SELECT * FROM users WHERE id='$member_id1' ")or die(mysqli_error($conn)); //only display member that has uploaded file
$res = mysqli_fetch_array($query_member);
$member_id = $res['id']; //this will be use for displaying uploaded file
$member_username = $res['username'];
?>
<a href="member.php?member_id=<?php echo $member_id; ?>" target="_blank" ><?php echo $member_username; ?></a><br> <!-- -->
<?php
}
}
else{
echo "No Result Found.";
}
?>
</div>
<!-- end of main content -->
This will be the member.php
<?php
$db = mysqli_connect("localhost", "root","","artworks");
$member_id = $_GET['member_id'];
$query_artwork = mysqli_query($db,"SELECT * FROM artwork WHERE member_id='$member_id' ")or die(mysqli_error($db));
if(mysqli_num_rows($query_artwork) > 0){
while($res = mysqli_fetch_array($query_artwork)){
$filename = $res['filename'];
?>
<img src="uploads/<?php echo $filename; ?>" height= "300px" width="333.33px" >
<?php
}
}
else{
echo "No artworks found.";
}
?>
This is the piece of my code from index.php:
<div class="row">
<?php
$conn = mysql_connect("localhost","root","");
mysql_select_db("imdb_db");
$res = mysql_query("select * from `film` ORDER BY ID DESC");
while($row = mysql_fetch_array($res)) {
?><div class='col-xs-2'><?php
?><p style="position:relative;left:-40px"><?php echo $row["Cim"];?></p><?php
?> <img src="php/imageView.php?ID=<?php echo $row["ID"];?>" class="img-responsive" style="width:200px;height:250px;" alt="Image"> <?php
?></div><?php
}
mysql_close($conn);
?>
</div>
This is the imageView.php:
<?php
$conn = mysql_connect("localhost","root","");
mysql_select_db("imdb_db") or die(mysql_error());
if(isset($_GET['ID'])) {
$sql = "SELECT `Boritokep` FROM `film` WHERE ID=". $_GET['ID'];
$result = mysql_query("$sql") or die("<b>Error:</b> Problem on Retrieving Image BLOB<br/>" . mysql_error());
$row = mysql_fetch_array($result);
echo $row["Boritokep"];
}
mysql_close($conn);
?>
And this is the result:
With php/imageView.php?ID=<?php echo $row["ID"] I getting the path of my file:
uploads/film/pic/5.jpg
Why it doesn't displays the image?
Why it doesn't displays the image?
Because php/imageView.php?ID=<?php echo $row["ID"];?> returns the path of an image and doesn't return the image itself.
imageView.php can take that path and redirect the user agent to it:
header(sprintf('Location: %s', $row["Boritokep"]));
I am working on my blog and I can't seem to figure this out. The if ($result->fetchColumn()) should be working. It does give the error but when data(row) exists it does not show anything. What is wrong?
<?php
$id = $_GET["id"];
$sql = "SELECT * FROM `posts` WHERE `id` = $id";
$result = $conn->query($sql);
if ($result->fetchColumn() > 0) {
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
echo '<div class="post-preview">
<a>
<h2 class="post-title">
'.$row["title"].'
</h2>
<h3 class="post-subtitle">
'.$row["content"].'
</h3>
</a>
<p class="post-meta">'.$row["creator"].' | '.$row["date"].'</p>
</div>
<hr>';
}
}
else {
echo "<center><h1>Post does not exist.</h1></center>";
header("Refresh: 2; url=index.php");
}
?>
Whenever I remove the line if ($result->fetchColumn() > 0) { it works fine. But I need to check if the $id exists in the database. If it does, it should show the data, if not it should show the error and link back to index.php.
Your code could be much simpler
Just populate your $row variable and see whether it contains anything or not.
<?php
$stmt = $conn->prepare("SELECT * FROM `posts` WHERE `id` = ?");
$stmt->execute([$_GET["id"]]); // essential to protect from SQL injection!
$row = $stmt->fetch(PDO::FETCH_ASSOC)
if ($row) {
echo '<div class="post-preview">
<a>
<h2 class="post-title">
'.$row["title"].'
</h2>
<h3 class="post-subtitle">
'.$row["content"].'
</h3>
</a>
<p class="post-meta">'.$row["creator"].' | '.$row["date"].'</p>
</div>
<hr>';
}
else {
echo "<center><h1>Post does not exist.</h1></center>";
header("Refresh: 2; url=index.php");
}
I am building a blog and trying to show all comments that apply to the post.
Each post has an ID and each comment is stored with the post ID.
here is my code:
<?php
$con = mysql_connect("localhost","cl49-XXX-b","X");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("cl49-XXX-b", $con)or die( "Unable to select database line 873");
$result=mysql_query("SELECT * FROM blogcomments WHERE ID='".$ID."'") or die('Error on Line 215 :'.mysql_error());
echo " <ul class='comments'> "; // first row beginning
for ($i = 1; $i <= mysql_num_rows($result); $i++)
{
$row = mysql_fetch_array($result);
$name = $row['name'];
$email = $row['email'];
$comment = $row['comment'];
$created=$row['created'];
echo" <li>
<div class='comment'>
<div class='thumbnail'>
<img class='avatar' alt='' src='img/avatar.jpg'>
</div>
<div class='comment-block'>
<div class='comment-arrow'></div>
<span class='comment-by'>
<strong>$name</strong>
<span class='pull-right'>
<span> <a href='#'><i class='icon-reply'></i> Reply</a></span>
</span>
</span>
<p>$comment</p>
<span class='date pull-right'>$created</span>
</div>
</div> ";
echo " </li>"; // it's time no move to next row
}
?>
When I run this code the page only shows one comment, although my DB has 3 comments with the correct ID.
I would consider using mysqli_ as mysql_ has been depreciated. I'd also consider using a while loop, hopefully this will help:
<?php
$DBServer = 'localhost';
$DBUser = 'xxxx';
$DBPass = 'xxxx';
$DBName = 'xxxx';
$mysqli = new mysqli($DBServer, $DBUser, $DBPass, $DBName);
if ($mysqli->connect_errno) {
echo "Failed to connect to the database: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
$query = "SELECT * FROM blogcomments WHERE ID='". $ID ."'";
echo " <ul class='comments'> ";
if ($result = $mysqli->query($query)) {
while ($row = $result->fetch_assoc()) {
?>
<li>
<div class='comment'>
<div class='thumbnail'>
<img class='avatar' alt='' src='img/avatar.jpg'>
</div>
<div class='comment-block'>
<div class='comment-arrow'></div>
<span class='comment-by'>
<strong><?php echo $row['name']; ?></strong>
<span class='pull-right'>
<span><a href='#'><i class='icon-reply'></i> Reply</a></span>
</span>
</span>
<p><?php echo $row['comment']; ?></p>
<span class='date pull-right'><?php echo $row['created']; ?></span>
</div>
</div>
</div>
</li>
<?php
} $result->close();
} $mysqli ->close();
echo "</ul>";
?>
I haven't tested this for bugs, but let me know if you like further information.
When you do this :
mysql_query("SELECT * FROM blogcomments WHERE ID='".$ID."'")
You select just one comment in the database.
Maybe you have to do that :
mysql_query("SELECT * FROM blogcomments WHERE post_ID='".$post_ID."'")
Because you are selecting the row where ID field value is $ID in the table blogcomments.
I guess you store the referred post_id to whom the comment is connected in a field called something like "post_id". You better point your select query to that field ;)
If you put the structure of the table in the question, I might help :)
You're not closing your <ul>. Might it just be a HTML formatting issue?
i tried the empty selector in jquery but it doesnt work. the content is still being displayed. i am retrieving some rows from the SQL database.. if there is no database then i dont want to display that div.
<div id="scrollingText">
<div class="scrollWrapper">
<div class="scrollableArea">
<marquee behavior="scroll" direction="left">
<p>
<?php
$con = mysql_connect("localhost","fraptech_test","");
mysql_select_db("fraptech_test", $con);
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysql_select_db("fraptech_ndsnotice", $con);
$result = mysql_query("SELECT * FROM ndsnotice");
while($row = mysql_fetch_array($result))
{
echo $row['Notice'];
}
?>
</p>
</marquee>
</div>
</div>
</div>
Is that your whole code? Are you working with ajax? If you're using pure PHP without ajax just set the output div into your "if"-conditions?
If you want to use jQuery, try:
if ( ($("div.scrollableArea p").text()).length > 0 )
{
$("div.scrollableArea p").show();
}
else
{
$("div.scrollableArea p").hide();
}
But you can do it without jQuery, I guess.
You need to put the HTML inside your php if clause.
Simple example:
<div id="scrollingText">
<?php
$con = mysql_connect("localhost","fraptech_test","");
mysql_select_db("fraptech_ndsnotice", $con);
$result = mysql_query("SELECT * FROM ndsnotice");
if (mysqli_connect_errno($con)) {
<div class="scrollWrapper">
<div class="scrollableArea">
<marquee behavior="scroll" direction="left">
<p>
<?php
while($row = mysql_fetch_array($result))
{
echo $row['Notice'];
}
?>
</p>
</marquee>
</div>
</div>
<?php } else { ?>
<div class="errordiv">Display this on error</div>
<?php } ?>
</div>
thank you guys with ur help i made some change. the below code did it:
<?php
$con = mysql_connect("localhost","fraptech_test","");
mysql_select_db("fraptech_test", $con);
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysql_select_db("fraptech_ndsnotice", $con);
$result = mysql_query("SELECT * FROM ndsnotice");
if (mysql_num_rows($result) > 0)
{
?><div id="scrollingText">
<div class="scrollWrapper">
<div class="scrollableArea">
<marquee behavior="scroll" direction="left">
<p>
<?php while($row = mysql_fetch_array($result))
{
echo $row['Notice'];
}
?> </p>
</marquee>
</div>
</div>
</div>
<?php } ?>