troubleshooting Question

PHP - photo gallery upload problem

Avatar of JohnMac328
JohnMac328Flag for United States of America asked on
PHPMySQL Server
5 Comments1 Solution486 ViewsLast Modified:
I found this example code of an image gallery that connects to a mysql database.  I am trying to use the add a photo portion.  I tested the database connection separately and it is fine.  For some reason the page just does not trigger the form action.  If I change a table field name it does not throw an error.  The built in error message also does not display an error.  The page refreshes with no errors and no images are uploaded.  The only time I see the image is when I select it and then it appears in the image preview section.  Any help is appreciated.
<?php include("include.php"); ?>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
	<title><?php echo $titleheader;?> - Add a pic to databse</title>
	<script language="JavaScript" type="text/javascript">
	function check() {
	//here we check if the password is in both fields the same
	if(document.add.pw.value != document.add.checkpw.value) {
	alert("Please enter the same password in both fields");
	return false;
	}else{
	return true;
	}
	}
	function picpreview() {
	// open a new window for preview the full pic
	var pic = document.add.filename.value;
	 window.open("add.php?action=picpreview&pic=" + pic,"","fullscreen");

	
	}
	</script>
		<link rel="stylesheet" type="text/css" href="index.css">
</head>

<body>
<h1>.:Add:.</h1>
<?php 
$query = getenv("QUERY_STRING");
parse_url($query);
if($action == "picpreview") {	//this is the preview routine
$pic = stripcslashes($pic);
$info = getimagesize($pic);
echo "<table cellpadding='10'>
<tr>
	<td align='left'><b>Preview</b><br><br><a href='#' onclick='window.close()'>Close window</a></td>
	<td align='right'><img src='$pic' $info[3]></td>
</tr>
</table>";
}



if($action == "") {		//main routine, its all just html code so i dont explain
echo'<form action="add.php?action=add" method="post" enctype="multipart/form-data" name="add" onsubmit="return check()">

  <table width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr> 
      <td width="27%">Choose file</td>
      <td width="48%"> 
        <input type="file" name="filename" accept="image/jpeg" onChange="document.images.preview.src = document.add.filename.value;">
      </td>
      <td width="25%"><a href="#" onclick="picpreview()">Preview</a></td>
    </tr>
    <tr> 
      <td width="27%">Who?</td>
      <td width="48%"> 
        <input type="text" name="who">
      </td>
      <td rowspan="6"><img name="preview" id="preview" width="250"></td>
    </tr>
    <tr> 
      <td width="27%"> 
        <p>Description:</p>
      </td>
      <td width="48%"> 
        <textarea cols="50" rows="12" name="descr"></textarea>
      </td>
    </tr>
    <tr> 
      <td width="27%">Author/Copyright&nbsp;</td>
      <td width="48%"> 
        <input type="text" name="autor">
      </td>
    </tr>
    <tr> 
      <td width="27%">Password<br> (you can leave empty, so its not protected)</td>
      <td width="48%"> 
        <input type="Password" name="pw">
      </td>
    </tr>
    <tr> 
      <td width="27%">Confirm</td>
      <td width="48%"> 
        <input type="Password" name="checkpw">
      </td>
    </tr>
    <tr> 
      <td width="27%"> 
        &nbsp;
      </td>
      <td width="48%"> 
        <input type="submit" name="submit" value="Go...">
      </td>
    </tr>
  </table>
</form>
';

}


if($action == "add") {			//here we add the file to the database
$db = connect_database();		//connect database
$datahex = gethex($filename);	//make the hex of the pic
$newpicfile = createthumb($filename,$quality);// Now we create a thumbnail. So we have later, when we look at the pics, less traffic
$newpicdatahex = gethex($newpicfile); //make the hex of the thumb
@unlink($newpicfile);
$who = prepare($who,"no");				//we prepare the text for the database (removing tags etc., so that there is no possibility for hackers ;-)
$descr = prepare($descr,"yes");
$author = prepare($author,"no");
//this is the mysql query
$cmd = "INSERT INTO '$db_table' ('id', 'who', 'descr', 'autor', 'stamp', 'pic', 'thumb', 'pw') VALUES ('', '$who ', '$descr', '$autor', NOW(NULL), '$datahex', '$newpicdatahex', '$pw')"; 
$result = mysql_db_query($db_name,$cmd); //query to the database
// if the query was successfull...
if($result) { 
echo "<p>Picture added sucessfully<br><a href='index.php'>Go on</a></p>"; 
}else{ 		//if not
echo "<p>Error:<br>";
echo mysql_error($db);
}

mysql_close($db); //close database connection

}





?>


</body>
</html>

Open in new window

ASKER CERTIFIED SOLUTION
pkahuria

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros