Link to home
Start Free TrialLog in
Avatar of dcatkin
dcatkin

asked on

PHP/Mysql search engine

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
Avatar of Giovanni G
Giovanni G
Flag of Italy image


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..
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
Avatar of Karitz
Karitz

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
---- 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.
Avatar of dcatkin

ASKER

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>
ASKER CERTIFIED SOLUTION
Avatar of Giovanni G
Giovanni G
Flag of Italy 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