Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A tutorial for storing paths to images in db (upload, display)

Status
Not open for further replies.

pugs421

Technical User
Joined
Nov 25, 2002
Messages
114
Location
US
Does anyoe know of a tutorial for allowing users to upload a picture and store the file path in db?
 
Hi,
Make a folder that is public for your images. The below sript calls up the path and inserts it into the img tag in html. ******************************************************
<?php

// open the connection
$conn = mysql_connect(&quot;localhost&quot;, &quot;&quot;, &quot;&quot;);

// pick the database to use
mysql_select_db(&quot;dbase&quot;,$conn);

// create the sql statement
$sql = &quot;SELECT picture FROM table&quot;;

// execute the sql statement
$result = mysql_query($sql, $conn) or die(mysql_error());

print &quot;<table border=1><tr><th>Picture</th></tr>&quot;;


// go through each row in the result set and display data
while ($newArray = mysql_fetch_array($result)) {
// give a name to the fields
$picture = $newArray['picture'];

//echo the results on screen
echo &quot;
<tr>
<td><img src=\&quot;$picture\&quot;></td>
</tr>&quot;;
}
print &quot;</table>&quot;;
?>
*****************************************************
Create a form that you can type in the image path to be posted to your database

<?php
session_start();
?>
<html>
<head>
<title>
Inserting info to database
</title>
</head>
<body>

<form method=&quot;post&quot; action=&quot;insert.php&quot;>
Path:<input type=&quot;text&quot; name=&quot;path&quot;><br>
<input type=&quot;Submit&quot; name=&quot;submit&quot; value=&quot;Enter information&quot;>
</form>

</body>
</html>
******************************************************
<?php

// open the connection
$conn = mysql_connect(&quot;localhost&quot;,&quot;&quot;,&quot;&quot;);

// pick the database to use
mysql_select_db(&quot;dbase&quot;, $conn);

// create the SQL statement
$sql = &quot;INSERT INTO table values ('$_POST[path]')&quot;;


if ($_POST[path] == &quot;&quot;)
{die (&quot;<P>Please Enter A Path!<P><FORM><INPUT TYPE=\&quot;BUTTON\&quot; VALUE=\&quot;BACK\&quot; onClick=\&quot;history.back()\&quot;></FORM>&quot;);}

else

//execute the statement
if (mysql_query($sql, $conn)) {
echo &quot;record added!&quot;;
}else{
echo &quot;something went wrong or username already in use&quot;;
}
?>

add just place theimage in manually or get a normal html file upload to load it up to your folder.


Reality is built on a foundation of dreams.
 
Thanks for the response. I am a beginner but I think I understand whats going on in the code. One thing I dont understand is that in the input form, a path to the file is supposed to be given...correct?

My goal is to let the user upload the picture and then have it displayed on their member info page, so they wouldn't be typing in the path to the image, just uploading it. Can that be done from the code you gave?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top