Loading images to MySQL

images php coding issues or problems here.

Moderators: macek, egami, gesf

vaavi8r
New php-forum User
New php-forum User
Posts: 2
Joined: Mon Nov 17, 2003 9:06 am

Loading images to MySQL

Postby vaavi8r » Mon Nov 17, 2003 9:10 am

Self - proclaimed "newbie"...............

I am attempting to allow my members to upload an image along with a report and have the image show on an index page along with the data from the report.

I have a column in the table titled 'image' and it is set up as a BLOB NOT NULL.

The following is the FORM script:



PHP:
--------------------------------------------------------------------------------
<HTML>
<HEAD>
<BODY background="sky01.jpg">
<TITLE>PIREP INPUT FORM</TITLE>
<center><h2>AUTOMATED PIREP REPORTING FORM</h2></center>
<b>Please complete all information in the formats described next to the input boxes. Your data will be automatically added to your flight log and your totals updated.<P>
<?
$today = getdate();
$month = $today['mon'];
$day = $today['mday'];
$year=$today['year'];
?>

<form method="post" action="script.php">
DATE: <input type="text" name="date" value="<? echo $year?>-<? echo $month ?>-<? echo $day; ?>"><B>FORMAT='YYYY-MM-DD'</b><br>
NAME: <input type="text" name="name"><br>
MACID: <SELECT name="macid">
<OPTION SELECTED>---
<OPTION>MAC100
<OPTION>MAC101
<OPTION>MAC102
<OPTION>MAC103
<OPTION>MAC104
<OPTION>MAC496
<OPTION>MAC111
<OPTION>MAC116
<OPTION>MAC117
<OPTION>MAC127
<OPTION>MAC187
<OPTION>MAC188
<OPTION>MAC189
<OPTION>MAC190
<OPTION>MAC191
<OPTION>MAC192
<OPTION>MAC193
<OPTION>MAC194
<OPTION>MAC195
</select><br>
FLIGHT TIME: Enter as a decimal, rounded to one decimal place. <br>If you are flying a charter flight, please double your hours before entering the total here.<br>HOURS:<input type="text" name="hours"><br>
TYPE OF FLIGHT: <SELECT name="type">
<OPTION SELECTED>Macnet
<OPTION>Bushnet
<OPTION>Vatsim
<OPTION>Offline
<OPTION>Charter
<OPTION>Codeshare
<OPTION>Club Event
</select><BR>
DEPARTURE: <input type="text" name="departure"><b>USE ICAO CODE ONLY</b><br>
DESTINATION: <input type="text" name="destination"><b>USE ICAO CODE ONLY</b><br>
CHARTER #: <input type="text" name="charterno"><br>
CHARTER PAY: $<input type="text" name="charterdoll"><br>
COMMENT: <br><TEXTAREA name="comment" rows="10" cols="30"></TEXTAREA><br>

<form method="post" action="script.php" enctype="multipart/form-data">
SCREENSHOT:<input type="file" name="image"> <P>
If all information is correct, please click:
<input type="submit" name="submit" value="SUBMIT ">
</form>
</html>


--------------------------------------------------------------------------------



SCRIPT.php looks like this:


PHP:
--------------------------------------------------------------------------------
<HTML>
<body background="sky01.jpg">
<a href="http://www.margaritaair.com/roster.html">BACK TO ROSTER</a><P>
</html>

<?php
$db = mysql_connect("localhost", "tddftegh","scarlet");
mysql_select_db("margaritaair_com",$db);

$date=$_POST['date'];
$name=$_POST['name'];
$macid=$_POST['macid'];
$hours=$_POST['hours'];
$type=$_POST['type'];
$departure=$_POST['departure'];
$destination=$_POST['destination'];
$charterno=$_POST['charterno'];
$charterdoll=$_POST['charterdoll'];
$comment=$_POST['comment'];
$image=$_POST['image'];

$sql = "INSERT INTO flightlog (date, name, macid, hours, type, departure, destination, charterno, charterdoll, comment, image) VALUES ('$date', '$name', '$macid','$hours','$type','$departure','$destinati
on','$charterno','$charterdoll','$comment','$image
')";
$result = mysql_query($sql);
if ($result) {
echo ("PIREP recorded successfully");
}

MySQL_close()
?>
<P>

</HTML>
--------------------------------------------------------------------------------



And the index.php looks like this:


PHP:
--------------------------------------------------------------------------------
<html>
<head>
<body background="sky.jpg">
<a href="http://www.margaritaair.com/index.html">MAIN PAGE</a><P>
<A href="http://www.margaritaair.com/roster.html">ROSTER</a><P>
<a href="update.php">UPDATE / DELETE RECORD</a><br>

<?
include("dbinfo.inc.php");
mysql_connect(localhost,$username,$password);
mysql_select_db ("margaritaair_com");
$query = "SELECT SUM(hours) AS total FROM flightlog WHERE macid='MAC100'";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_assoc($result);
mysql_close();
?>

</html>

