im facing a problem for displaying edit,delete buttons for the comments that belongs to that user ,,,this is my code and dont get it where is the problem and how to solve it (because like this: edit and delete buttons will show up for the all comments !
$results = mysqli_query($c, "SELECT * from comments where idpub=".$_GET['com']."and iduser=".$_SESSION['id']);
while ($rows = mysqli_fetch_assoc($results)) {
?>
<?php echo $rows['username']; ?>
<?php echo $rows['textcom']; ?>
<a href="comment2.php?edit=<?php echo $rows['id']; ?>" class="edit_btn" >Edit</a>
Delete
<?php } ?>```
You have to check logged in User's Id and the User id of Comment who add that comment. If Both ids are the same then display those buttons.
$results = mysqli_query($c, "SELECT * from comments where idpub=".$_GET['com']."and iduser=".$_SESSION['id']);
based on the above code I think you are fetching all the comments of user id which is store in a session, so it shows edit delete buttons with each comment. So You have to change your code like below.
$results = mysqli_query($c, "SELECT * from comments where idpub=".$_GET['com']);
while ($rows = mysqli_fetch_assoc($results)) {
?>
<?php echo $rows['username']; ?>
<?php echo $rows['textcom']; ?>
<?php if($_SESSION['id'] == $rows['userid']){ ?>
<a href="comment2.php?edit=<?php echo $rows['id']; ?>" class="edit_btn"
>Edit</a>
Delete
<?php } }?>
Related
<?php include ("../navbar.php"); ?>
<?php include("../conn.php");
$query = "select * from semi_synthetic";
$result = mysqli_query($conn,$query);
?>
<div class="container">
<div class="compound_report_card">
<h2>Compound Report</h2>
<div class="card-content">
<?php
//Check If Id Isset.
if(isset($_GET["CHIHBT_ID"]))
//Display Id On A Webpage.
echo "<p>Calculated Properties of : " . $_GET[ "CHIHBT_ID" ] . "</p>";
$result = mysqli_query($conn, $query);
?>
<?php
while($row = mysqli_fetch_assoc($result )) {
?>
<ul>
<li>Canonical smiles : <?php echo $row['smile_notation']; ?> </li>
<li> Molecular weight : <?php echo $row['molecular_weight']; ?></li>
<li> Heavy atoms : <?php echo $row['number_of_heavy_atoms']; ?></li>
<li> Aeromatic heavy atoms : <?php echo $row['number_of_aeromatic_heavy_atoms']; ?></li>
<li> Rotatable bonds : <?php echo $row['number_of_rotatable_bonds']; ?></li>
<li> H bond acceptors : <?php echo $row['number_of_h_bond_acceptors']; ?></li>
<li> H bond donors: <?php echo $row['number_of_h_bond_donors']; ?></li>
<li> Tpsa A**2 : <?php echo $row['tpsa']; ?></li>
</ul>
<?php } ?>
</div>
</div>
</div>
<?php include ("../footer.php"); ?>
so this is my new code and i am displaying data on web from my database and . So what i am trying to do is i have a lot of id's and i want that when i click on any id the data of that id from
database should get displayed. So the current situation is that when i am clicking on one id whole data from that table is getting displayed and i want only one to get displayed. I want onlyspecific data from every id that i click. So the problem i think is in while loop so please see that also
The problem: Your code is getting all records and isn't querying against your selected ID.
What you need to do: Add selected ID in query to get specific result. Like shown below:
Update: From What I understand $_GET["CHIHBT_ID"] is your ID, right? Change this code in your script:
<?php include("../conn.php");
$query = "select * from semi_synthetic";
$result = mysqli_query($conn, $query);
?>
To
<?php include("../conn.php");
$query = "select * from semi_synthetic";
?>
Then, change the following code:
<?php
//Check If Id Isset.
if (isset($_GET["CHIHBT_ID"]))
//Display Id On A Webpage.
echo "<p>Calculated Properties of : " . $_GET["CHIHBT_ID"] . "</p>";
$result = mysqli_query($conn, $query);
?>
With this:
<?php
//Check If Id Isset.
if (isset($_GET["CHIHBT_ID"])) {
$query .= ' WHERE CHIHBT_ID = ' . $_GET["CHIHBT_ID"]; // add selected id in query
//Display Id On A Webpage.
echo "<p>Calculated Properties of : " . $_GET["CHIHBT_ID"] . "</p>";
}
$result = mysqli_query($conn, $query); // run query
?>
and that's it.
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.";
}
?>
I have two tables “categories” and “posts”, I’m trying to create a category.php page that displays the different categories with post titles as links to the original posts. I’ve tried different variations of loops but can’t seem to get it right. I’m hoping someone could point me in the right direction.
$query = "SELECT post_id, title, body, category_id, posted
FROM posts
INNER JOIN categories ON categories.category_id = posts.category_id";
$result = mysqli_query($dbc, $query)
or die('Error querying database.');
while ($row = mysqli_fetch_array($result)){
<a href='index.php?id=<?php echo $post['post_id']; ?>' ><?php echo $post['title']; ?></a></h2>
<p>
Posted on <?php echo date('d-m-y h:i:s',strtotime($post['date_posted'])); ?>
In <a href='category.php?id=<?php echo $post['category_id']; ?>' ><?php echo $post['name']; ?></a>
</p>
echo "<hr />";
}
Thank you for any input
Forgive my syntax (it may be off a bit), but this should give you a good idea:
$categories = array(
'sports' => array(
post1,
post2,
),
'weather' => array(
post1,
post2,
),
);
Get all of your categories first, then loop through each and get all the posts related to that category. Then you can loop through the array normally like:
foreach($categories as $category){
//display category title
foreach($category['posts'] as $post){
//display post info
}
}
You have some issues in your code.
Modified code:
$query = "SELECT post_id, title, body, category_id, posted FROM posts INNER JOIN categories ON categories.category_id = posts.category_id";
$result = mysqli_query($dbc, $query) or die('Error querying database.');
while ($row = mysqli_fetch_array($result)){
?>
<a href="index.php?id=<?php echo $row['post_id']; ?>" >
<?php echo $row['title']; ?></a></h2> <p>
Posted on
<?php
echo date('d-m-y h:i:s',strtotime($row['date_posted']));
?>
In
<a href="category.php?id=<?php echo $row['category_id']; ?>">
<?php
echo $row['name'];
?></a> </p>
<hr />
<?php } ?>
Issues:
array index using with single quote inside the single quote.
wrong variable using post.
html tags inside the php
You are using a variable called $post inside your while loop, but you are retrieving each row in that while loop into a variable called $row.
Fix that and many of your issues will disappear.
You also need to stop and start the PHP interpreter when you want to output HTML in the way that you have done here, note the ?> and <?php I addded
$query = "SELECT post_id, title, body, category_id, posted
FROM posts
INNER JOIN categories ON categories.category_id = posts.category_id";
$result = mysqli_query($dbc, $query)
or die('Error querying database.');
// change this to use a variable called $post
while ($post = mysqli_fetch_array($result)){
?>
<a href='index.php?id=<?php echo $post['post_id']; ?>' ><?php echo $post['title']; ?></a>
<!-- Dont think this should be here -->
</h2>
<p>Posted on <?php echo date('d-m-y h:i:s',strtotime( $post['date_posted'] ) );?>In <a href='category.php?id=<?php echo $post['category_id']; ?>' ><?php echo $post['name']; ?></a>
</p>
<?php
echo "<hr />";
}
?>
There is HTML code inside <?php ?> try this :
<?php $query = "SELECT post_id, title, body, category_id, posted
FROM posts
INNER JOIN categories ON categories.category_id = posts.category_id";
$result = mysqli_query($dbc, $query)
or die('Error querying database.');
while ($row = mysqli_fetch_array($result)){ ?>
<a href='index.php?id=<?php echo $post['post_id']; ?>' ><?php echo $post['title']; ?></a></h2>
<p>
Posted on <?php echo date('d-m-y h:i:s',strtotime($post['date_posted'])); ?>
In <a href='category.php?id=<?php echo $post['category_id']; ?>' ><?php echo $post['name']; ?></a>
</p>
echo "<hr />";
<?php } ?>
I need to set two variable in a url, $id and $job. The data should come from mysql select statement, [id] and [job_number_id]. My page displays a client job proposal and if there is more than one all proposals are displayed but the [id] and the [job_number_id] determines what is displayed on the webpage. I dont have a clue as to how this done. Any help would be greatly appreciated. Here's the code:
<?php
$url = 'http://localhost/estimate_lar/homepage.php';
$id = $_SESSION['id'];
$query = "SELECT id, client_job_name, job_number_id
FROM `job_name`
WHERE `id`='$id'";
$allJobs = $db->query($query);
?>
<?php foreach ($allJobs as $site_title) : ?>
<p>
<tr><?php echo ''.$site_title['client_job_name'],$site_title['job_number_id']. '<br />'.''; ?>
<td></td>
</tr>
</p>
<?php endforeach; ?>
If you want the variables to be avaialble in the URL you need to read them with $_GET.
Getting the arguements from a url such as index.php?id=1&job_number_id=3 will look like that:
if (isset($_GET['id']) && isset($_GET['job_number_id'])) {//make sure both arguments are set
$id = $_GET['id'];
$job_number_id = $_GET['job_number_id'];
}
To set it in your foreach statement:
<?php foreach ($allJobs as $site_title) : ?>
<p>
<tr><?php
$url = "http://localhost/estimate_lar/homepage.php?id=" . $site_title['id'] . "&job_number_id=" . $site_title['job_number_id'];
echo ''.$site_title['client_job_name'],$site_title['job_number_id']. '<br />'.'';
?>
<td></td>
</tr>
</p>
<?php endforeach; ?>
PLEASE remember to read about SQL injection and making sure you are escaping your inputs. Or even better - use a prepared statement.
Currently your script is volunerable, since everyone could just alter the URL and manipluate your DB.
Hope this helps!
Try this.
<?php
session_start();
$id = $_SESSION['id'];
$url = 'http://localhost/estimate_lar/homepage.php';
if($id){
$query = "SELECT id, client_job_name, job_number_id FROM `job_name` WHERE `id`='$id'";
$allJobs = $db->query($query);
}else{
echo "Id not in session";
}
?>
<table>
<?php if ($allJobs) { foreach ($allJobs as $site_title) : ?>
<tr>
<td>
<?php echo $site_title['job_number_id']. " ".$site_title['job_number_id']; ?>
</td>
</tr>
<?php endforeach; ?>
</table>
<?php }else{ echo 'No results Found' ;} ?>
This may help you.
I'm not really sure what the problem is here, I have a posts table that has a foreign key of userid and I want to pull their usernames from the users table based on that, it seems to pull them out fine but it won't display on the first post.
My code is:
$query_therealuserid = 'SELECT users.username, users.userid, posts.userid, posts.created_at
FROM users, posts
WHERE users.userid = posts.userid
ORDER BY posts.created_at';
$therealuserid = mysql_query($query_therealuserid, $akaearthdb) or die(mysql_error());
and
<?php do { ?>
<div id= "post">
<?php if (stripos($row_rsjustpost['link'], ".png") !== false) {?>
<img src="<?php echo $row_rsjustpost['link']; ?>" id="postImage" alt="Broken Link">
<?php }?>
<?php if (stripos($row_rsjustpost['link'], ".gif") !== false) {?>
<img src="<?php echo $row_rsjustpost['link']; ?>" id="postImage" alt="Broken Link">
<?php }?>
<?php if (stripos($row_rsjustpost['link'], ".jpg") !== false) {?>
<img src="<?php echo $row_rsjustpost['link']; ?>" id="postImage" alt="Broken Link">
<?php }?>
<?php if (stripos($row_rsjustpost['link'], ".jpeg") !== false) {?>
<img src="<?php echo $row_rsjustpost['link']; ?>" id="postImage" alt="Broken Link">
<?php }?>
<?php echo $row_rsjustpost['title']; ?>
<br/>
<?php echo $row_rsjustpost['text']; ?>
</p>
<br />
<?php
do {
if ($whileLoopCounter0!=$whileLoopCounter1){break;}
?>By: <?php echo $row['username'];
echo "<br />";
$whileLoopCounter1++;
} while($row = mysql_fetch_array($therealuserid));
?>
</div>
<?php $whileLoopCounter0++; ?>
<?php } while ($row_rsjustpost = mysql_fetch_assoc($rsjustpost)); ?>
</div>
when I pull up the page I get all the posts but the first one doesn't have a username and the usernames are all pushed down one post. The first post has a postid of 2, if I create a new post with a postid of 1 and a userid it shows up at the top without a username and the others usernames are moved up so that they are corect.
That's because you use a do-while loop, the $row = mysql_fetch_array($therealuserid) is executed at the end of the loop, why not use a regular while loop.
You're currently using a do-while loop, so first the do block is executed and then the while block is evaluated only after the 1st execution of the do block.
Use this instead:
while ($whileLoopCounter0==$whileLoopCounter1) {
$row = mysql_fetch_array($therealuserid);
if ($row){
?>
By:
<?php
echo $row['username'];
echo "<br />";
$whileLoopCounter1++;
}
}
Looks like you are fetching the username after you increment the loop counter. The user name is pulled after the loops is one, so the first row will be empty because that data doesn’t exist yet.
There are a lot of other things though that you’re doing that are just wasting effort though. Try tossing the results into an object and using foreach($result as $key=>$value) or foreach($result as $item) if you do not need the key instead.