Tell me what's wrong with this PHP code..

Looking for volunteers to join your project? Need help with a script but can't afford to pay? Want to offer your services as a volunteer to build up your portfolio? This is the place for you...

Moderator: General Moderators

Post Reply
davediamondprovo
Forum Newbie
Posts: 2
Joined: Wed Nov 26, 2003 5:32 pm

Tell me what's wrong with this PHP code..

Post by davediamondprovo »

I'm just learning PHP but I've got a client who needs some help with a small problem when attempting to add photos to a database.

IN BRIEF:

A user edits the contents of a number of textboxes and textareas in a form, adds a few photos and then saves the page. The page is displayed in a larger template where the photos are used.

THE PROBLEM:

Deleting existing photos with the DELETE hyperlinks on the page does not work. It's also not allowing them to ADD photos to the page.

THE MYSTERY:

I experimented with deleting the photos that appear on a clients page from the specified image directory and they were no longer displayed on the form. This is good because I then tried adding photos to that directory to see if they showed up in the form and they did. They also display properly on the template page. All is well.. almost. For some reason the author of the page put the DELETE and ADD photos buttons there but they don't work.

Could someone look at this code and tell me where it goes south?

Code: Select all

<?
($id && !$action)?($action = "Update"):($action = "Add");
?>  
<body bgcolor="#FFFFFF"><div align="center">
<form action="houses.php" method="post" enctype="multipart/form-data">
  <b><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Please submit 
  the House details below.</font></b><br>
  <br>
  <table width="400" border="0" cellpadding="3" cellspacing="0">
    <input type="hidden" name="id" value="<?=$row&#1111;"id"]?>">
    <tr> 
      <td align="right"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">House</font></td>
      <td> 
        <input name="house" value="<?=$row&#1111;"house"]?>" size="35" maxlength="55">
      </td>
    </tr>
    <tr> 
      <td align="right"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Member</font></td>
      <td> 
        <input name="member" value="<?=$row&#1111;"member"]?>" size="35" maxlength="44">
      </td>
    </tr>
    <tr> 
      <td align="right"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Director</font></td>
      <td> 
        <input name="director" value="<?=$row&#1111;"director"]?>" size="35" maxlength="55">
      </td>
    </tr>
    <tr> 
      <td align="right"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Address</font></td>
      <td> 
        <textarea name="address" cols="35" rows="3"><?=$row&#1111;"address"]?></textarea>
      
    </tr>
    <tr> 
      <td align="right"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Phone</font></td>
      <td> 
        <input name="phone" value="<?=$row&#1111;"phone"]?>" size="25" maxlength="25">
      </td>
    </tr>
    <tr> 
      <td align="right"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Email</font></td>
      <td> 
        <input name="email" value="<?=$row&#1111;"email"]?>" size="35" maxlength="55">
      </td>
    </tr>
    <tr> 
      <td align="right"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Location</font></td>
      <td> 
        <textarea name="location" cols="35" rows="3"><?=$row&#1111;"location"]?></textarea>
      </td>
    </tr>
    <tr> 
      <td align="right"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">URL</font></td>
      <td> 
        <input name="url" value="<?=$row&#1111;"url"]?>" size="35" maxlength="55">
      </td>
    </tr>
    <tr> 
      <td align="right"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Background</font></td>
      <td> 
        <textarea name="background" cols="35" rows="3"><?=$row&#1111;"background"]?></textarea>
      </td>
    </tr>
    <tr> 
      <td align="right"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Description</font></td>
      <td> 
        <textarea name="description" cols="35" rows="3"><?=$row&#1111;"description"]?></textarea>
      </td>
    </tr>
    <tr> 
      <td align="right"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">House 
        Size</font></td>
      <td> 
        <input name="house_size" value="<?=$row&#1111;"house_size"]?>" size="35" maxlength="55">
      </td>
    </tr>
    <tr> 
      <td align="right"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Facilites</font></td>
      <td> 
        <textarea name="facilities" cols="35" rows="3"><?=$row&#1111;"facilities"]?></textarea>
      </td>
    </tr>
    <tr> 
      <td align="right"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Staff</font></td>
      <td> 
        <textarea name="staff" cols="35" rows="3"><?=$row&#1111;"staff"]?></textarea>
      </td>
    </tr>
    <tr> 
      <td align="right"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Video 
        Link</font></td>
      <td> 
        <input name="video_link" value="<?=$row&#1111;"video_link"]?>" size="35">
      </td>
    </tr>
