Display image from database showing same images - php

I am trying to display an image coming from the database and I was able to get it but the problem is, it's displaying same images even though I uploaded different images.
Here's my code:
<?php
$con = mysql_connect('localhost','root','')
or die(mysql_error());
mysql_select_db ("dbname");
$query = "SELECT * FROM news ORDER BY date DESC";
$result = mysql_query($query);
echo "<table align='center'>";
while($row = mysql_fetch_assoc($result))
{
echo "<tr>";
echo "<td>";
echo "<img src='getImage.php?id='".$row['id']."' height='230px' width='300px'> <br /><br />";
//Some codes here...
echo "</table>";
mysql_close($con);
?>
getImage.php
<?php
$con = mysql_connect('localhost','root','')
or die(mysql_error());
mysql_select_db ("dbname");
$query = "SELECT * FROM news ORDER BY date DESC";
$result = mysql_query($query);
header("Content-type: image/png");
while($row = mysql_fetch_assoc($result))
{
echo $row['image'];
}
mysql_close($con);
?>
Pls. help me...

Because I'm lazy, I'm going to assume id is numeric here.
In getImage.php:
We need to tell getImage.php to just get the image that has the ID we want.
$id = intval($_GET['id']);
//Forces $id to be numeric. You wouldn't have to worry about doing this if
//you used prepared statements. Like those in John Conde's links.
$query = "SELECT * FROM news WHERE `id`=$id";
As it is now, getImage.php is actually outputting all your images. You only see the one with the latest date however. This is because it's the first retrieved by your script thanks to the ORDER BY clause.
Also, in your display loop, change this:
echo "<img src='getImage.php?id='".$row['id']."' height='230px' width='300px'> <br /><br />";
to this:
echo "<img src='getImage.php?id=".$row['id']."' height='230px' width='300px'> <br /><br />";
Removed the extra single quote between id= and it's number, this way the number will actually be sent to your script.

Your problem is the getImage.php does not return the corresponding image with id in query string. Your source codes always return the image of latest news, right?
Try to replace this :
$query = "SELECT * FROM news ORDER BY date DESC";
to
$query = "SELECT * FROM news WHERE id = {$_GET['id']} LIMIT 1";

Related

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>";
}
?>

PHP - Looping Through rows in MySql and displaying a field based on user's click

My first question on Stack Overflow, so please forgive if I make any mistake.
I have a database, in which I store the User Id and the title and corresponding content. Then in a PHP page, I loop through the rows of the table and display the title of the content in an anchor tag.
The problem is, When the user clicks on a specific title, he should be redirected to new page where the content corresponding to the title which he clicked is displayed.
Could you please guide me as to how to achieve it?
Thanks.
Here is the code that I've tried.
<?php
$counter1 = 0;
$sql = "SELECT * FROM blogdata2";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()){
echo "<a style='color: #E64A19;' href='#'/>".$arrayContent[$counter1]['title'];
echo "<br/>";
$counter1++;
Try like this...
$sql = "SELECT * FROM blogdata2";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()){
echo "<a style='color: #E64A19;' href='newpage.php?content=".$row['content']."'>".$row['title']."</a>";
echo "<br/>";
}
and in newpage.php
<?php
$content=$_GET['content'];
echo "<p>".$content."</p">;
But following way is best...
$sql = "SELECT * FROM blogdata2";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()){
echo "<a style='color: #E64A19;' href='newpage.php?id=".$row['id']."'>".$row['title']."</a>";
echo "<br/>";
}
and in newpage.php
<?php
$id=$_GET['id'];
$sql="SELECT * FROM blogdata2 WHERE id='$id'";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
echo "<p>".$row['content']."</p>";

showing relating records in website using mysql / php

