Link to home
Start Free TrialLog in
Avatar of shuboarder
shuboarderFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Help setting up mysql profiles with photos

Hi,

I am new to php and mysql and am looking for a free script that can achieve the following:

1. Form that enables profile creation: e.g. name, dob, details password, etc. and photo to be submitted to a mysql database.

2. A page which displays all the photos in the mysql database that when clicked will take you to the appropriate profile.

I'll be looking at editing existing profiles later, but any help on setting up the 2 points above will be greatly appreciated!

Thanks!
Avatar of Muhammad Wasif
Muhammad Wasif
Flag of Pakistan image

Avatar of shuboarder

ASKER

Hi wasifq,

I should have mentioned I have actually been through a number of tutorials already, including the ones listed which do not cover my question directly. Basically a google search on "php mysql tutorials" was how I found them.

Thanks for the reply though :)
create a unique id for each profile either by mysql AUTO_INCREMENT or php time();

then send the unique id thru the POST or GET methods when you click on the profile.
on the profile page, use something like this:

$sql = "
  SELECT * FROM table
  WHERE id=". $_GET['id'] .";


Make sure to add security as this can easily be hacked the way i have showed you, although it's the most basic.
Hi Oscurochu,

thanks for the advice, but I don't quite understand what you're getting at...

Here is my code so far....

HTML FORM:

<body>

<form action="insert.php" method="post" enctype="multipart/form-data">
  <p class="style1">First Name:       
    <input type="text" name="first">
    <br>
Last Name:
<input type="text" name="last">
<br>
Phone:
<input type="text" name="phone" />
<br>
Mobile:
<input type="text" name="mobile">
<br>
Fax:
<input type="text" name="fax">
<br>
E-mail:
<input type="text" name="email">
<br>
Web:
<input type="text" name="web">
<br>
Photo:
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input type="file" name="photo">
</p>
  <p>
  <span class="style1">
  <input type="submit" name="Submit" value="Submit!">
  </span><br>
    </p>
</form>

</body>

insert.php:

<p>
  <?
$username="username";
$password="password";
$database="database";

$first=$_POST['first'];
$last=$_POST['last'];
$phone=$_POST['phone'];
$mobile=$_POST['mobile'];
$fax=$_POST['fax'];
$email=$_POST['email'];
$web=$_POST['web'];
$photo=$_POST['photo'];

mysql_connect('host',$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query = "INSERT INTO Main VALUES ('','$first','$last','$phone','$mobile','$fax','$email','$web','$photo')";
mysql_query($query);

mysql_close();

?>

output.php

<?
$username="username";
$password="password";
$database="database";

mysql_connect("host",$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM Main";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

echo "<b><center>Database Output</center></b><br><br>";

$i=0;
while ($i < $num) {

$first=mysql_result($result,$i,"first");
$last=mysql_result($result,$i,"last");
$phone=mysql_result($result,$i,"phone");
$mobile=mysql_result($result,$i,"mobile");
$fax=mysql_result($result,$i,"fax");
$email=mysql_result($result,$i,"email");
$web=mysql_result($result,$i,"web");
$photo=mysql_result($result,$i,"photo");

echo "<b>$first $last</b><br>Phone: $phone<br>Mobile: $mobile<br>Fax: $fax<br>E-mail: $email<br>Web: $web<br><br>Photo: $photo<br><hr><br>";

$i++;
}

?>

Data is being added to the database just fine, however, the photo part is not working.
How can I adjust my code to get the photo to upload to my host and then have a page display all the photos that when clicked take you to the respective profile.

Hope this makes things clearer...

Thanks for your help so far!
Avatar of Eddie Shipman
What is your FIELDTYPE for your photo field?

To be honest with you, it is very inefficient to store images in a datbase. You should rethink your plan and
store the binary images in a location on your server and store the location in the database. This would be
a whole lot less headache on both the posting and viewing sides of the equation.
Hi Eddie,

yes I realised this...

The field type is a MEDIUMBLOB.

Ideally I want the photo to upload to a folder (lets call it upload) with a filename equal to the id number that it is uploaded with.

Then I would like a page that displays all the photos so that when a photo is clicked it takes you to the relevant profile.
Then you have to save your uploaded photos first, then store the location in the
database after saving the file, unless you know the name of the file before saving.

Not that difficult to do. I've even seen some free scripts that do this on hotscripts.com

Hi Eddie,

thanks. Can you post the link to a script that achieves this?
Nothing specific but you can find one by searching these:
http://www.hotscripts.com/PHP/Scripts_and_Programs/Image_Galleries/index.html
While those may not be exactly what you're looking for, the code behind one or more of them is usable
in your situation.
Hi Eddie,

thanks for the link, but after searching through the available scripts I really cannot find anything that is similar to my situation - which I find unbelievable!
I will find one for you.
All the scripts I saw, stored the filename in the database and the image in a directory.

But, you could base64 encode the photos and store them as longtext in the database.

Then, when you show them, decode them back before presenting them.

Not difficult and easier to manage than BLOBs.
Another option I found on PHPBuilder.com forums said this:

You don't need to base64 encode and decode to store the image, you can load it into your script, then use mysql_escape_string on it, then do a direct insert. And no, you don't use stripslashes when you read it back out.

Or you can load the file directly into the db like:
UPDATE files SET data=LOAD_FILE($pathToFile) WHERE fileid=55
Hi Eddie,

No, i shouldn't really be storing the photos in the database, but I couldn't find an example that would allow me to upload the photos to a specific directory as the profiles are created.

Ideally, the uploaded photo should automatically rename itself to be the same as the record id.

i.e. When person 24 creates a new profile the photo (whatever it is called) will be renamed 24.jpg

I should then have a page that displays all the .jpgs and when any particular photo is clicked it takes you to the profile details.

ASKER CERTIFIED SOLUTION
Avatar of Eddie Shipman
Eddie Shipman
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
A gallery of uploaded photos is not that hard, either. There are lots of gallery scripts out there.
Forced accept.

Computer101
EE Admin