<!--    <tr> 
      <td align="right"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Reservations</font></td>
      <td> 
        <textarea name="reservations" cols="35" rows="3"><?=$row&#1111;"reservations"]?></textarea>
      </td>
    </tr>-->
<input type="hidden" name="reservations" value="">
    <tr> 
      <td align="right"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Reservations 
        Phone</font></td>
      <td> 
        <input name="reservation_phone" value="<?=$row&#1111;"reservation_phone"]?>" size="35">
      </td>
    </tr>
    <tr> 
      <td align="right"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Reservations 
        Email</font></td>
      <td> 
        <input name="reservation_email" value="<?=$row&#1111;"reservation_email"]?>" size="35">
      </td>
    </tr>
    <?
if(!$id)&#123;
	
	$this->filefields(7);
&#125; else &#123;
	?>
    <tr> 
      <td colspan="2" width="100%"><br><center>
        <table width="100%">
          <?
	$this->displaypics($row&#1111;"house"], $row&#1111;"id"]);
	?>
        </table><br>
      </td>
    </tr>
    <?
&#125;
?>
    <br>
  </table>
  <center>
    <input type="submit" name="action" value="<?=$action?>">
  </center>
</form>
</div>
<center>
  <a href="houses.php?action=view"><font face="Verdana, Arial, Helvetica, sans-serif" size="2">Back 
  to Admin</font></a> 
</center>

The form calls this page as well. It has more functions listed in it.

Code: Select all

<?

require("Template_class.php");

class houses extends Template&#123;
	
	function houses()&#123;
	
		$this->Template("localhost", "xxxxxxxx", "xxxxxxxx", "xxxxxxxx", "xxxxxxxx");
	&#125;

	function addhouse($vars, $files)&#123;
		
		$folder = $vars&#1111;"house"];

		foreach($vars as $key => $val) $argstr .= ""$val",";
		$argstr = $this->FixString($argstr);
		eval("\$this->SubmitInfo(".$argstr.");");

		$this->addpictures($folder, $files);
	&#125;
	
	function addpictures($folder, $files)&#123;

		foreach($files as $file => $var)&#123;
			if($var&#1111;"tmp_name"] != "none") ($argstr .= """.$var&#1111;"tmp_name"]."","".$var&#1111;"name"]."",");
		&#125;
		$argstr = $this->FixString($argstr);
		if(strlen($argstr) > 1) eval("\$this->SavePictures("".$folder."",".$argstr.");");
	&#125;
	
	function deletepicture($file)&#123;
		
		if(file_exists($file)) unlink($file);
	&#125;
	
	function viewhouses()&#123;
		
		$q = "SELECT id, house, director FROM $this->table";
		$r = $this->ExecuteQuery($q);
		
		?><body bgcolor="#EEEEEE">
		<table width="540" border="0" cellspacing="1" cellpadding="3" align="center">
		  <tr align="center"> 
		    <td width="195" bgcolor="#E7D6AD"><b><font face="Verdana, Arial, Helvetica, sans-serif" size="3">Tools</font></b></td>
		    <td width="164" bgcolor="#E7D6AD"><b><font face="Verdana, Arial, Helvetica, sans-serif" size="3">House</font></b></td>
		    <td width="163" bgcolor="#E7D6AD"><b><font face="Verdana, Arial, Helvetica, sans-serif" size="3">Director</font></b></td>
		  </tr>
		<?

		while($row = $this->HashQuery($r))&#123;
			
			?>
			  <tr> 
		    	<td width="195" bgcolor="#FAF3DC" align="center">
				  <a href="template.php?id=<?=$row&#1111;"id"]?>"><img src="images/buttons_03.gif" width="52" height="15" hspace="2" border="0"></a>
	    		  <a href="?action=edit&id=<?=$row&#1111;"id"]?>"><img src="images/buttons_05.gif" width="52" height="15" hspace="2" border="0"></a>
				  <a href="?action=delete&id=<?=$row&#1111;"id"]?>&folder=<?=urlencode($row&#1111;"house"])?>"><img src="images/buttons_07.gif" width="52" height="15" hspace="2" border="0"></a>
		    	</td>
			    <td width="164" bgcolor="#FAF3DC"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><?=$row&#1111;"house"]?></font></td>
			    <td width="163" bgcolor="#FAF3DC"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><?=$row&#1111;"director"]?></font></td>
			  </tr>
			<?
		&#125;
		
		$this->ReleaseResult($r);
		
		?>
		<tr>
 		 <tr align="center"> 
   			 <td colspan="3"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><a href="?action=New"><br>
    			  Add a House</a> | <a href="/index.php">Home Page</a></font></td>
  			</tr>
		</table>
		<?
	&#125;
	
	function viewdetail()&#123;
	
	&#125;
	
	function displaypics($folder, $id)&#123;
		
		$farray = $this->GetPictures($folder);
		($farray == "Error")?($count = 0):($count = count($farray));
		echo "<tr><center>";
		for($i = 0; $i < $count; $i++)&#123;
			?>
			<td align="center">
			<font face="verdana"><img src="<?=$folder."/".$farray&#1111;$i]?>" width="100" height="100"><br><a href="?action=delpic&id=<?=$id?>&file=<?=urlencode($folder."/".$farray&#1111;$i])?>">Delete</a>
			</td>
			<?
			if($i==3) echo "</tr><tr>";
		&#125;
		echo "</tr>";
		$this->filefields(7 - $count);
	&#125;
	
	function filefields($max)&#123;
	
		for($i = 1; $i <= $max; $i++)&#123;	
			?>
			<tr>
				<td><font face="verdana">Picture<?=$i?></td><td><input type="file" name="f_pic<?=$i?>"></td>
			</tr>
			<?
		&#125;
	&#125;
	
	function doform($id)&#123;
		
		if($id)&#123;
			
			$q = "SELECT * FROM $this->table WHERE id='$id'";
			$r = $this->ExecuteQuery($q);
			$row = $this->HashQuery($r);
			$this->ReleaseResult($r);
			if(!$row)&#123;
				$action = "Add";
				$row&#1111;"id"] = $id;
			&#125;
		&#125;
		
		include("form.php");
	&#125;
&#125;

if(!$action) ($action = "view");
unset($HTTP_POST_VARS&#1111;"action"]);
$h = new houses();

//include("../members/header.php");

switch($action)&#123;
			
	case "Add":
		$h->addhouse($HTTP_POST_VARS, $HTTP_POST_FILES);
		$h->viewhouses();
		break;
				
	case "delete":
		$h->Delete($id, $folder);
		$h->viewhouses();
		break;
			
	case "Update":	
		$h->Update($id, $HTTP_POST_VARS);
		$h->addpictures($house, $HTTP_POST_FILES);
		$h->doform($id);
		break;
			
	case "view":
		$h->viewhouses();
		break;
	
	case "delpic":
		
		$h->deletepicture($file);
		$h->doform($id);
		break;
	
	case "New":
		$h->doform(null);
		break;
	
	case "edit":
		$h->doform($id);
		break;
				
	case "detail":
		$h->viewdetail();
		break;
&#125;

$h->Destroy();
//include("../members/footer.php");
?>

Thanks for whatever help you can offer.

Dave
davediamondprovo
Forum Newbie
Posts: 2
Joined: Wed Nov 26, 2003 5:32 pm

Can you help with this?

Post by davediamondprovo »

BUMP
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post by microthick »

The code is really long which makes people discouraged, I think.

Could you please cut out everything but where you believe the problem is occuring?
Post Reply