Jump to content


This topic is now archived and is closed to further replies.


Another Newb Is Drowning - Suggestions?

Recommended Posts

Hi. I am relatively new to PHP. Thus far I have done a few small projects. I am currently working on one for uploading photoghaphs via POST, scaling them to size, and publishing them to a website. This tool is intended for potential clients to be able to update the photos section of their website themselves. If this is the right place, and nobody has any objections, I would like to post my project, as far as I am, and hear some suggestions to clean it up.
I currently have  a bit of a bug when I am adding an image. I have to refresh the page before it shows up. It would make sense that I placed something out of order... but I'm through editing the mysql before I have to recall it at all. I can't quite understand why it doesn't show up. Also when the database is empty it struggles and pops out errors.
I've been trying to keep it all on one page, which doesn't make it look very manageable, but it is. It connects to a mysql database to store information, such as ID (just a call number that will also be applied to the uploaded image), caption (allowing captions to be recalled later), and group (allowing images to be grouped and displayed by event or theme.
$uploadDir = getcwd()."/uploaded/";
//set up general info for re-use
$host = "localhost";
$user = "michaelp_admin";
$database = "michaelp_images";
$password = "••••••";
$link = mysql_connect( $host , $user , $password);
if (!$link) {
die('Could not connect: ' . mysql_error());
mysql_select_db($database) or die( "Unable to select database");
//end select connect

$table = "images";

if($_GET["del"] !== null){
$delSQL = "DELETE FROM $table WHERE id=$del";
$query = "SELECT * FROM $table";
$result1 = mysql_query($query);
$num = mysql_numrows($result1);
print "<h1>Num is the number of rows: $num<br>Result1 is: $result1</h1><br>";
$id = $num;
$file = $_FILES['userfile'];

// put file in its correct place
if($file['size'] > 0){
if(! is_uploaded_file($file['tmp_name'])){
die('File is not authentic.');
$fileName = basename($file['name']);
$uploadFile = "$uploadDir$id.jpg";
if (move_uploaded_file($file['tmp_name'], $uploadFile)){
$insertSQL = "INSERT INTO `images` (`id`, `cap`, `group`) VALUES ('".$id."', '".$caption."' , '".$group."')";
$result = mysql_query($insertSQL);
echo "File: ". $fileName ." uploaded successfully.\n <br><br>";

//Display Data
if(0 < $num) {
while ($i< $num) {
echo "<a href='./index.php?del=$id'>delete</a><br> id: $id<br>caption: $caption <br>group: $group<br>num: $num<br><hr>";

//close that shiz

<!-- The data encoding type, enctype, MUST be specified as below -->
<form enctype="multipart/form-data" action="index.php" method="POST">
    <!-- MAX_FILE_SIZE must precede the file input field -->
    <input type="hidden" name="MAX_FILE_SIZE" value="300000" />
    <!-- Name of input element determines name in $_FILES array -->
    Send this file: <input name="userfile" type="file" />
    <br> Caption: <input name="caption" type="text" maxlength="200" >
    <? echo '<input name="id" type="hidden" value="$id">'; ?>
    <input type="submit" value="Send File" />

Thanks a Bunch.
If I am posting this in the wrong place let me know and I"ll move it.


Share this post

Link to post
Share on other sites
Line 19
[code] $delSQL = "DELETE FROM $table WHERE id=$del";[/code]

Either your running with "register_globals" ON - bad idea- or $del isn't defined.

Make the "id" field an auto_increment value instead of setting it to the number of records in the table.

Having uploaded and stored the file on the server, where do store a reference to that file so it can be retrieved?

Where is "$group" given a value [b]before[/b] inserting it into table?

Share this post

Link to post
Share on other sites
Bholbrook: thank you very much... that helped. I obviously have some other things to work out but that was a huge stumbling block for me. I guess I didn't have a great understanding of the "select" statement.

Barand: register_globals is on. Unfortunately I don't have a choice on that, it's not my server. On the fortunate side; this is only to be used by the client, and will be protected by a .htaccess file. There's no real security risk for this client. As for the reference to the file, I am applying the $id variable to each jpg file that is uploaded (i have yet to limit it to just jpg files, but will do that too.) as the title. I then recall the file for display by constructing the file name of $id.jpg. The $group variable will be defined in a form that accesses a different table in my database, exclusively for groups (which are intended to be sort of like photo albums albums).

Thank you very much for the help. I always appreciate it.

Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.