passing data using hyperlink in PHP - php

I'm trying to pass the path of an image ['guest'] from one page to another using a link. (I'm storing URLs of images in database)
Can't seem to get the image to display, which is a bigger image of 'url'. I'm doing it this way so that I can have a larger image displayed in the target page (does_this_work.php) plus adding some other bits on the page too.
I'm still learning and can;t seem to see what I'm doing wrong. Any help appreciated,
<?php
$host=""; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name
$tbl_name=""; // Table name
// Connect to server and select databse.
mysql_connect($host, $username, $password)or die("cannot connect");
mysql_select_db($db_name) or die("cannot select DB");
$photo=mysql_query("SELECT * FROM `images` ORDER BY (ID = 11) DESC, RAND() LIMIT 7");
while($get_photo=mysql_fetch_array($photo)){ ?>
<div style="width:300px;">
<a href="does_this_work.php?big_image=$get_photo['guest']>" target=""><img src="<?
echo $get_photo['url']; ?>" title="">
</div>
<? } ?>
I then use the following code to try and display the array data in the target file
<?php
echo "this is the page where you get a larger picture of image on previous page, plus
further info";
$big_image = $_GET['guest'];
echo $big_image;
?>

You're missing an opening tag in here (And a closing semi colon, but that's not as problematic here):
<a href="does_this_work.php?big_image=$get_photo['guest'] ?>"
Change to:
<a href="does_this_work.php?big_image=<?= $get_photo['guest']; ?>"

There are several errors in your code. First of all, this is how you use $_GET and $_POST:
ex. site.php?argument=value
To retrieve the value of argument, you need this code in site.php:
//The variable must not necessarily be $value
$value = $_GET['argument'];
//Alt.
$value = $_POST['argument'];
Secondly (like the other answers tell you) you are missing a php-tag here:
<a href="does_this_work.php?big_image=$get_photo['guest']>" target=""><img src="<? echo $get_photo['url']; ?>" title="">
Instead it should be:
<a href="does_this_work.php?big_image=<?php echo $get_photo['guest']; ?>" target=""><img src="<?php echo $get_photo['url']; ?>" title="">
Now, in order to make that compatible with your second code, you need to change the argument sent to guest like this:
<a href="does_this_work.php?guest=<?php echo $get_photo['guest']; ?>" target=""><img src="<?php echo $get_photo['url']; ?>" title="">
OR change the $_GET['guest']; to $_GET['big_image'];
I think I got it all right..

<a href="does_this_work.php?big_image=$get_photo['guest'] ?>" target=""><img src="<?
echo $get_photo['url']; ?>" title="">
You are missing your php starting tags. This should read:
<a href="does_this_work.php?big_image=<? $get_photo['guest'] ?>" target=""><img src="<?
echo $get_photo['url']; ?>" title="">

Related

Image from MYSQL database (PHP, MySql)

I am trying to display a image URL from mysql database into my website. It's saved as URL in my database.
http://www.upload.ee/image/5697422/prague-1168302_1920.jpg
this is the url which should appear as $rida['pilt']
<?php
$paring = 'SELECT * FROM postitus ORDER BY id';
$valjund = $yhendus->query($paring);
while($rida = mysqli_fetch_assoc($valjund))
?>
<img class="img-thumbnail" alt="city" src="<?php echo $rida['pilt']; ?>" style="width:250px;height:200px" />
I guess in your code you're getting error undefined index as you're accessing your variable out of while loop. Try below code:
<?php
$paring = 'SELECT * FROM postitus ORDER BY id';
$valjund = $yhendus->query($paring);
while($rida = mysqli_fetch_assoc($valjund)) : ?>
<img class='img-thumbnail' alt='city' src='<?php echo $rida['pilt']; ?>' style="width:250px;height:200px" />
<?php endwhile; ?>
You should recheck $rida. After you use "echo var_dump($rida);", you will know all parameters in array. You can choose trust array

Rel attribute not working from a mysql database

I have a series of images displayed on a page. They are being called from a mysql database. This part works fine. I then implemented a hover image with description using the following code:
...echo "<td>";?> <img src="<?php echo $row['image']; ?>" rel="imgtip[0]" height="100" width="125" title="<?php echo $row['title_tag']; ?>" /><?php echo "</td>";...
The rel="imgtip[0]" is the part that calls the hover image and description. This will only call the first image identified as '0'. I want it to change to rel="imgtip[1]", rel="imtip[2]" etc. so each hover image will relate to the one being called from the database.
I added a new field to the mysql database and added each rel as a new record. I then changed the php code to call the rel attribute from the database as follows:
...echo "<td>";?> <img src="<?php echo $row['image']; ?>" height="100" width="125" title="<?php echo $row['title_tag']; ?>" rel="<?php echo $row['img_tip'] ?>" /><?php echo "</td>";...
This time when I viewed the page no hover image appeared. Is there some way to call the rel attribute from the database so it will display the required hover images?
From your comment, it looks like the MySQL field img_tip contains the definition for rel= already. If that's the case, update your code as follows:
<img src="<?php echo $row['image']; ?>" height="100" width="125" title="<?php echo $row['title_tag']; ?>" <?php echo $row['img_tip'] ?> />

How to Serve 4 Images (Blobs) from Mysql using PHP in a single HTTP Response?