<?
include("dbinfo.inc.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM flightlog WHERE macid='MAC100' ORDER BY date DESC";
$result=mysql_query($query);



$num=mysql_numrows($result);
$date = date('j-m-y');
mysql_close();

echo "<b><h2><center>MAC100 FLIGHT LOG</h2></center></b><br>";
echo "<B><center>PILOT NAME: Todd Lucas<br></center>";
echo "<b><center>HOMETOWN: Lexington, SC<br></center>";
echo '<font size="+2">Total Hours Flown: '.$row['total'];
?>
<table border="2" cellspacing="2" cellpadding="2">
<tr>
<th><font face="Arial, Helvetica, sans-serif">ID</font></th>
<th><font face="Arial, Helvetica, sans-serif">MACID</font></th>
<th><font face="Arial, Helvetica, sans-serif">Name</font></th>
<th><font face="Arial, Helvetica, sans-serif">Date</font></th>
<th><font face="Arial, Helvetica, sans-serif">Type</font></th>
<th><font face="Arial, Helvetica, sans-serif">Departure</font></th>
<th><font face="Arial, Helvetica, sans-serif">Destination</font></th>
<th><font face="Arial, Helvetica, sans-serif">Time</font></th>
<th><font face="Arial, Helvetica, sans-serif">Charter #</font></th>
<th><font face="Arial, Helvetica, sans-serif">Charter Pay</font></th>
<th><font face="Arial, Helvetica, sans-serif">Comment</font></th>
<th><font face="Arial, Helvetica, sans-serif">Image</font></th>

</tr>

<?
$i=0;
while ($i < $num) {
$id=mysql_result($result,$i,"id");
$macid=mysql_result($result,$i,"macid");
$date=mysql_result($result,$i,"date");
$name=mysql_result($result,$i,"name");
$type=mysql_result($result,$i,"type");
$departure=mysql_result($result,$i,"departure");
$destination=mysql_result($result,$i,"destination");
$hours=mysql_result($result,$i,"hours");
$charterno=mysql_result($result,$i,"charterno");
$charterdoll=mysql_result($result,$i,"charterdoll");
$comment=mysql_result($result,$i,"comment");
$image=mysql_result($result,$i,"image");
$datePosted = "$date";
$formatted = date("M-d-Y", strtotime($datePosted));
$departure = strtoupper($departure);
$destination = strtoupper($destination);
?>

<tr>
<td><font face="Arial, Helvetica, sans-serif"><a href="update.php"><? echo "$id"; ?></a></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$macid"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$name"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? if (!$date) { echo "&nbsp;"; } else { echo $formatted; } ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? if (!$type) { echo "&nbsp;"; } else { echo $type; } ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? if (!$departure) { echo "&nbsp;"; } else { echo $departure; } ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? if (!$destination) { echo "&nbsp;"; } else { echo $destination; } ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$hours"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? if (!$charterno) { echo "&nbsp;"; } else { echo $charterno; }?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? if (!$charterdoll) { echo "&nbsp;"; } else { echo $charterdoll; } ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? if (!$comment) { echo "&nbsp;"; } else { echo $comment; } ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? if (!$image) { echo "N/A"; } else { echo ? $image; } ?></font></td>
</tr>

<?
++$i;
}
echo "</table>";

?>
<?
include("dbinfo.inc.php");
mysql_connect(localhost,$username,$password);
mysql_select_db ("margaritaair_com");
$query = "SELECT SUM(charterdoll) AS charterpay FROM flightlog WHERE macid='MAC100'";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_assoc($result);
echo '<font size="+2">Total Charter Pay: $'.$row['charterpay'];
mysql_close();
?>


--------------------------------------------------------------------------------




The information seems to be getting to the table, but is not being displayed in the index. What appears is the path to the image on my hard drive. You can view these pages at http://www.margaritaair.com/test1.php
http://www.margaritaair.com/flightlog/index.php

Any help would be appreciated.

User avatar
Redcircle
Moderator
Moderator
Posts: 830
Joined: Tue Jan 21, 2003 10:42 pm
Location: Michigan USA
Contact:

Postby Redcircle » Tue Nov 18, 2003 1:11 am

I would reccomend using the filesystem to store images. It is a much faster way to do things. If you keep the images in the database then there will be more database calls and speed will be affected. What I would do is keep them on the filesystem in a directory and just keep the path of the images in the database instead of the actual image in the database.

vaavi8r
New php-forum User
New php-forum User
Posts: 2
Joined: Mon Nov 17, 2003 9:06 am

Postby vaavi8r » Tue Nov 18, 2003 5:13 am

Thanks, yes I have changed the script to store the images in
another folder and call the url of the image from the database.

Thanks for the help, guys! :lol:

bonkers
New php-forum User
New php-forum User
Posts: 46
Joined: Wed Mar 12, 2003 7:17 pm

Postby bonkers » Thu Nov 27, 2003 2:38 am

That's what I've done to on my website, http://www.carsfx.com. The images are stored in a folder and named by the unique name of the car and the username. I just combine the both to retrieve the picture. It also allows me to manipulate the images with imagemagic via exec();

User avatar
ruturajv
php-forum Super User
php-forum Super User
Posts: 1280
Joined: Sat Mar 22, 2003 9:42 am
Location: Mumbai, India
Contact:

Postby ruturajv » Tue Aug 31, 2004 9:34 pm

considering that you are using apache....


http://httpd.apache.org/docs/misc/FAQ.html#image-theft


Return to “PHP coding => Images”

Who is online

Users browsing this forum: No registered users and 1 guest

cron