Solved

PHP/Mysql search engine

Posted on 2003-10-30
9
795 Views
Last Modified: 2013-12-12
I have a php script that runs a search engine for old cars and parts on my site and I need to add a picture upload that corralates with the item that the people search for. So when the search locates and item in the database there will be a picture associated with the search item.
the url for the search page is here http://hp-car.com/cars/index.php

Thank you
David C. Atkin
0
Comment
Question by:dcatkin
9 Comments
 
LVL 14

Expert Comment

by:ThG
ID: 9653366

Good. You have two ways to do it, but for both ways you first need to learn very well this page:
http://www.php.net/manual/en/features.file-upload.php

After that, you choose wether to insert the picture to the database, or to store it on your local HD with some kind of indexed numbering.
The easiest way is to store the uploaded image in a dedicate directory with a name like car1.jpg, car2.jpg.. where the number is the row id of your matching database entry (you have an auto_increment field, do you?).
That's all i think..
0
 
LVL 3

Expert Comment

by:red010knight
ID: 9655117
Actually I think you are looking to have a picture display with the link you call up? not have a user upload a picture to the item they search for.

In that case what you really need to do is this:
In your MySQL database add a field to your item list that is searched called 'picLinks' then put the file location of the file there.

In your php/HTML script

you would have something like
<img src=<?php "/"". $itemdb["picLinks"] ."/"" ?>>

This way you don't need to have a file naming standard for the images you upload and so forth.
I think this what your asking for if I understand your question correctly.

Hope this helps, have fun coding!
Red010Knight
0
 
LVL 5

Expert Comment

by:Karitz
ID: 9655541
Merge both the suggestions of ThG (for the uploading part) and red010knight (for Link to image)

Just a question.....how many images do you need of each particular item? one, or more than one
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 6

Expert Comment

by:DoppyNL
ID: 9656031
---- quote ThG:
After that, you choose wether to insert the picture to the database, or to store it on your local HD with some kind of indexed numbering.
The easiest way is to store the uploaded image in a dedicate directory with a name like car1.jpg, car2.jpg.. where the number is the row id of your matching database entry (you have an auto_increment field, do you?).
That's all i think..
----

do NOT store files in your database!! it will cause a huge load on the database wich almost all databases can't handle.
It's a much better idea to store the files in some folder and put a reference to that file in your database.

except from storing files in the database I completely agree with the suggestions made to do what you need.
0
 

Author Comment

by:dcatkin
ID: 9659083
What I don't know is where to modify the code I have, I don't know if I should add the code here or to the other php script.

<?php
$usr = "dcatkin";
$pwd = "hellfire";
$db = "cars";
$host = "localhost";
$cid = mysql_connect($host,$usr,$pwd);
mysql_select_db($db);
if (!$cid) { print "ERROR: " . mysql_error() . "\n"; }
?>
<?php
$Make = $_POST["Make"];
$Year = $_POST["Year"];
$Model = $_POST["Model"];
$Price = $_POST["Price"];
$Description = $_POST["Description"];
$Name = $_POST["Name"];
$Address = $_POST["Address"];
$Email = $_POST["Email"];
$Phone = $_POST["Phone"];
$URL = $_POST["URL"];
$pclinks = $_Post["picLinks"];
if ($_SERVER['REQUEST_METHOD'] == "POST") {
$sql = " INSERT INTO Cars ";
$sql .= " ( Make, Year, Model, Price, Description, Name, Address, Email, Phone, URL, pclinks) VALUES ";
$sql .= " ('$Make', '$Year', '$Model', '$Price', '$Description', '$Name', '$Address', '$Email', '$Phone', '$URL','pclinks') ";
$sql = " INSERT INTO Cars";
$sql .= " ( Make, Year, Model, Price, Description, Name, Address, Email, Phone, URL, pclinks) VALUES ";
$sql .= " ('$Make', '$Year', '$Model', '$Price', '$Description', '$Name', '$Address', '$Email', '$Phone', '$URL','pclinks') ";
$result = mysql_query($sql, $cid);
if (mysql_error()) { print "Database ERROR: $sql " . mysql_error(); }
print "<br>";
print "<br>";
print "<center>";
print "<B>";
print "New Car Added";
print "<br>";
print "<br>";
print "<center>";
print "<B>";
print "Thank you for adding your car to our database";
print "<br>";
print "<br>";
print "<center>";
print "<B>";
print "Come back often.";
}
?>

<?php
   session_start();
   mysql_connect("localhost", "dcatkin", "hellfire")
     or die("(".mysql_errno().") Error:".mysql_error());
   mysql_select_db( "cars" )
     or die("(".mysql_errno().") Error:".mysql_error());
