Jump to content

Recommended Posts

Hello Everyone,

 

The noob here again with another question.

 

I'm trying to insert text and an image into my database... but I'm having serious problems. Below are my process.php and my form. I can upload an image with no problems... I can even update tables in my database without any problems either but doing it together is proving to be difficult for me.

 

If anything... pointing me to the right direction in terms of uploading a images(in a directory) and text(database) would be great.

 

Thanks,

 


<?php require_once('includes/conn.php'); ?>
<?php 
include('includes/title.inc.php');  
 session_start();

// is the one accessing this page logged in or not?
if (!isset($_SESSION['db_is_logged_in']) || $_SESSION['db_is_logged_in'] !== true) {
   session_register("db_is_logged_in");
}

// define a constant for the maximum upload size
define ('MAX_FILE_SIZE', 51200);

if (array_key_exists('upload', $_POST)) {
  // define constant for upload folder
  define('UPLOAD_DIR', 'C:\web\htdocs\BTI\addMySQL\Gallery\images');
  // replace any spaces in original filename with underscores
  // at the same time, assign to a simpler variable
  $file = str_replace(' ', '_', $_FILES['image']['name']);
  // convert the maximum size to KB
  $max = number_format(MAX_FILE_SIZE/1024, 1).'KB';
  // create an array of permitted MIME types
  $permitted = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png');
  // begin by assuming the file is unacceptable
  $sizeOK = false;
  $typeOK = false;
  
  // check that file is within the permitted size
  if ($_FILES['image']['size'] > 0 && $_FILES['image']['size'] <= MAX_FILE_SIZE) {
	$sizeOK = true;
	}

  // check that file is of an permitted MIME type
  foreach ($permitted as $type) {
	if ($type == $_FILES['image']['type']) {
	  $typeOK = true;
	  break;
	  }
	}
  
  if ($sizeOK && $typeOK) {
	switch($_FILES['image']['error']) {
	  case 0:

		// check if a file of the same name has been uploaded
		if (!file_exists(UPLOAD_DIR.$username.'/'.$file)) {
		  // move the file to the upload folder and rename it
		  $success = move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR.$username.'/'.$file);
		  }
		else {
		  // get the date and time
		  ini_set('date.timezone', 'America/New_York');
		  $now = date('Y-m-d-His');
		  $success = move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR.$username.'/'.$now.$file);
		  }
		if ($success) {
		  $result = "$file uploaded successfully";
		  }
		else {
		  $result = "Error uploading $file. Please try again.";
		  }
		break;
	  case 3:
		$result = "Error uploading $file. Please try again.";
	  default:
		$result = "System error uploading $file. Contact webmaster.";
	  }
	}
  elseif ($_FILES['image']['error'] == 4) {
	$result = 'No file selected';
	}
  else {
	$result = "$file cannot be uploaded. Maximum size: $max. Acceptable file types: gif, jpg, png.";
	}
  }


?>


<?php

$photoID = $_POST['photoID'];
$photoTitle = $_POST['photoTitle'];
$photoDesc = $_POST['photoDesc'];
$photoLocation = $_FILES['image']['name'];
$photoThumb = $_FILES['image']['name'];
$albumID = $_POST['albumID'];

$photoID = trim($photoID);
$photoTitle = trim($photoTitle);
$photoDesc = trim($photoDesc);
$photoLocation = trim($photoLocation);
$photoThumb = trim($photoThumb);
$albumID = trim($albumID);

//$errmsg = "";

//if (!$photoID && !$photoTitle && !$photoDesc && !$photoLocation && !$photoThumb && !$albumID) 

	//$errmsg .= "<p>Please fill the form out properly</p>";

//if ($errmsg!= "") 

//{
	//echo $errmsg;
	//echo "<a href=\"javascript:history.back();\">$errmsg</a>";
	//exit;
//} 

//else 	

//{

mysql_connect("localhost", "root", "*****") or die(mysql_error());
@mysql_select_db("dbalbum") or die(mysql_error());


$query = "INSERT INTO tblphotos (`photoID`,`photoTitle`,`photoDesc`,`photoLocation`,`photoThumb`,`albumID`) VALUES (
					'".$_POST['photoID']."',
					'".$_POST['photoTitle']."',
					'".$_POST['photoDesc']."',
					'"."images/".$_FILES['image']['name']."',
					'"."images/".$_FILES['image']['name']."',
					'".$_POST['albumID']."')";

//echo "The form data was successfully added to your database.";
//echo "<br><br><a href=\"javascript:history.back();\">Add Another Entry</a>";
mysql_query($query);
mysql_close();
//header('Location: addTest.php');
//}

?>

 


<form action="" method="post" enctype="multipart/form-data">
                    <?php
				// if the form has been submitted, display result
				if (isset($result)) {
				  echo "<p><strong>$result</strong></p>";
				  }
				?>
                    <table>
                        <tr>
                            <td>Upload Image:</td>
                            <td>
						<label for="image">Upload image:</label>
						<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MAX_FILE_SIZE; ?>" />
						<input type="file" name="image" id="image" /> 

						<input type="submit" name="upload" id="upload" value="Upload" />
						</td> 
                            <td>Title:</td>
                            <td><input type="text" name="photoTitle" value=""/></td> 
                        </tr>
                        <tr>
                            <td>Description:</td> 
                            <td><input type="text" name="photoDesc" value=""/></td> 
                        </tr>
                        <tr>
                            <td>Album ID:</td>
                            <td><Select name="albumID">
                                <option selected value="">Album</option>
                                <option>1</option>
                                <option>2</option>
                                <option>3</option>
                                <option>4</option>
                                <option>5</option>
                                </Select></td>
                        </tr>
                        <tr>
                            <td><input type="Submit"/></td> 
                            <td><input type="reset" value="Reset" name="Reset"></td> 
                        </tr>
                    </table>
                </form>

Link to comment
https://forums.phpfreaks.com/topic/97710-can-anyone-help-me/
Share on other sites

Thanks for replying.

 

The problem is that the code above is not working properly. If I separate the forms into twho part then it works other than that imgages and text in the same form do not work in the script above. in essence all I want to do is be able to upload to my database an image and a caption at the same time.

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/97710-can-anyone-help-me/#findComment-500369
Share on other sites

Okay by it doesn't work.."

what do you mean ?

 

the image doesn't upload

the theirs no insert into the database

a blank record is inserted

the inserted data is wrong (if so how)

it doesn't upload or insert!

 

also

change

mysql_query($query);

 

to

mysql_query($query) or die("$query<br>".mysql_error());

for better debugging

Link to comment
https://forums.phpfreaks.com/topic/97710-can-anyone-help-me/#findComment-500547
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.