Help a Newbie?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
causeimblonde
Forum Newbie
Posts: 3
Joined: Mon Nov 29, 2004 8:43 pm

Help a Newbie?

Post by causeimblonde »

Hi everyone!!

i asked for some help on another forum, but no one replied...just looking around to see if ANYONE can help :D

my friend is helping me write a script for my webpage, but he doesn't know how to help me on this one. I have a form (script below) to add stuff to my database, but I want it to also let me upload photos. So, then when I go to the online form, I can just add all my data and the pics right there, and if other people want to help me, they can go in there and add the photos too, without having to use my admin program. So, where it says "thumba", "thumbb" etc, that's where I want to have a place to upload photos. And then when i make my display page, it shows up as thumbnails, but when you click on it, a fuill size image appears. Does that makes sense? Can anyone help me? I've tried to find a script, but i just don't understand how to put it in the script I already made...and since it took me forever to get this script to work, I don't want to mess it up!! You guys are so great if you can help me!!!!

here's my script:

Code: Select all

<?
$host="******";
$user="******";
$password="******";
$dbname="******";
$dbtable="******";

$homepage="******.php";

$dbh=mysql_connect ("******", "******", "******") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("w******");

print "<html>\n";
print "<head>\n";
print "<title>******</title>\n";
print "</head>\n\n";


// PREADD CONTENT
if ($action == "preadd")
&#123;
?> 


</head>
<body>

<p>You are adding a new jersey:</p>
<p>
<form action="<? print $homepage ?>" method="post" enctype="multipart/form-data">
 <table width="100%" border="0" align="center" cellpadding="4" cellspacing="0">
   <tr> 
     <td><p>First:</p></td>
     <td> <input name="first" type="text" size="18"> </td>
   </tr>
   <tr> 
     <td><p>Last:</p></td>
     <td><p> 
         <input name="last" type="text" size="18">
       </p></td>
   </tr>
   <tr> 
     <td><p>Thumb A</p></td>
     <td><p> 
         <input name="thumba" type="text" size="18">
       </p></td>
   </tr>
   <tr> 
     <td><p>Thumb B</p></td>
     <td><p> 
         <input name="thumbb" type="text" size="18">
       </p></td>
   </tr>
   <tr> 
     <td><p>Thumb C</p></td>
     <td><p> 
         <input name="thumbc" type="text" size="18">
       </p></td>
   </tr>
   <tr> 
     <td><p>Thumb D</p></td>
     <td><p> 
         <input name="thumbd" type="text" size="18">
       </p></td>
   </tr>
   <tr> 
     <td><p>Team</p></td>
     <td><p> 
         <input name="team" type="text" size="18">
       </p></td>
   </tr>
 </table>
 <p>
   <input name="submit" type="submit" value="Add Entry">
 </p>
 <p>
   <input type="hidden" name="action" value="add">
 </p>
</form>




<?
&#125;


// ADD CONTENT


if ($action == "add")
&#123;

$last = addslashes($last);

$sql = "insert into $dbtable (first,last,thumba,thumbb,thumbc,thumbd,team) values ('$first','$last','$thumba','$thumbb','$thumbc','$thumbd');";
$result = mysql_query($sql, $dbh) or die( mysql_error() ); 

if ($result)
&#123;

print "You have successfully added an Entry for <b>$last</b><br>\n";
&#125;
else
&#123;
print "Error: Unable to Add Entry<br>";
&#125;

print "<br>Click below to be redirected to the Home Page.</p>";
print "<b><a href='$homepage'>Home Page</a></b>";

&#125;



// LIST ENTRIES (DEFAULT)

if ($action == "list" || $action == "")
&#123;
?>

</head>
<body>
 
<p>Main Page</p>
<table width="60%" border="1">
<p>&#1111;<a href="<? print $homepage ?>?action=preadd">Add New</a>] </p>
<p>  
 <?
$sql = "select id, last, first from $dbtable order by id desc;";
$result = mysql_query($sql, $dbh) or die( mysql_error() ); 

?>
 

 <?
for ($x = 0; $x < mysql_num_rows($result); $x++)

&#123;
 $id = mysql_result($result, $x, "id");
 $last = mysql_result($result, $x, "last");
 $first = mysql_result($result, $x, "first");
 

print "<tr>\n";
print "<td><p align='center'>";
print "$id";
print "</p></td>\n";
print "<td><p align='center'>";
print "$last";
print "</p></td>\n";
print "<td><p align='center'>";
print "$first";
print "</p></td>\n";

&#125;
print "</table>\n";
&#125;

?>



</body>
</html>
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Well first of all the form type needs to be 'file' and not 'text' for the files you wish to upload.

This will then upload the files to a tmp folder from which you will have to move them to where you want. In the processing part of the script do:

Code: Select all

print_r($_FILES);
This should show you details of the files that you have uploaded and give you help on what you need to do next - hint check the file type and move it to another directory.

Also take a look at:

viewtopic.php?t=17214

In particular the make_image() function at line 254 as it will show you how to make a thumbnail image :)
causeimblonde
Forum Newbie
Posts: 3
Joined: Mon Nov 29, 2004 8:43 pm

ugh

Post by causeimblonde »

Ok, so I changed the type to "file", which created a "browse" button and allowed me to upload an image. Now, in my database, it says "/tmp/php62niyF"

I read through the tutorial you posted, but I don't understand where in my script anything belongs. I've read through all these previous posts and everything is a script on its own, not as part of another form. Do I have to make a totally seperate form to upload images, or is there anyway to make them as one?

I'm sorry if I sound stupid, I'm really trying to understand all this. I just seem to learn better from going backwards than forwards. If someone showed me a sample script, I could dissect it and figure everything out, but I have trouble building i tmyself. I really do appreciate any help anyone can provide!!!
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

When you upload a file it is saved to a temp folder on the server, which is the location that you got - /tmp/php62niyF. Because its a temp folder the file will be deleted from it after a period of time, so we therefore have to move it out of this folder to where we want it to be stored - hopfully in a web accessable folder.

To move the file you can either use copy() or move_uploaded_file(). This will then have moved the file to the new location. You will often place this inside of an if statment so that you can check to make sure it worked correctly:

Code: Select all

if(!copy($tmp_location, $final_location)){
   echo "Could not move file.";
}else{
   echo "File saved correctly.";
}
Then save this new location in the database. Tell me if this works and then ill help you with the resizing.
causeimblonde
Forum Newbie
Posts: 3
Joined: Mon Nov 29, 2004 8:43 pm

argh

Post by causeimblonde »

I spent most of the day trying to figure this image upload stuff out. I've read a bazillion tutorials and posts and forums...and this is the best i could do. I found a tutorial, wrote the below script. Its great, its simple, it works...but it's still a seperate form. How do I get it so that this form is part of my other one?

This is the image upload script:

Code: Select all

<?php 
function do_upload() &#123; 
    $allowed_types = array( 
        "image/gif" => "gif", 
        "image/pjpeg" => "jpg", 

    ); 
