PHP didn't show text from table MySQL for specific user - php

i tried to make some changes for my page.
So,i want to show for users a specific coins what are in the table.(Table are created)
But,when i try to doing this didn't work(didn't show) if i want for the username.
Work only without verification for user id/name.
My code:
$strSQL = "SELECT * FROM users WHERE id=" . $_GET["id"];
$rs = mysql_query($strSQL);
while($row = mysql_fetch_array($rs)) {
echo "<dt>Coins:</dt><dd>" . $row["coins"] . "</dd>";
}
I tried to $_GET["id"] with ["username"].But also didn't work (Verification for session is like this: if(isset($_SESSION['username']))
Somebody know why didn't work?

$strSQL = "SELECT * FROM users WHERE id='".$_GET['id']."'";

Related

how can I select and display all the rows from the same user, underneath each other with php

Users can add their workouts into a database. There will have more than one workout per user. How can i display the workout names for the logged in user, underneath each other? I have no problem adding the workouts. When I echo the names of the added workouts, it display right next to each other like "workout1workout2". I want to be able to display the workouts underneath each other. I have no idea how to do it. It would be best if I could display each workout as a button.
$query = "SELECT * FROM info WHERE username='$_SESSION[username]' ";
$results = mysqli_query($db, $query);
while($data = mysqli_fetch_array($results)) {
echo $data['workout'];
}
Firstly, you shouldn't put that username in query like that. Look into SQL Injection and how to avoid that in PHP.
As for displaying buttons/workouts. You can embed HTML code inside the echo statement like below. You can add any html like that and it will render HTML.
$query = "SELECT * FROM info WHERE username='$_SESSION[username]' ";
$results = mysqli_query($db, $query);
while($data = mysqli_fetch_array($results)) {
echo "<button>$data['workout']</button></br>";
}
$query = "SELECT * FROM info WHERE username='$_SESSION[username]' Group By username";

is it possible to send a query in database if the no. of likes are above 50

I am creating a page in which user can upload their picture and other users can like it.
Now is there any method that when the user's like goes above 50 lets say 51, then execute a query that saves this users name and postid. And then show a notification to user that your post have crossed 50 likes.
Also i dont want the query to execute again and again i want it to send that query only once for that particular post i am using php any suggestions
this is the code that i used but didnt work:
<?php if ($vote>=50){
mysqli_query($con, "insert into notifications (user_id,post_id) values('$id3','$pixid')")or die(mysqli_error($con));
}
?>
try below :
if($vote > 50) {
$result = mysqli_query("select * from notifications where user_id = " . $id3 . " And post_id = " . $pixid);
if(!mysqli_num_rows($result)) {
mysqli_query($con, "insert into notifications (user_id,post_id) values('$id3','$pixid')")or die(mysqli_error($con));
}
}

How do I display a logged in user's images from the database?

Say if a user uploaded multiple images to their profile page, how would I display that specific user's images? I tried I did something like this:
$db = mysqli_connect("localhost", "root", "", "testdb");
$sql = "SELECT * FROM users ORDER BY id DESC";
$result = mysqli_query($db, $sql);
while ($row = mysqli_fetch_array($result)) {
echo "<a href='profiles/uploads/".$row['image']."> ";
echo "<img id='img_div' title='".$row['image']."' alt='".$row['image']."' src='profiles/uploads/".$row['image']."'/>";
//echo "<p id='img_div'>".$row['desc']."</p>";
echo "</a>";
But I feel like this is terribly wrong because it is showing everyone's images and not the user's images. I tried looking up answers, but can't seem to find one.
You need a where clause. where id = 5, of course, replace the number with whatever user you are looking for.
Right now the query SELECT * FROM users ORDER BY id DESC is saying:
Select all columns from all users, ordering them by id.
Instead, you want something like:
SELECT * FROM users WHERE id = {user id} which states:
Select all columns from all users, where the id equals the user id.
As an aside, I'm not sure how you are setting up your database, but
if a user uploaded multiple images to their profile page, how would I display that specific user's images
makes me think that you should really be having separate tables, if you want to allow multiple pictures.
Try this:
You need to set user in sql query like id = 10.
And also missing ' last of this line: echo "<a href='profiles/uploads/".$row['image'].">";
<?php
$user_id = 10;
$db = mysqli_connect("localhost", "root", "", "testdb");
$sql = "SELECT * FROM users WHERE id = ". $user_id ." LIMIT 1";
$result = mysqli_query($db, $sql);
while ($row = mysqli_fetch_array($result)) {
echo "<a href='profiles/uploads/". $row['image'] ."'>";
echo "<img id='img_div' title='".$row['image']."' alt='".$row['image']."' src='profiles/uploads/".$row['quotes']."'/>";
//echo "<p id='img_div'>".$row['desc']."</p>";
echo "</a>";
}
?>

Using MYSQL to call a row from a table

I am trying to show specific information for my logged in users, i can call the user_name, but i have been able also to call info from the "website" entry which holds the html for the page, i need to be able to call "website" to each logged in user, i.e Susan (user_name) has the html showing when she logs in, BUT Barry (user_name) has the same html showing as susan, as it is being called from Susans row! Please help it is driving me mad!
Here is the code that is on the myaccount.php:
Welcome To <?php echo $_SESSION['user_name'];?>'s Account Page</strong>
<?php
if (isset($_GET['msg'])) {
echo "<div class=\"error\">$_GET[msg]</div>";
}
?>
<?php
$data = mysql_query("SELECT * FROM users") or die(mysql_error());
$info = mysql_fetch_array( $data );
Print "<b></b> ".$info['$website'] . " ";
?>
You have to add a WHERE clause to your query. If you have a field in your table called user_name it will be like this :
"SELECT * FROM users WHERE user_name = '" . $_SESSION['user_name'] . "'";
Note : You are using a deprecated (mysql_query) you should use rather (mysqli_query) which has different syntax
http://php.net/manual/en/mysqli.query.php
Have you tried
$data = mysql_query("SELECT * FROM users WHERE username='$_SESSION[user_name]'") or die(mysql_error());

Displaying the users information from the database

I am trying to created a way to call information from the database for a user to view. Such as they log in and it has their registered information viewed. I have this
session_start();
if($_SESSION['id'])
$result = mysql_query("SELECT * FROM User WHERE `id` = $_SESSION[id]")
or die(mysql_error());
while($row = mysql_fetch_array( $result )) {
echo '<b>First Name:</b>' .$row['fname'];
echo '<br>';
echo '<b>Last Name:</b>' .$row['lname'];
}
but nothing shows up. My database name is megan, table is user, fields i want displayed are first name (fname) and last name (lname).
Can someone point me in the right direction. Thank you in advance!
Bad array indexing.
$result = mysql_query("SELECT * FROM User WHERE `id` = " . $_SESSION['id'])
You should turn on PHP error displaying.
First change this,
if(isset($_SESSION['id']))
because you have to check if the session isset correctly then do the query,
then the sql change to this,
$result = mysql_query("SELECT * FROM User WHERE `id` = ".$_SESSION['id'])

Categories