?>
<html>
<head>
<title>Upload</title>
<meta http-equiv="Content-Type" content=
"text/html; charset=iso-8859-1">
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);
//-->
</script>
<style type="text/css">
 body {
  background-color: #0066CC;
 }
 div.c5 {position:absolute; width:587px; height:342px; z-index:1; left: 220px; top: 143px;}
 strong.c4 {font-size: 120%}
 div.c3 {text-align: center}
 a.c2 {font-size: 144%}
 td.c1 {font-size: 144%}
</style>
</head>
<body>
<div class="c3">
<table width="800" border="0" summary="Upload">
<tr>
<td width="677" height="688" align="left" valign="top" bgcolor=
"#00FFFF">
<table width="100%" border="0" summary="Upload">
<tr>
<td height="682" valign="top" bgcolor="#00CCFF">
<table width="100%" border="0" bgcolor="#0033CC" summary=
"Upload">
<tr>
<td height="115"><img src="images/3dtext_21393.gif" alt="Hp-Car"
width="790" height="106"></td>
</tr>
</table>
<table width="120" border="0" align="left" summary="Hp-Car">
<tr>
<td width="114" height="38" align="center" bgcolor="#3300CC"
class="c1"><span class="c2"><a href=
"http://hp-car.com/index.html"><img src="buttons/home.gif" alt=
"parts/buttons/home.gif" width="100" height="37" border="0">
</a></span></td>
</tr>
<tr>
<td align="center" bgcolor="#3300CC" class="c3"><a href=
"http://hp-car.com/parts/index.php" class="c2"><img src=
"buttons/parts.gif" alt="buttons/parts.gif" width="100" height=
"37" border="0"></a></td>
</tr>
<tr>
<td align="center" bgcolor="#3300CC" class="c3"><a href=
"http://hp-car.com/info/index.html" class="c2"><img src=
"buttons/info.gif" alt="buttons/info.gif" width="100" height="37"
border="0"></a></td>
</tr>
<tr>
<td align="center" bgcolor="#3300CC" class="c3"><a href=
"http://hp-car.com/engine/index.html" class="c2"><img src=
"buttons/engine.gif" alt="buttons/engine.gif" width="100" height=
"37" border="0"></a></td>
</tr>
<tr>
<td align="center" bgcolor="#3300CC" class="c3"><a href=
"http://hp-car.com/users/index.html" class="c2"><img src=
"buttons/user.gif" alt="buttons/user.gif" width="100" height="37"
border="0"></a></td>
</tr>
<tr>
<td align="center" bgcolor="#3300CC" class="c3"><a href=
"http://hp-car.com/b+p/index.html" class="c2"><img src=
"buttons/body.gif" alt="buttons/body.gif" width="100" height="37"
border="0"></a></td>
</tr>
<tr>
<td align="center" bgcolor="#3300CC" class="c3"><a href=
"http://hp-car.com/cgi-bin/chat/chat.cgi" class="c2"><img src=
"buttons/chat.gif" alt="buttons/chat.gif" width="100" height="37"
border="0"></a></td>
</tr>
<tr>
<td align="center" bgcolor="#3300CC" class="c3"><a href=
"http://hp-car.com/forum/index.php" class="c2"><img src=
"buttons/forums.gif" alt="parts/buttons/forums.gif" width="100"
height="33" border="0"></a></td>
</tr>
<tr>
<td align="center" bgcolor="#3300CC" class="c3"><a href=
"http://hp-car.com/advert/index.html" class="c2"><img src=
"buttons/biz.gif" alt="parts/buttons/biz.gif" width="100" height=
"37" border="0"></a></td>
</tr>
<tr>
<td align="center" bgcolor="#3300CC" class="c3"><a href=
"http://hp-car.com/gbook/jgb.php3" class="c2"><img src=
"buttons/guest.gif" alt="buttons/guest.gif" width="100" height=
"37" border="0"></a></td>
</tr>
<tr>
<td height="37" align="center" bgcolor="#3300CC" class="c3"><a
href="http://hp-car.com/links/index.php" class="c2"><img src=
"buttons/links.gif" alt="parts/buttons/links.gif" width="100"
height="37" border="0"></a></td>
</tr>
<tr>
<td height="23" valign="top" bgcolor="#0000CC">
<div class="c1 c3"><a href="http://hp-car.com/cars/index.php"
class="c2"><img src="buttons/CARS.GIF" alt="Button" width="100"
height="37" border="0" lowsrc="/buttons/CARS.GIF"></a></div>
</td>
</tr>
<tr>
<td height="23" valign="top" bgcolor="#0000CC">
<div class="c1 c3"><a href=
"http://hp-car.com/cgi-bin/search/search.pl" target="_self"
class="c2"><img src="buttons/SEARCH.GIF" alt="Button" width="100"
height="37" border="0" lowsrc="/buttons/SEARCH.GIF"></a></div>
</td>
</tr>
<tr>
<td height="35" align="center" valign="top" bgcolor="#00CCFF"><a
href="http://www.redhat.com" class="c2"><img src=
"images/poweredby.png" alt="Button" width="88" height="31"
border="0"></a></td>
</tr>
</table>
<div id="Layer1" class="c5">
<div class="c3">
<table width="98%" border="0" bordercolor="#CCCCCC" bgcolor=
"#00FFFF" summary="Upload">
<tr>
<td height="26">
<?php
$DEBUG=1;
if (!isset($PHP_SELF)) $PHP_SELF=$_SERVER['PHP_SELF'];
// treat data
/* FYI : received data will be under the form :
$_POST[] = Array
(
   [MAX_FILE_SIZE] => 3000000
   [submit] => Send File
)
$_FILES[] = Array
(
   [userfile] => Array
       (
           [name] => ~cars.jpg
           [type] => image/pjpeg
           [type] => image/gif
           [type] => image/png
           [tmp_name] => C:\Documents and Settings\Administrator\My DocumentsTemp\php9A.tmp
           [error] => 0
           [size] => 16936
       )
)
*/
if (isset($_POST['submit'])) { // POSTed data present
 if ($DEBUG==1) echo "test: good POST, copying file...<BR>";
 $MAX_FILE_SIZE=$_POST['MAX_FILE_SIZE'];
 $freeSpace=$MAX_FILE_SIZE;
 if ($DEBUG==1) echo "test: file oversize received $freeSpace (0 means 'too big')<BR>";
 if ($freeSpace==0) echo "no space left<BR>";
 else {
   // get data
    $userfile=$_FILES['userfile'];
   // analyse data
   if (!isset($userfile)) { echo "Problem: no file uploaded"; exit; }
   // else we can continue
   if ($userfile['size']==0) { echo "Problem: Uploaded file is zero in length"; exit; }
   if ($DEBUG==1) echo "file type received : {$userfile['type']}<BR>";
   // NB : you lack a lot of image formats hereafter, particularly image/pjpeg and image/jpeg
   // I suggest to build an array of acceptable mime types and use in_array()
   if ($userfile_type != 'image/jpg') { echo "Problem: file is not an image"; exit; }
   if ($userfile_type != 'image/gif') { echo "Problem: file is not an image"; exit; }
   if ($userfile_type != 'image/png') { echo "Problem: file is not an image"; exit; }
   if ($DEBUG==1) echo '<pre>';
   if ($DEBUG==1) $upfile=$userfile['images'];
    else $upfile = "/home/hp-car/www/cars/images/".$upfile['images']; // _images doesn't exist
   if (move_uploaded_file($userfile['tmp_name'], $upfile)) { // typo again
     print "File is valid, and was successfully uploaded to $upfile.  Here's some more debugging info:\n";
     if ($DEBUG==1) print_r($_FILES);
     // fall-through
   } else {
     print "Possible file upload attack!  Here's some debugging info:\n";
     if ($DEBUG==1) print_r($_FILES);
     exit;
   }
   if ($DEBUG==1) echo '</pre>';
 } // space left
} // POST data received
if ($DEBUG==1) echo "test: getting out to HTML<BR>";
// display form
echo <<<EOS
<form enctype="multipart/form-data" action="$PHP_SELF" method="post"><input type="hidden" name="MAX_FILE_SIZE" value="3000000"> Send this file: <input name="userfile" type="file">
<input name="submit" type="submit" value="Send File">
</form>
EOS;
/* strange/weird :
</td>
</tr>
</table>
</div>
*/
?>
</td>
</tr>
</table>
</div>
<div class="c3">
<table width="98%" border="0" summary="Upload">
<tr>
<td><strong class="c4">A picture is worth 1,000 words, upload a
picture of the car you have for sale here.</strong></td>
</tr>
</table>
</div>
</div>
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
</body>
</html>
0
 
LVL 14

Accepted Solution

by:
ThG earned 500 total points
ID: 9660580

you need to modify both the submit form and this file.
In the form you add the well-known file input html tag. Here you'll need to handle the file uploaded, whose record is in $_FILES as shown in the link i gave you, at the top where it handles other $_POST data as well.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
incrementing radio button names and ids using php 5 37
Paypal 502 Error 3 66
Should I use subdomains or addon domains? 3 33
updating the date data 12 20
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now