I am trying to display 4 images using PHP and MySQL database. I have to display the 4 images as rows.
I use the table cars with fields (id_car, car_image1, car_image2, car_image3, car_image4), with all the images being blob datatype.
$id = $_GET['id'];
$link = mysql_connect("localhost", "root", "");
mysql_select_db("cars_database");
$sql = "SELECT car_image1, car_image2, car_image3, car_image4 FROM cars WHERE id_car='$id'";
$result = mysql_query("$sql");
mysql_close($link);
while($row=mysql_fetch_array($result))
{
header('Content-type: image/jpeg');
echo $row['car_image1'];
echo $row['car_image2'];
echo $row['car_image3'];
echo $row['car_image4'];
}
I can only display 1 image and not the other images. Since I am a newbie to this technology I need help.
That's how HTTP works (at least, current version): you cannot have a URL that points to more than one resource simultaneously. Your script needs to send one image.
Simply, call it four times:
<img src="/get-image.php?id=1">
<img src="/get-image.php?id=2">
<img src="/get-image.php?id=3">
<img src="/get-image.php?id=4">
You need to define path for images.
<?php
while($row=mysql_fetch_array($result))
{
header('Content-type: image/jpeg');
?>
<img src="path<?php echo $row['car_image1']; ?>" alt="" />
<img src="path<?php echo $row['car_image2']; ?>" alt="" />
<img src="path<?php echo $row['car_image3']; ?>" alt="" />
<img src="path<?php echo $row['car_image4']; ?>" alt="" />
<?php
}
?>

Can't get image to display in dynamic pages

I can't seem to get my image to display properly. Previously, I have used the following code snippet and it worked perfectly.
catalog.php (worked perfectly):
<p class="image">
<a href="synopsis.php?id=<?php echo $row['id']; ?>">
<img src="getImage.php?id=<?php echo $row['id']; ?>" alt="" width="175" height="200" />
</a>
</p>
synopsis.php (not displaying image at all):
<?php
$id = $_GET['id'];
...?>
<p class="image">
<img border="0" class="floatleft" src="getImage.php?id=<?php echo $row['id']; ?>" width="250" height="400" />
<?php echo $row['synopsis']; ?>
</p>
where getimage.php:
<?php
$id = $_GET['id'];
$link = mysql_connect("localhost", "root", "");
mysql_select_db("dvddb", $link);
$sql = "SELECT dvdimage_path FROM dvd WHERE id=$id";
$result = mysql_query($sql, $link);
$row = mysql_fetch_assoc($result);
mysql_close($link);
header("Content-type: image/jpeg");
echo file_get_contents($row['dvdimage_path']);
?>
Any idea why can't I display this image?
EDIT 1:
So after debugging, I got an error message:
Undefined index: id in C:\xampp\htdocs\synopsis.php on line 106
so i went to add the following code into the php code just before echo $row['id']:
<p>getImage.php?id=<?php error_reporting(0); echo $row['id']; ?></p>
However,
the paragraph i got was just getImage.php?id=.
Then, i went into synopsis.php -> <img border="0" class="floatleft" src="getImage.php?id=<?php echo $row['id']; ?>
and changed that into:
<img border="0" class="floatleft" src="getImage.php?id=2">
Again, same problem happens, where i can't get the specific image out.
I suspect something is wrong with my getimage.php file. However, this getimage.php file has been working fine for other pages when i use the snippet.
My requirements are very simple:
In catalog.php, i populate images and text from dvd database using a while loop. Then, each of these images has got their specific primary ID. when i click the the images, they will go to the link: synopsis.php?id="primaryid" Then, using this "primaryid" i should be able use getimage.php?"primaryid" to generate an image on synopsis.php page.
EDIT 2:
actually, i made a syntax error somewhere. So this line:
<img border="0" class="floatleft" src="getImage.php?id=2">
is working perfectly, this means the fault lies in somewhere that i cant echo 'id' out correctly.
EDIT 3:
I have included the links to the relevant source code:
catalog.php
synopsis.php
getimage.php
sortmenu.css
style.css
database in xml format
Questions to ask yourself:
Is it really required that you use a php script to mimic the image? If not, just use the image path.
Is there any output before the header(); function in the getimage.php file? Even just a space before the
Is the image actually a JPEG?
Are there any errors coming up when you go to getimage.php?id=ID in your browser?
In synopsis.php you are getting the id from the querystring but then trying to use a database value. I cant see all of your code so im posting solutions to cover two scenarios
src="getImage.php?id=<?php echo $id; ?>"
or
src="getImage.php?id=<?php echo $row[$id]; ?>"

PHP echo function inside a HTML link

I have a PHP echo function inside of a HTML link, but it isn't working. I want to have an image location, defined in img src, be in part of the clickable link of the image. The page will have multiple images doing the same thing, so I am trying to use PHP to automate this.
<a href="http://statuspics.likeoverload.com/<?php echo $image; ?>">
<img src="<?php $image=troll/GrannyTroll.jpg?>" width="100" height="94" />
</a>
Turn
<?php $image=troll/GrannyTroll.jpg?>
into
<?php echo "troll/GrannyTroll.jpg"; ?>
?
Or provide more details on what you are trying to achieve.
Also, you might consider urlencode-ing some of those URL parameters.
Edit:
So you might try setting the variable beforehand:
<?php $image = "troll/GrannyTroll.jpg"; ?>
<img src="<?php echo $picture; ?>" width="100" height="94" />
So now i understand what you are trying to do.
One error is that you didn't enclose $image=troll/GrannyTroll.jpg with quotes like this:
$image = 'troll/GrannyTroll.jpg';
The second error is that you do it in the wrong order, you have to define $image first, before you use it.
That's what I believe you want to do:
<?php
$image = "troll/GrannyTroll.jpg";
?>
<img src="<?php echo $image; ?>" width="100" height="94"/>

Categories