Jump to content

Image and hyperlink


MDanz

Recommended Posts

Ok i've learnt how to store images into a database.

 

What i missed out was submitting a hyperlink with that image. 

 

So i've created a hyperlink text field rite next to my browse.

 

here is the php for insert.

 

<?php

 

$username = "";

$password = "";

$host = "localhost";

$database = "";

 

 

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

if (!$link) {

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

}

 

 

mysql_select_db ($database);

 

 

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

 

   

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

     

     

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

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

      $data = addslashes($data);

      fclose($fp);

     

 

      $query = "INSERT INTO tbl_images ";

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

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

     

     

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

     

}

else {

  print "No image selected/uploaded";

}

 

mysql_close($link);

?>

 

where do i add to assign a hyperlink? I just made a field in the database known as hyperlink.  So the image also has a hyperlink with it.  How do i use the submit button to insert it with the image.

 

heres the form with the submit button

 

<?php

session_start();

 

if ($_SESSION['username'])

 

echo "Welcome, ".$_SESSION['username']."!<br><a href='Logout.php'>Log Out</a>

 

<form enctype='multipart/form-data' action='insert.php' method='post' name='changer'>

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

<input name='image' accept='image/jpeg' type='file'>

<input name='Link' type='text'>

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

"

 

;

else

die("You must be logged in!");

 

?>

 

I've used Link as a textfield for typing in the hyperlink.  also what would be the right field type/attributes?.... text?

Link to comment
https://forums.phpfreaks.com/topic/168367-image-and-hyperlink/
Share on other sites

VARCHAR is enough unless you want to store mysterious attributes longer than 256 characters :). The hyperlink can be saved in the same query:

 

mysql_query('INSERT INTO tbl_images (`image`, `hyperlink`) VALUES(\''.$data.'\', \''.$_POST['Link'].'\')');

 

Of course you should validate the content of "Link" field to ensure that the user really sends a hyperlink with it.

Link to comment
https://forums.phpfreaks.com/topic/168367-image-and-hyperlink/#findComment-888157
Share on other sites

VARCHAR is enough unless you want to store mysterious attributes longer than 256 characters :). The hyperlink can be saved in the same query:

 

mysql_query('INSERT INTO tbl_images (`image`, `hyperlink`) VALUES(\''.$data.'\', \''.$_POST['Link'].'\')');

 

Of course you should validate the content of "Link" field to ensure that the user really sends a hyperlink with it.

 

thx

Link to comment
https://forums.phpfreaks.com/topic/168367-image-and-hyperlink/#findComment-888163
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.