issue displaying multiple users info by id - php

I'm trying to get it where it will get the friends id from the friends table, and then get the friends information from the users table. I've tried using a foreach but had no luck.
Here's what I have right now where it's only echoing one friend and not the three that I have in the table. Any ideas on how I can fix this issue? Maybe I wasn't using the for each properly? Thank You In Advance!
<?php
//Gets users information from users.
$stmt = $DB_con->prepare('SELECT friendsid FROM friends WHERE userid='.$_SESSION['user']['id']);
$stmt->execute();
if($stmt->rowCount() > 0) {
$row=$stmt->fetch(PDO::FETCH_ASSOC);
extract($row);
} else {
$friendsid = $row['friendsid'];
}
$stmt = $DB_con->prepare('SELECT username,userprofile,status FROM users WHERE id='.$friendsid);
$stmt->execute();
if($stmt->rowCount() > 0) {
while($row=$stmt->fetch(PDO::FETCH_ASSOC)) {
extract($row);
?>
<div class="row">
<div class="col-sm-3">
<div class="well">
<h4><strong><?php echo $row['username'];?></strong></h4>
<img src="images/profile/<?php echo $userprofile;?>" class="img-circle" height="70" width="70" alt="Avatar">
</div>
</div>
<div class="col-sm-9">
<div class="well">
<h3 class="text-left"><?php echo $row['status'];?></h3>
</div>
</div>
</div>
<?php
}
} else {
?>
<?php
}
?>

Prepared statements use parameters and you should use a JOIN.
$stmt = $DB_con->prepare('SELECT u.* FROM users AS u JOIN friends AS f ON u.id = f.friendsid WHERE f.userid = :user_id');
$stmt->bindParam(':user_id', $_SESSION['user']['id']);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
// Do whatever you want with $row['id'], $row['username'], $row['userprofile']
}

Related

How to display information form a database about gender user?

In the database I have a gender table where male is M and female is F. Then I want to display full information about the gender of the user, so if M is to be male, and if F is to be female. The problem is that it always displays Female. Can someone see the code and give a hint where the error lies?
public function getData()
{
$id = $_GET['id'];
$sql = $this->database->connect()->prepare("SELECT user.id, first_name, last_name, avatar, birth_date, gender, age, school, work, city, martial_status, number_phone FROM user JOIN user_data ON user.id = user_data.user_id where user.id = :abc and user_id = :abc");
$sql->bindParam(':abc',$id, PDO::PARAM_INT);
$sql->execute();
if($sql->rowcount())
{
$row = $sql->fetch();
}
$this->userData = $row;
}
public function display(string $colName)
{
echo (isset($this->userData[$colName])) ? $this->userData[$colName] : '';
}
<main>
<div class='container'>
<div class="row">
<div class="col-xl-12 d-flex justify-content-center">
<div id="name"><span><?php echo $profil->display('first_name') ?></span> <span><?php echo $profil->display('last_name') ?></span></div>
</div>
</div>
<div class="row" id="margin">
<div class="col-xl-12 d-flex justify-content-center">
<div>Płeć: <?php if($profil->display('gender')=='M'){ echo "Mężczyzna";} else{ echo "Kobieta";} ?></div>
</div>
</div>
</div>
</main>

I cannot print all images with a nested while loop

i'm newbie on this.
I explain shortly what i need.
Obtained the category ID via $_GET i want to print how images i have for each product (for that category).
For example, i have 2 product and 2 images for each product.
Problem found: with this code i have printed only the images for the first product and not for the second. Why? Thanks
$id=17;
$prod = "SELECT pd_id FROM product WHERE cat_id = ?";
$stmt = $con->prepare($prod);
$stmt->bindParam(1, $id);
$stmt->execute();
$num = $stmt->rowCount();
if($num>0)
{
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
extract($row);
$immagini = "SELECT pd_image FROM pd_images WHERE pd_id = ?";
$stmt = $con->prepare($immagini);
$stmt->bindParam(1, $pd_id);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
extract($row);
?>
<img src="<?php echo $home_url; ?>images/product/<?php echo $pd_image; ?>" class="img-fluid border-radius-0" alt="">
<?php
}
}
}else { ?>
<div class="col-lg-12 text-center">
<h6>There's no products for this category</h6>
<i class="rt-icon2-user"></i>
<div class="divider-10 d-none d-xl-block"></div>
</div>
<?php } ?>

How to Display User Chats using PHP and MySQL

