zerey
zerey

Reputation: 871

Sending php variable to another php page

I need a little help with some coding. I have a page named index.php and a picture.php.

index.php

$pid = 0;

for($ctr = 1; $ctr <= 2; $ctr++)
{
    echo '<tr>';
    $pid++;

    echo '<td>';
    echo '<a id="various3" href="picture.php" title="try" idno="5"><img src="picture.php" width="150" height="210">';
    //echo '<br>'.$pagenm;
    echo '</td>';

    echo '</td>';
    echo '</tr>';
}

After $pid++ is executed, I want its value to be sent to picture.php, so that it can be used for a retrieving query.

picture.php

$host = 'localhost';
$user = 'root';
$pw = '';
$db = 'thepillar';

mysql_connect($host, $user, $pw);
mysql_select_db($db);
$sql = "select pics, ext from infopics where id='5'";
// mysql_real_escape_string($sql, mysql_connect);
$result = mysql_query($sql) or die('Bad query at 12!' . mysql_error());
while ( $row = mysql_fetch_array($result, MYSQL_ASSOC) ) {
    $db_img = $row['pics'];
    $type = $row['ext'];
}
$img = base64_decode($db_img); // print_r($db_img );
$img = imagecreatefromstring($img);
header("Content-Type: image/jpeg");
imagejpeg($img);
imagedestroy($img);

I have only assigned 5 as the id to be used to retrieve the image from the database. This is because I don't know how to fetch $pid from index.php and assign it to a variable which I can use in my SQL query.

Any help would be much appreciated. Thanks in advance.

Upvotes: 1

Views: 443

Answers (3)

Your Common Sense
Your Common Sense

Reputation: 157828

Don't store pictures in database, but save names only.
You won't need to pass anything anywhere and you'll avoid other silly mistakes from your code.

make your main page like this

<?php
$host = 'localhost';
$user = 'root';
$pw = '';
$db = 'thepillar';

mysql_connect($host,$user,$pw); 
mysql_select_db($db); 
$sql = "select pic from infopics limit 2";
$result = mysql_query($sql) or trigger_error(mysql_error().$sql); 
while($row = mysql_fetch_array($result,MYSQL_ASSOC))
{
  echo '<tr>';
  echo '<td>';
  echo '<a id="various3" href="/pictures/'.$row['pic'].'" title="try" idno="5">
        <img src="/pictures/thumbnails/'.$row['pic'].'" width="150" height="210">';
  echo '</td>';
  echo '</tr>';
} 
?>

that's all

Upvotes: 0

Joseadrian
Joseadrian

Reputation: 4374

index.php

for($ctr=1; $ctr<=2; $ctr++)
{
     echo '<tr><td><a id="various3" href="picture.php?id=', 
     // I use $ctr insteaf of $pid because is the same... starts with 1
     $ctr,'" title="try" idno="5"><img src="picture.php" width="150" height="210">',
     '</td></td></tr>';
}

picture.php

<?php
...
$sql = sprintf("select pics, ext from infopics where id='%d'", $_GET['id']);
...
?>

Upvotes: 1

Long Ears
Long Ears

Reputation: 4896

You could include it in the query string of picture.php, e.g.:

echo '<a id="various3" href="picture.php?id=', $pid, '" ... ';

Then in picture.php:

if (!isset($_GET['id']) || !ctype_digit($_GET['id'])) {
    // todo: some proper error handling
    exit;
}
$sql = "select pics, ext from infopics where id=" . $_GET['id'];

Upvotes: 1

Related Questions