Display only the current user's details using PHP and MySQL - php

In this part of the code, both the score table, and the profile page display everything I want, however it is displaying EVERYBODIES details like
First Name
Surname
Email
Category
Username
However, I want it so that when the user is logged in they can only see THEIR OWN details
This is score.php
<?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
require("db_connect.php");
session_start();
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="../../../../favicon.ico">
<?php
$con=mysqli_connect("localhost","username","Password","Database");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Score where 'Username' LIKE _['Username']");
echo "<table border='1'>
<tr>
<th>ID</th>
<th>Username</th>
<th>Score</th>
<th>Gamedate</th>
<th>QuizTitle</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['Username'] . "</td>";
echo "<td>" . $row['Score'] . "</td>";
echo "<td>" . $row['Gamedate'] . "</td>";
echo "<td>" . $row['QuizTitle'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
this is profile.php
<?php
include_once 'db_connect.php';
?>
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="../../../../favicon.ico">
<title>Profile page </title>
<?php
$con=mysqli_connect("localhost","Username","password","database");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT `FirstName`,`Surname`,`Email`,`Username`,`Date_Creation`FROM Users ");
while($row = mysqli_fetch_array($result))
{
echo "<br />Your <b><i>Profile</i></b> is as follows:<br />";
echo "<b>First name:</b> ". $row['FirstName'];
echo "<br /><b>Last name:</b> ".$row['Surname'];
echo "<br /><b>Email:</b> ".$row['Email'];
echo "<br /><b>Year:</b> ".$row['Username'];
echo "<br /><b>Date created :</b> ".$row['Date_Creation'];
}
mysqli_close($con);
?>
</main>
</html>
These are the errors I recieve when I try to run the page. Before I changed the select query so it picked the current user that is logged in, but it displayed everyone's information

Single quotes in SQL are used to refer to a specific string like 'Bob'. If you're trying to check the value of the column, don't put the column name in quotes as 'Username'. Instead, put the column name without quotes: where Username like

About the errors. You can use the error handling to show what's the problem on your query. Second, you can use the ID to get the data information of the logged in user. You can save the ID of the user via session, when user logged in saved the ID then use it to your where clause.

in your score.php file, on this line:
$result = mysqli_query($con,"SELECT * FROM Score where 'Username' LIKE _['Username']");
Look at your WHERE statement, seems to be wrong.
And in your profile.php, on this line:
$result = mysqli_query($con,"SELECT `FirstName`,`Surname`,`Email`,`Username`,`Date_Creation`FROM Users ");
You dont pass any WHERE statement. So, the problems are in your SQL, specifically on your wheres.

Related

PHP Custom blog system, Duplicates message for every user

so I'm making a blog system on https://aindrigo.com/blog.php?bid=1 and for some reason the post duplicates for every user that exists.. any help?
<?php
$con = mysqli_connect("localhost","root","don't try hacking my db","data");
$bid = $_GET["bid"];
$queryb = $con->query("SELECT bid,title,content FROM blogposts WHERE bid=$bid");
$queryu = $con->query("SELECT id,username,password,avatarurl FROM accounts");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="description" content="aindev's Website">
<link rel="stylesheet" href="style.css?v=2222">
<link href="https://fonts.googleapis.com/css?family=Inter&display=swap" rel="stylesheet">
<title>Adam Indrigo</title>
</head>
<body>
<div class="start">
<h1>aindev</h1>
</div>
<div class="info">
<div class="inner">
<h1>aindev's Blog</h1>
<?php
if(!$bid){
echo "<h1>Please enter a Blog ID (bid) in the url</h1>";
}
while($row = mysqli_fetch_assoc($queryb)){
while($row2 = mysqli_fetch_assoc($queryu)){
echo "<img src='" . $row2["avatarurl"] . "?v=22222' style='max-width: 60px; max-height: 60px; border-radius: 6px;'>";
echo "<h2>" . $row["title"] . " by " . $row2["username"] . "</h2>";
echo "<p>" . $row["content"] . "</p>";
}
}
?>
</div>
</div>
</body>
</html>
I don't know the problem. I tried adding it so that it only shows it for the 1 user that created it. Even adding a cid tag, but that didn't work..
So apparently I made a mistake when I added the CID tag. I added the cid to the user query and not the blog query.

Picture references from MySql

i am trying to buid a Php site that show data from my MySQl database.
and i think im almost there, everything works except the Pictures.
i cant get my php site to show the pictures with the picture reference from Sql database.
<!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">
<?php
$con=mysqli_connect("localhost","root","","db1");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
<head>
<link rel="stylesheet" href="styles.css">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>untitled</title>
</head>
<body>
<div class="content">
<?php
$sql = "SELECT id, Producent, Model, kategori FROM tb1";
$result = mysqli_query($con,"SELECT * FROM `tb1");
echo "<table>";
while($row = $result->fetch_assoc())
{
echo "<tr>";
echo "<td>";?> <img scr="<?php echo $row["Billedurl"]; ?>"/> <?php echo " </td>";
echo "<td>" .$row["Producent"] .$row["Model"]; echo "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
<!-- end .content --></div>
<!-- end .container --></div>
<div class="footer"><br>
<!-- end .footer --></div>
</body>
</html>
You need to select all the columns you are going to use, so if you need Billedurl, you should change:
$sql = "SELECT id, Producent, Model, kategori FROM tb1";
to:
$sql = "SELECT id, Producent, Model, kategori, Billedurl FROM tb1";
Now the value of that column will be available in $row["Billedurl"].
Edit: It seems that now you have the correct value in your html, but the path to the image is not correct as it is a relative path.
You should prefix your variable with the correct folder so that the image is found by the browser. That can be as simple as just using an absolute path but that depends on where the pic/ directory is located.
So if your variable contains pic/l_jabra_evolve80.jpg" and the pic/ folder is on the root of the web-server, you can do something like:
# before the loop
$imagePrefix = '/';
# in the loop
... <img scr="<?php echo $imagePrefix . $row["Billedurl"]; ?>"/> ...
Now the browser will try to fetch the image from /pic/l_jabra_evolve80.jpg.
You need to do little bit of debugging:
first change little bit in your query
$sql = "select * FROM tb1";
$result = mysqli_query($con, $sql);
Then try to check print_r($result) and check if you are getting everything here or not.

Trying to get a PHP button to delete MySQL rows

EDIT: Added the whole code for viewProblems, with the actual credential information just ***.
I've been trying to do this all day and I can't figure it out. What I want is a button at the end of each row (the button shows up correctly) that allows me to delete that row from the MySQL database and the page (doesn't delete anything though). The code I have is as follows:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>View Problems</title>
<!-- Bootstrap core CSS -->
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="well">
<div class="container">
<div class="page-header">
<h1>Jimmy's Laundry</h1>
</div>
<ol class="breadcrumb">
<li>Home</li>
<li>Login</li>
<li>Admin page</li>
</ol>
</div>
</div>
<?php
$servername = "***";
$username = "***";
$password = "***";
$dbname = "***";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT problem_id, machine_id, description FROM tbl_problem";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
echo '<table class="table table-hover"><tr><th>Problem ID</th><th>Machine Number</th><th>Problem Description</th><th> </th></tr>';
while($row = $result->fetch_assoc())
{
echo "<tr><td>" . $row['problem_id']. "</td><td>" . $row['machine_id']. "</td><td>" . $row['description']. "</td><td><form action='deleteProblem.php?name=" . $row['problem_id']."' method= 'post'><input type='hidden' name='id' value=".$row['problem_id']."><input class ='btn btn-danger' type='submit' name='submit' value='Resolved?'></form></td></tr>";
}
echo "</table>";
}
else
{
echo "There are no problems! :)";
}
?>
</table>
</body>
For my main page, viewProblems.php. My deleteProblem.php page is as follows:
<?php
$query= "DELETE FROM tbl_problem WHERE problem_id={$_POST['id']}";
mysql_query ($query);
if (mysql_affected_rows() == 1)
{
echo "<strong>Row has been deleted</strong>"
}
else
{
echo "<strong>Deletion Failed</strong>"
}?>
I've been browsing this site and Google, and I'm trying to get it to work, but it just won't. The page loads the table correctly, but when I click the button, it takes me from website/viewProblems.php to website/deleteProblem.php?name=9(or 10, 11, 12, 13, depending on which button I press) but the page is just white space and the database doesn't get updated.
Any help would be appreciated.
P.S. I know that mySQL_ methods are dated, but we have to use them.
The script that deletes the row is a separate script that is executed in a separate request. Therefor, it is completely isolated from the request that generated the page and you will have to make a new database connection if you want to query or delete data.
In your current situation, you don't make a connection, so that's why the delete statement fails.

php code display code in browser

I am doing a project using my university web server where my httpdocs are stored.
Everything works fine including my other PHP scripts which connect to phpmyadmin database both posting and getting from the database. However when I put this script on the web server and try to access it the page outputs some of the PHP code and I cannot find out why. I am not seeing any open tags etc. The page looks like this:
<!DOCTYPE html>
<html>
<head>
<title>Gallery</title>
Home<br />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
mysql_connect("XXXXXXX","XXXXX","XXXXXXXX","XXXXXXXXXXX");
mysql_select_db("sql1103884");
$res=mysql_query("SELECT * FROM Images");
echo "<table>";
while($row=mysql_fetch_array($res))
{
echo "<tr>";
echo "<td>";?> <img src="<?php echo $row["imagepath"]; ?>" height="100" width="100"> <? php echo "</td>";
echo "<td>"; echo $row["name"]; echo "</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>
If your file extension is not php then please change it to .php and second replace with below code. there is space between php opening tag like this <? php so it should be <?php
<!DOCTYPE html>
<html>
<head>
<title>Gallery</title>
Home<br />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
mysql_connect("XXXXXXX","XXXXX","XXXXXXXX","XXXXXXXXXXX");
mysql_select_db("sql1103884");
$res=mysql_query("SELECT * FROM Images");
echo "<table>";
while($row=mysql_fetch_array($res))
{
echo "<tr>";
echo "<td>";?> <img src="<?php echo $row["imagepath"]; ?>" height="100" width="100"> <?php echo "</td>";
echo "<td>"; echo $row["name"]; echo "</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>

how to add links to a list of whats trending from the Foursquare API using PHP

Hi so I have created a list of the trending places in a given area using the Foursquare API: see the following link http://createmate.co/foursquare-whatshot/
I am trying to figure out how to make each item link to its respective Foursquare URL. Here is the code so far...
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Foursquare What's Hot</title>
<!-- external CSS link -->
<link rel="stylesheet" href="css/normalize.css">
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div id="container">
<h1>Foursquare What's Hot</h1>
<?php
/* GET THE DATA */
$trending_url = file_get_contents("https://api.foursquare.com/v2/venues/trending?ll=40.7,-74&oauth_token=BQEPEMYIFHHH2C1OGBTJDI4GBYV5HQAPNFLR5ON1JIAI42GN&v=20130220");
$trending_output = json_decode($trending_url);
/* TEST THE DATA */
//echo "<pre>";
//print_r($trending_output);
//echo "</pre>";
/* PRINT RESULTS */
for ($i=0;$i<10;$i++) {
echo "<ul id ='locations'>";
echo "<li> <a href= " . $trending_output->response->venues[$i]->canonicalURL . "> <h3>" . $trending_output->response->venues[$i]->name . "<br>";
echo "<li> <h4>" . $trending_output->response->venues[$i]->location->address . "<br>";
echo "</ul>";
}
?>
</div>
</body>
Any help is much appreciated.
Adam
The property you retrieve should be canonicalUrl, not canonicalURL.
You should also surround your href attribute with quotes and don't forget to close your <a> tag as well.

Categories