Sorry for my lack of experience, but I try to explain my goal the best a can...
I have 2 MySQL tables with the following fields...
tbl_cars_clients
FIELDS
"client_name,
logo_img,
phone,address,email"
tbl_cars_items
FIELDS
"car_name,
client_name,cars_img"
I have a main page called index.php that contains the following code
$query_cake = mysql_query("select * from tbl_cars_clients", $connection);
while ($row = mysql_fetch_array($query_cars)) {
echo "<img class='img' src={$row['ad_img']} width='350px' height='225px'>";
I have another page called items-cars.php
<?php
if (isset($_GET['id'])) {
$client_id = $_GET['id'];
$query1 = mysql_query("SELECT * FROM tbl_cars_items WHERE client_name = '$client_id'", $connection);
while ($row1 = mysql_fetch_array($query1)) {
?>
<?php echo "$row1['cars_img']} width='250px' height='250px'>";?>
On index.php, When I click on any of these Image hyperlinks, it opens items-cars.php and only display records with the same client_name field from the 2 tables.
My goal is to not only show the relating records on items-cars.php, but to display the records from the tbl_cars_clients table.
EXAMPLE: If I click image hyperlink for TOYOTA on index.php, It currently opens up the following records on items-cars.php...
COROLLA,
PRIUS,
CAMRY,
TUNDRA,
Instead, I would like it to display results like...
TOYOTA
888-123-4567
123 Maple Dr.
info#toyota.com
COROLLA,
PRIUS,
CAMRY,
TUNDRA,
i think you want this...
<?php
if (isset($_GET['id'])) {
$client_id = $_GET['id'];
$query1 = mysql_query("SELECT * FROM tbl_cars_items WHERE client_name = '$client_id'", $connection);
while ($row1 = mysql_fetch_array($query1)) {
$query2 = mysql_query("SELECT * FROM tbl_cars_clients WHERE client_name = '$client_id'", $connection);
$row2 = mysql_fetch_array($query2);
?>
<?php echo $row2['client_name']; ?><br />
<?php echo $row2['phone']; ?><br />
<?php echo $row2['address']; ?><br />
<?php echo $row2['email']; ?><br />
<?php echo "$row1['cars_img']} width='250px' height='250px'>";?>
}

PHP MySQL single column/row display?

I'm trying to get a single result from my database, just one name.
I tried using;
$row = mysql_fetch_array(mysql_query("SELECT * FROM persons WHERE id = '$id'"));
echo $row['name'];
But that din't work, any other way to simply show only one result?
Thanks in advance!
[EDIT:]
(I'm using PHP 5.3)
<?php
include("connection.php");
$id = $_GET['deletid'];
$result = mysql_query("SELECT * FROM persons WHERE id = '$id' LIMIT 1");
if(!$result){
echo mysql_error();
}
if ($row = mysql_fetch_array($result)){
echo $row['name'];
}
echo "<p>id:$id</p>";
?>
If you need just the name and you need just one result you should rewrite your query as follow:
$row = mysql_fetch_array(mysql_query("SELECT name FROM persons WHERE id = '". (int) $id ."' LIMIT 1"));
Now to get the result you should just get it with a
$row['name'];
EDIT
Now that you posted your entire code i got what's wrong: You are deleting that result before getting its name. Basically you delete that user and then you attempt to get its name.
EDIT
<?php
include("connection.php");
if (empty($_GET['deleteid'])) {
exit('"deleteid" is empty');
}
$id = mysql_real_escape_string($_GET['deletid']);
$result = mysql_query("SELECT name FROM persons WHERE id = '". (int) $id ."' LIMIT 1");
if(!$result){
echo mysql_error();
}
$row = mysql_fetch_assoc($result); // for just one result you don't need of any loop
echo $row['name'];
echo "<p>id:". htmlspecialchars($id) ."</p>";
?>
try
$row = mysql_fetch_array(mysql_query("SELECT name FROM persons WHERE id = ". (int) $id));
echo $row['name'];

Display only queried ID+row PHP/MySQL

I have my data stored in a MySQL table, which includes an auto_increment ID number (unique) for each new row.
I'd like users to be able to get a certain ID number, using the $_GET function.
eg. User loads http://mysite.com/id.php?id=123
Page displays ID number 123 along with the row.
echo $row['id'];
echo "<table>";
echo "<tr> <th>Unit</th> <th>Message</th> <th>Date</th> </tr>";
while($row = mysql_fetch_array( $result )) {
echo "<tr><td>";
echo $row['title'];
echo "</td><td>";
echo $row['description'];
echo "</td><td>";
echo $row['pubDate'];
echo "</td></tr>";
}
echo "</table>";
echo "</center>";
I'm stuck as to where I put the $_GET bit.
Thanks :)
You should append it to your query (using intval to avoid SQL injection) like this:
// use the id in your WHERE clause, convert it to an integer to avoid sql injections
$query = 'SELECT fields FROM table WHERE id = ' . intval($_GET['id']);
$result = mysql_query($query);
$row = mysql_fetch_row($result);
... do stuff with $row ...
Firstly, your code does not make much sense, since you use $row before it was defined.
Secondly, $result isn't defined at all, and it should be, for example like this:
$id = intval($_GET['id']);
$result = mysql_query("SELECT FROM table WHERE id = '$id'");
And now you know how and where to use $_GET['id'].
Dont waste your time doing the comparison afterwards, you'll save yourself alot of time by adding it to the original query
$id = intval($_GET['id']);
$query = "SELECT whatever FROM table WHERE id=$id";
$id = $_GET['id'];
$id = mysql_real_escape_string($id);
$query = "SELECT * FROM `Table` WHERE `id`='" . $id . "'";
$res = mysql_query ($query);
$exist = mysql_num_rows($res);
if ($exist) {
$row = mysqlfetch_assoc($res);
...
}

Categories