I wrote this query To fetch details but its not working. Here is my Code:
$sender_id=$_GET['sender_id'];
$sender_id=mysqli_real_escape_string($con,$sender_id);
$sender_id=htmlentities($sender_id);
$sql1="SELECT * FROM massges where receiver_id='".$_SESSION['u_id']."' and sender_id='".$sender_id."' order by msg_id";
$sql2="SELECT * FROM massges where receiver_id='".$sender_id."' and sender_id='".$_SESSION['u_id']."' order by msg_id";
$sqls=[$sql1,$sql2];
foreach($sqls as $k => $sql){
$run=mysqli_query($con,$sql);
if (mysqli_num_rows($run)>0) {
while ($row=mysqli_fetch_assoc($run)) {
$msg_text=$row['msg_text'];
$msg_id=$row['msg_id'];
$query1="SELECT * FROM users where u_id='".$sender_id."'";
$query2="SELECT * FROM users where u_id='".$_SESSION['u_id']."'";
$querys=[$query1,$query2];
foreach($querys as $k => $query){
$res=mysqli_query($con,$query);
while($data=mysqli_fetch_assoc($res)){
$user_image=$data['user_image'];
}
echo '<br>
<div class="row">
<div class="col s2" style="margin: 0px;">
<img src="users/'.$user_image.'" class="circle right" height="20px">
</div>
<div class="col s6"><span class=" blue lighten white-text" style="margin: 0px;border-radius: 10px;padding:5px;">'.$msg_text.'<span></div>
</div>';}
Here is the displayed data:
Result Image
How can I display sender's and receiver's messages?
you can do this in one query, your code will be much easier to debug if you do so:
$sender_id = $_GET['sender_id'];
$user_id = $_SESSION['u_id'];
// Get all Messages where the user and sender are involved
$statement = $con->prepare('
SELECT
messages.msg_text,
users.user_image
FROM
messages
JOIN
users ON users.u_id = messages.sender_id
WHERE
(
messages.sender_id = ?
AND messages.receiver_id = ?
) OR (
messages.sender_id = ?
AND messages.receiver_id = ?
)
ORDER BY
messages.msg_id
');
$statement->bind_param(
"iiii",
$sender_id,
$user_id,
$user_id,
$sender_id
);
$statement->execute();
// Assign the content of 'msg_text' and 'user_image'
$statement->bind_result($text, $image);
// loop through all results
while ($statement->fetch()) {
/* ECHO YOUR HTML HERE */
echo $text;
echo $image;
}
Links that may help:
http://php.net/manual/en/mysqli-stmt.bind-param.php
http://php.net/manual/en/mysqli-stmt.bind-result.php
$sender_id = $_GET['sender_id'];
$user_id = $_SESSION['u_id'];
$sql="SELECT
massges.msg_text,
massges.msg_id,
users.user_image
FROM
massges
JOIN
users ON users.u_id = massges.sender_id
WHERE
(
massges.sender_id = '".$sender_id."'
AND massges.receiver_id = '".$user_id."'
) OR (
massges.sender_id = '".$user_id."'
AND massges.receiver_id = '".$sender_id."'
)
ORDER BY
massges.msg_id";
$run=mysqli_query($con,$sql);
if (mysqli_num_rows($run)>0) {
while ($row=mysqli_fetch_assoc($run)) {
$msg_id=$row['msg_id'];
$msg_text=$row['msg_text'];
$user_image=$row['user_image'];
echo '<br>
<div class="row">
<div class="col s2" style="margin: 0px;">
<img src="users/'.$user_image.'" class="circle right" height="20px">
</div>
<div class="col s6"><span class=" blue lighten white-text" style="margin: 0px;border-radius: 10px;padding:5px;">'.$msg_text.'<span></div>
</div>'; } }

Fetching results from database in PDO

I developed a simple upload photo and comment system. When I use for each, it repeats the image. E.g If there are 5 comments on a photo. It repeats the photo 5 times with the 5 different comments. Instead of one image for 5 comments.
I want 1 image with each comments. How do I get all the comments for each photo without repeating the photo?
I use PDO and here is the php code:
<?php
//This is for the images
$results = $connecDB->prepare("select image,caption,id from gallery order by id desc LIMIT $start, $limit ");
$results->execute();
$results = $results->fetchAll();
foreach($results as $results) {
$pid=$results["id"];
//This is for the comments
$re= $connecDB->prepare("select * from comments where pid = :pid order by id");
$re->bindParam(':pid', $pid);
$re->execute();
foreach($re as $re) {
$name=$re["name"];
$comment=$re["comment"];
?>
<div class="item" id="item-<?php echo $results['id']?>">
<p><img src="../upload/images/<?php echo $results['image']?>" height="500px" width="500px"</p>
<p><?php echo $results['caption']?></p>
<div class="large-2 columns small-3"><font color="#3366ff"><?php echo $name?></font></div>
<div class="large-10 columns"><p><?php echo $comment?></p></div>
<?php}
}?>
Thanks.
You were looping everything inside inner loop, try like this.
<?php
//This is for the images
$results = $connecDB->prepare("select image,caption,id from gallery order by id desc LIMIT $start, $limit ");
$results->execute();
$results = $results->fetchAll();
foreach($results as $results) {
$pid=$results["id"];
?>
<div class="item" id="item-<?php echo $results['id']?>">
<p><img src="../upload/images/<?php echo $results['image']?>" height="500px" width="500px" /></p>
<p><?php echo $results['caption']?></p>
<?php
//This is for the comments
$re= $connecDB->prepare("select * from comments where pid = :pid order by id");
$re->bindParam(':pid', $pid);
$re->execute();
foreach($re as $re) {
$name=$re["name"];
$comment=$re["comment"];
?>
<div class="large-2 columns small-3"><font color="#3366ff"><?php echo $name?></font></div>
<div class="large-10 columns"><p><?php echo $comment?></p></div>
<?php}?>
</div>
<?php}?>
Actually you are not getting the result in $re variable. You need to fetchAll data after executing the query. As well as you were looping everything in inner foreach.
foreach($results as $results) {
$pid=$results["id"];
//This is for the comments
$re= $connecDB->prepare("select * from comments where pid = :pid order by id");
$re->bindParam(':pid', $pid);
$re->execute();
$comments = $re->fetchAll();
<div class="item" id="item-<?php echo $results['id']?>">
<p><img src="../upload/images/<?php echo $results['image']?>" height="500px" width="500px"</p>
<p><?php echo $results['caption']?></p>
<div class="large-2 columns small-3"><font color="#3366ff"><?php echo $name?></font></div>
foreach($comments as $re) {
$name=$re["name"];
$comment=$re["comment"];
?>
<div class="large-10 columns"><p><?php echo $comment; ?></p></div>
<?php}
}?>

Variable for $_GET array not returning any values

I can't seem to return any values on my $_GET array.
It works fine when e.g.
$sql = "SELECT * FROM review WHERE brand='brandx'" but when I change it to brand='$id' in line 5, nothing gets passed.
The fetch array in my index.php works perfectly fine however when it gets href to brand.php (as shown below), I lose my marbles.
<?php
if(isset($_GET["id"])){
include "php_includes/db_conx.php";
$id = preg_replace('#[^0-9]#i', '', $_GET["id"]);
$sql = "SELECT * FROM review WHERE brand='$id'";
$query = mysqli_query($db_conx, $sql);
$productList = "";
// Now make sure that brand exists in the table
$productCount = mysqli_num_rows($query);// count the output amount
if($productCount > 0){
//get the products off the selected brand
while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
$username = $row['username'];
$productname = $row['productname'];
$comment = $row['comment'];
$rating = $row['rating'];
$date = $row['date'];
$productList .=
'
<div class="wrapper">
<div class="brand-and-name">
<div class="brand">
<a href="brand.php?id='.$id.'">
<span>'.$id.'</span>
</a>
</div>
<div class="prod-name">
'.$productname.'
</div>
</div>
<div class="prod-container" id="pd1">
<div class="prod-img"><img src="https://giovanniphotography.files.wordpress.com/2011/09/creativemevid19.jpg" /></div>
<div class="comment">
<b>My Score: '.$rating.'/10</b>
<br /><br />
<p>'.$comment.'</p>
</div>
<div class="profile">
<div class="profile-thumb" id="pt1"></div>
<div class="name" id="nm1">
'.$username.'<br />'.$date.'
</div>
</div>
<div class="social-share-1">
<div class="like-btn"></div>
<div class="comment-btn"></div>
<div class="wishlist-btn">+ wishlist</div>
</div>
</div><!--end .prod-container#pd1-->
</div><!--wrPer-->
';
}
}else{
echo "Product doesnt exist";
exit ();
}
}else{
echo "You got to pick a brand man!";
exit ();
}
?>
Does this code work, after your preg_replace? If not you may not have magic quotes enabled in php.ini. I notice in the rest of your output you are concatenating strings and variables.
print "ID: $id";
use a prepared statement and dont hassle with sanitizing of user input:
if($stmt = $db_conx->prepare("SELECT username, productname, comment, rating, date FROM review WHERE brand=?)")
{
$stmt->bind_value('s', $_GET["id"]);
$result = $stmt-execute();
$stmt->bind_result($username, $productname, $comment, $rating, $date ); //bind result to vars
//now you can loop through your result:
while($stmt->fetch()) {
//use $username, $productname, $comment, $rating, $date etc to work with your values
}
}

Categories