if(!array_key_exists($_FILES&#1111;'userfile']&#1111;'type'], $allowed_types)) &#123; 
        die("Invalid file type."); 
    &#125; 
     
    $maxfilesize = 51200; 

    if($_FILES&#1111;'userfile']&#1111;'size'] > $maxfilesize) &#123; 
        die("File too large"); 
    &#125; 
    $uploaddir = $_SERVER&#1111;'DOCUMENT_ROOT'] . "/uploads/"; 
    $file = $_FILES&#1111;'userfile']&#1111;'tmp_name']; 
    $filename = $_FILES&#1111;'userfile']&#1111;'name']; 
    if(file_exists($uploaddir . $filename)) &#123; 
        die("A file with that name already exists on this server."); 
    &#125; else &#123; 
        copy($file, $uploaddir.$filename) or die("Could not copy file."); 
    &#125; 

    echo "Upload successful"; 
&#125; 

?> 
<html> 
    <head> 
        <title></title> 
    </head> 
    <body> 
        <form method="post" enctype="multipart/form-data"> 
            <input type="hidden" name="action" value="do_upload"> 
            <input type="file" name="userfile"><br/> 
            <input type="submit" name="submit" value="Upload Image"> 
        </form> 
    </body> 
</html> 
<?php 
if($_POST&#1111;'action'] == "do_upload") &#123; 
    do_upload(); 
&#125; 
?>
And this is my other form. I'd like the image upload to be where the "Thumb A", "Thumb B", etc are currently. What am I missing?!? Is it even possible to combine these functions?

Code: Select all

<?
$host="******";
$user="******";
$password="******";
$dbname="******";
$dbtable="******";

$homepage="******";

$dbh=mysql_connect ("******", "******", "******") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("******");

print "<html>\n";
print "<head>\n";
print "<title>Untitled</title>\n";
print "</head>\n\n";

print "<body>\n\n";

print "<table width='640' cellspacing='5' cellpadding='5'>\n";
print "<tr>\n";
print "<td width='40'></td>\n";
print "<td>\n\n";


// PREADD CONTENT

if ($action == "preadd")
&#123;
?>

You are adding a new item:<p>
		 
<form action="<? print $homepage ?>" method="post" enctype="multipart/form-data">
  <table border="0" cellpadding="3" cellspacing="3">
    <tr> 
      <td>First Name:</td>
      <td>  
        <input type="text" name="first" size="40" maxlength="80">
        </td>
    </tr>
    <tr> 
      <td>Last Name:</td>
      <td>  
	  <input type="text" name="last" size="40" maxlength="80">
        </td>
    </tr>
	    <tr> 
      <td>Team:</td>
      <td>  
	  <input type="text" name="team" size="40" maxlength="80">
        </td>
    </tr>
    <tr> 
      <td>Number:</td>
      <td>  
	  <input type="text" name="number" size="40" maxlength="80">
        </td>
    </tr>
    <tr> 
      <td>League:</td>
      <td>  
	  <input type="text" name="league" size="40" maxlength="80">
        </td>
    </tr>
	   <tr> 
      <td>Year:</td>
      <td>  
	  <input type="text" name="year" size="40" maxlength="80">
        </td>
    </tr>
	   <tr> 
      <td>Home</td>
      <td>  
	  <input type="text" name="home" size="40" maxlength="80">
        </td>
    </tr>
	   <tr> 
      <td>Brand:</td>
      <td>  
	  <input type="text" name="brand" size="40" maxlength="80">
        </td>
    </tr>
	   <tr> 
      <td>LOA:</td>
      <td>  
	  <input type="text" name="loa" size="40" maxlength="80">
        </td>
    </tr>
	   <tr> 
      <td>Size:</td>
      <td>  
	  <input type="text" name="size" size="40" maxlength="80">
        </td>
    </tr>
	   <tr> 
      <td>Description:</td>
      <td>  
	  <input type="text" name="description" size="40" maxlength="80">
        </td>
    </tr>
	   <tr> 
      <td>Drafted:</td>
      <td>  
	  <input type="text" name="draft" size="40" maxlength="80">
        </td>
    </tr>
	   <tr> 
      <td>Current:</td>
      <td>  
	  <input type="text" name="current" size="40" maxlength="80">
        </td>
    </tr>
	   <tr> 
      <td>Thumb A:</td>
      <td>  
	  <input type="file" name="thumba" size="40" maxlength="80">
        </td>
    </tr>
	   <tr> 
      <td>Thumb B:</td>
      <td>  
	  <input type="file" name="thumbb" size="40" maxlength="80">
        </td>
    </tr>
	   <tr> 
      <td>Thumb C:</td>
      <td>  
	  <input type="file" name="thumbc" size="40" maxlength="80">
        </td>
    </tr>
	   <tr> 
      <td>Thumb D:</td>
      <td>  
	  <input type="file" name="thumbd" size="40" maxlength="80">
        </td>
    </tr>
    <tr> 
      <td></td>
      <td> 
        <input type="submit" value="Add item">
        </td>
    </tr>
  </table>
   
  <input type="hidden" name="action" value="add">
   
</form>
 
<?
&#125;


// ADD CONTENT

if ($action == "add")
&#123;
	$body = addslashes($body);

	$sql = "insert into $dbtable (first,last,thumba,thumbb,thumbc,thumbd,team,number,league,year,home,brand,loa,size,description,draft,current) values ('$first','$last','$thumba','$thumbb','$thumbc','$thumbd','$team','$number','$league','$year','$home','$brand','$loa','$size','$description','$draft','$current'
);";

	$result = mysql_query($sql, $dbh) or die( mysql_error() );

	if ($result)
	&#123;
	print "You have added a item for <b>$first $last</b>.<br>\n";
	&#125;
	else
	&#123;
	print "Error: Could Not Add item.<br>";
	&#125;
	
	print "<br>If no errors are encountered, please check the appropriate area of the website to make sure it appears correctly.<p>";
	print "<b><a href='$homepage'>Return to  Admin</a></b>";
	
&#125;


// EDIT CONTENT

if ($action == "edit")
&#123;
	$sql = "select id,first,last,thumba,thumbb,thumbc,thumbd,team,number,league,year,home,brand,loa,size,description,draft,current from $dbtable where id=$id;";
	$result = mysql_query($sql, $dbh) or die( mysql_error() );

$first = mysql_result($result, 0, "first");
$last = mysql_result($result, 0, "last");
$thumba = mysql_result($result, 0, "thumba");
$thumbb = mysql_result($result, 0, "thumbb");
$thumbc = mysql_result($result, 0, "thumbc");
$thumbd = mysql_result($result, 0, "thumbd");
$team = mysql_result($result, 0, "team");
$number = mysql_result($result, 0, "number");
$league = mysql_result($result, 0, "league");
$year = mysql_result($result, 0, "year");
$home = mysql_result($result, 0, "home");
$brand = mysql_result($result, 0, "brand");
$loa = mysql_result($result, 0, "loa");
$size = mysql_result($result, 0, "size");
$description = mysql_result($result, 0, "description");
$draft = mysql_result($result, 0, "draft");
$current = mysql_result($result, 0, "current");

?>
You are editing a item:  
<p> 
<form action="<? print $homepage ?>" method="post" enctype="multipart/form-data">
  <table border="0" cellpadding="3" cellspacing="3">
    <tr> 
      <td>First:</td>
      <td>  
        <input type="text" name="first" value="<? print $first ?>" size="40" maxlength="80">
        </td>
    </tr>
    <tr> 
      <td>Last:</td>
      <td>  
	  <input type="text" name="last" value="<? print $last ?>" size="40" maxlength="80">
        </td>
    </tr>
	<tr> 
      <td>Team:</td>
      <td>  
	  <input type="text" name="team" value="<? print $team ?>" size="40" maxlength="80">
        </td>
    </tr>
    <tr> 
      <td>Number:</td>
      <td>  
	  <input type="text" name="number" value="<? print $number ?>" size="40" maxlength="80">
        </td>
    </tr>
    <tr> 
      <td>League:</td>
      <td>
	  <input type="text" name="league" value="<? print $league ?>" size="40" maxlength="80">
        </td>
    </tr>
	 <tr> 
      <td>Year:</td>
      <td>
	  <input type="text" name="year" value="<? print $year ?>" size="40" maxlength="80">
        </td>
    </tr>
	 <tr> 
      <td>Home/Road/Alt:</td>
      <td>
	  <input type="text" name="home" value="<? print $home ?>" size="40" maxlength="80">
        </td>
    </tr>
	 <tr> 
      <td>Brand:</td>
      <td>
	  <input type="text" name="brand" value="<? print $brand ?>" size="40" maxlength="80">
        </td>
    </tr>
	 <tr> 
      <td>Team LOA:</td>
      <td>
	  <input type="text" name="loa" value="<? print $loa ?>" size="40" maxlength="80">
        </td>
    </tr>
	 <tr> 
      <td>Size:</td>
      <td>
	  <input type="text" name="size" value="<? print $size ?>" size="40" maxlength="80">
        </td>
    </tr>
	 <tr> 
      <td>Description:</td>
      <td>
	  <input type="text" name="description" value="<? print $description ?>" size="40" maxlength="80">
        </td>
    </tr>
	 <tr> 
      <td>Drafted:</td>
      <td>
	  <input type="text" name="draft" value="<? print $draft ?>" size="40" maxlength="80">
        </td>
    </tr>
	 <tr> 
      <td>Current League:</td>
      <td>
	  <input type="text" name="current" value="<? print $current ?>" size="40" maxlength="80">
        </td>
    </tr> <tr> 
      <td>Thumb A:</td>
      <td>
	  <input type="file" name="thumba" value="<? print $thumba ?>" size="40" maxlength="80">
        </td>
    </tr>
	 <tr> 
      <td>Thumb B:</td>
      <td>
	  <input type="file" name="thumbb" value="<? print $thumbb ?>" size="40" maxlength="80">
        </td>
    </tr>
	 <tr> 
      <td>Thumb C:</td>
      <td>
	  <input type="file" name="thumbc" value="<? print $thumbc ?>" size="40" maxlength="80">
        </td>
    </tr>
	 <tr> 
      <td>Thumb D:</td>
      <td>
	  <input type="file" name="thumbd" value="<? print $thumbd ?>" size="40" maxlength="80">
        </td>
    </tr>
	
    <tr> 
      <td></td>
      <td> 
        <input type="submit" value="Edit item">
        </td>
    </tr>
  </table>
   
  <input type="hidden" name="id" value="<? print $id ?>">
  <input type="hidden" name="action" value="update">
   
</form>
 
<?
&#125;


// UPDATE CONTENT

if ($action == "update")
&#123;
	$body = addslashes($body);

	$sql = "update $dbtable set first='$first',last='$last',team='$team',number='$number',league='$league' ,year='$year',home='$home',brand='$brand',loa='$loa',size='$size',description='$description',draft='$draft',current='$current',thumba='$thumba',thumbb='$thumbb',thumbc='$thumbc',thumbd='$thumbd' where id='$id'";
	$result = mysql_query($sql, $dbh) or die( mysql_error() );

	if ($result)
	&#123;
	print "You have edited a item entitled <b>$first $last</b>.<br>\n";
	&#125;
	else
	&#123;
	print "Error: Could Not Edit item<br>";
	&#125;
	
	print "<br>If no errors are encountered, please check the appropriate area of the website to make sure it appears correctly.<p>";
	print "<b><a href='$homepage'>Return to  Admin</a></b>";
&#125;


// DELETE CONTENT

if ($action == "delete_check")
&#123;
	print "Are you sure you want to PERMANENTLY DELETE the item entitled <b>$first $last</b>?<p>\n\n";
	
	print "<b><a href='$homepage?action=delete&id=$id'>Yes</a></b><br><br>";
	print "<b><a href='$homepage'>No - Return to  Admin</a></b>";
&#125;

if ($action == "delete")
&#123;
	$sql = "delete from $dbtable where id='$id';";
	$result = mysql_query($sql, $dbh) or die( mysql_error() );

	if ($result)
	&#123;
	print "You have permanently deleted the item entitled <b>$first $last</b>.<p>";
	&#125;
	else
	&#123;
	print "Error: Could Not Delete item.<p>";
	&#125;
	
	print "<b><a href='$homepage'>Return to  Admin</a></b>";
&#125;


// LIST ENTRIES (DEFAULT)

if ($action == "list" || $action == "")
&#123;
?>
&#1111;<a href="<? print $homepage ?>?action=preadd">Add New item</a>] 
<p>  
  <?
	$sql = "select id, first, last from $dbtable order by id desc;";
	$result = mysql_query($sql, $dbh) or die( mysql_error() );

?>
  
<table border="1" cellspacing="0" cellpadding="5">
  <tr> 
    <td>ID</td>
    <td>First</td>
	<td>Last</td>
    <td>Functions</td>
  </tr>
  <?
	for ($x = 0; $x < mysql_num_rows($result); $x++)
	&#123;
		$id = mysql_result($result, $x, "id");
		$first = mysql_result($result, $x, "first");
		$last = mysql_result($result, $x, "last");
	
		print "	<tr>\n";
		print " <td bgcolor='#aaaaaa' align='center'>";
		print "$id";
		print "</td>\n";

		print "<td bgcolor='#bbbbbb'>";
		print "$first";
		print "</td>\n";

		print "<td bgcolor='#bbbbbb'>";
		print "$last";
		print "</td>\n";
		
		print "<td>";
		print "&#1111;<a href='$homepage?action=edit&id=$id&first=$first&last=$last'>Edit</a>] &#1111;<a href='$homepage?action=delete_check&id=$id&first=$first&last=$last'>Delete</a>]";
		print "</td>\n";
		print "	</tr>\n\n";
	&#125;
		print "</table>\n";
&#125;

?>
  </td> </tr> 
</table>

</body>
</html>
Post Reply