Jump to content

Using a single php page to display images from Multiple Mysql tables

- - - - -

  • Please log in to reply
3 replies to this topic

#1
whitestar

whitestar

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts
Hello

I need some help I have a page "show.php" that I use to display the profile image of my subscribers. It gets the subscriber_id from the insert.php page and then retrieves the "subscriber_profile_image" from the database and table "subscriber_info" and then sends the image back to the insert.php page where it is viewed on the page. Now this all works perfectly but I have images in other tables eg: "administrator_info" in the column "admin_profile_image". But I need to know how to write the script to allow me to view images from the different tables all from the same show.php page, or do I have to create a show page for ever table?eg show_subscriber_image.php and then show_administrator_image.php.

The way I have it at the moment I can only display the subscriber image I know there is something not right aim just not sure what it is!

here is the script I have for the insert.php page that request and then displays the "subscriber_profile_image" and "administrator_profile_image".
<html>

	<body>

	<img src="show.php?subscriber_id=<?php echo $_POST['subscriber_id']; ?>" />

	</body>

	</html>
& then below that is
<html>

	<body>

	<img src="show.php?admin_id=<?php echo $_POST['admin_id']; ?>" />

	</body>

	</html>

show.php script
<?php


	$username = "";

	$password = "";

	$host = "localhost";

	$database = "the_secrets_out";

	$table = "subscriber_info";

	$column = "subscriber_profile _image";

	$sort = "subscriber_id";

	


	@mysql_connect($host, $username, $password) or die("Can not connect to database: ".mysql_error());


	@mysql_select_db($database) or die("Can not select the database: ".mysql_error());


	$id = $_GET[$sort];


	if(!isset($id) || empty($id)){

	die("Please select your image!");

	}else{


	$query = mysql_query("SELECT * FROM $table WHERE $sort='".$id."'");

	$row = mysql_fetch_array($query);

	$content = $row[$column];


	header('Content-type: image/jpg');

	echo $content;


	}


	?>


<?php


	$username = "";

	$password = "";

	$host = "localhost";

	$database = "the_secrets_out";

	$table = "administrator_info";

	$column = "administrator_profile_image";

	$sort = "admin_id";

	


	@mysql_connect($host, $username, $password) or die("Can not connect to database: ".mysql_error());


	@mysql_select_db($database) or die("Can not select the database: ".mysql_error());


	$id = $_GET[$sort];


	if(!isset($id) || empty($id)){

	die("Please select your image!");

	}else{


	$query = mysql_query("SELECT * FROM $table WHERE $sort='".$id."'");

	$row = mysql_fetch_array($query);

	$content = $row[$column];


	header('Content-type: image/jpg');

	echo $content;


	}


	?>

any help is greatly appreciated!

Edited by Orjan, 07 February 2011 - 04:05 AM.
Please use [php]-tags when posting php code


#2
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#
at first, you need to choose what to display, an admin picture or an subscriber picture, preferably with an if. on the other hand, why are you separating the images in different tables? wouldn't it be better to have all imgs in one table and just reference to it's number in db?

beside that, you can not have any whitespace whatsoever outside your <?php ?> tags if you want it to work. remove the closing and reopening in the middle for example.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#3
whitestar

whitestar

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts
You have a good point and its actually how I wanted to do it at first but I couldnt figure out how to add the image to the "images" table and then add the "image_id" into the "subscriber_info" table at the same time maybe you cauld give me some advice for example if I use this script on my "add.php" page. to add the image into a table called "images". but I also send the "subscriber_id" to the "insert.php" page

<input type="hidden" name="MAX_FILE_SIZE" value="102400" >

          <input name="image" type="file" class="style1" accept="image/jpeg" />

          <input type="hidden" name="subscriber_id" value="<?php echo $row_subscriber_view['subscriber_id']; ?>" />

          <input type="submit" value="Submit" >


and then I use this script on the "insert.php" page to insert the image into the "images" table. How will I update the "subscriber_info" table with the "image_id" from the "images_table" at the same time?

<?php 

// Create MySQL login values and 

// set them to your login information.

$username = "Myusername";

$password = "******";

$host = "localhost";

$database = "the_secrets_out";


// Make the connect to MySQL or die

// and display an error.

$link = mysql_connect($host, $username, $password);

if (!$link) {

    die('Could not connect: ' . mysql_error());

}


// Select your database

mysql_select_db ($database);  

// Make sure the user actually 

// selected and uploaded a file

if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) { 


      // Temporary file name stored on the server

      $tmpName  = $_FILES['image']['tmp_name'];  

       

      // Read the file 

      $fp      = fopen($tmpName, 'r');

      $data = fread($fp, filesize($tmpName));

      $data = addslashes($data);

      fclose($fp);

      


      // Create the query and insert

      // into our database.

      $query = "INSERT INTO images";

      $query .= "(image) VALUES ('$data')";

      $results = mysql_query($query, $link);

      

      // Print results

      print "Thank you, your file has been uploaded.";

      

}

else {

   print "No image selected/uploaded";

}


// Close our MySQL Link

mysql_close($link);

?>

Edited by whitestar, 07 February 2011 - 10:47 PM.


#4
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
  • Location:New York, NY
whitestar, please use [noparse]
 ... 
[/noparse]
tags when posting php code.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users