Jump to content

Recommended Posts

I have a form that allows for a image to be uploaded. I am not sure how to do this so I was hoping someone could point me to someplace I could read how to do this or explain to me how this can be done.

 

Really what I am wanted is for the image to be uploaded to a file then renamed and the URL to that image in the file be entered in the DB. I already have the database built and the form and page built. So All I am really needing is to put the function together. Can anyone help me with this please?

 

-Thanks

Link to comment
https://forums.phpfreaks.com/topic/86842-insert-images-into-mysql/
Share on other sites

Thank you that is a start but I already have a table with all the information. That explains how to do it with a new table but I don't need a new table. I need to add this to a table I already have so that the information is kept in the same row with the others users information.

 

http://mesquitechristmas.com/local/submit.php

 

Can I just add the image information to that table?

 

Is the tutorial says I need an image_id but I have an ID already for the table and row I am using now. Here is the columns in the table I use now. What all do I need to add to this so I can use the same table?

 

CREATE TABLE `users` (

  `id` int(4) NOT NULL auto_increment,

  `email` varchar(65) NOT NULL default '',

  `password` varchar(65) NOT NULL default '',

  `displayname` varchar(40) NOT NULL,

  `displaytype` varchar(40) NOT NULL,

  `description` longtext NOT NULL,

  `address` varchar(50) NOT NULL,

  `address2` varchar(50) NOT NULL,

  `city` varchar(10) NOT NULL,

  `state` varchar(5) NOT NULL,

  `postal` int(5) NOT NULL,

  `country` varchar(15) NOT NULL,

  `website` varchar(40) NOT NULL,

  `imagefile` blob NOT NULL,

  PRIMARY KEY  (`id`)

) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=42 ;

 

 

-Thanks

Like I already stated and showed in my last post. I already have an ID field that is already auto_increment. MySQL doesn't allow for two fields in the same table to be auto_increment. That is why I ask how to just be able to add an image to the one I have now. As you notice I already have my image set to blob. Can I not just use the ID that is already being used for the table?

 

So what do you mean by update the BLOB column with the $_FILES that was uploaded and assign it an ID?

What do you mean "use the following in the insert"?

 

This is the PHP for the form....

 

<?php

// here, we check if the form has been submitted, because we need to handle
// redirection before we handle outputting the HTML stuff.

if (isset($_POST['submitted']))
{
    if (empty($_POST['displayname']) || empty($_POST['displaytype']) || empty($_POST['description']) || empty($_POST['address']) || empty($_POST['city']) || empty($_POST['state']) || empty($_POST['postal']) || empty($_POST['country']))
    {
        $error = '*Please fill in all fields.';  // here, they have not filled in all fields.  Set an error.
    }
    else
    {

// MAKE CONNECTION
include ('db_connect.php'); 

$displayname = $_POST['displayname'];
$displaytype = $_POST['displaytype'];
$description = $_POST['description'];
$address = $_POST['address'];
$address2 = $_POST['address2'];
$city = $_POST['city'];
$state = $_POST['state'];
$postal = $_POST['postal'];
$country = $_POST['country'];
$website = $_POST['website'];

mysql_query("INSERT INTO users (displayname, displaytype, description, address, address2, city, state, postal, country, website, imagefile) 
VALUES ('$displayname', '$displaytype', '$description', '$address', '$address2', '$city', '$state', '$postal', '$country', '$website')")or die (mysql_error()); 

    }

}

?>

Hi

 

$imgdata = file_get_contents($_FILES['replacewithfieldname']['tmp_name']);

$query = "INSERT INTO users (displayname, displaytype, description, address, address2, city, state, postal, country, website, imagefile) VALUES ('$displayname', '$displaytype', '$description', '$address', '$address2', '$city', '$state', '$postal', '$country', '$website', '$imgdata')";

mysql_query($query) or die (mysql_error()); 

 

Sujith

Here is the entire code for this page.

 

http://mesquitechristmas.com/local/submit.php

 

Is this right? am I missing anything? They database setup is above.

 

<?php

// here, we check if the form has been submitted, because we need to handle
// redirection before we handle outputting the HTML stuff.

if (isset($_POST['submitted']))
{
    if (empty($_POST['displayname']) || empty($_POST['displaytype']) || empty($_POST['description']) || empty($_POST['address']) || empty($_POST['city']) || empty($_POST['state']) || empty($_POST['postal']) || empty($_POST['country']) || empty($_POST['imgdata']))
    {
        $error = '*Please fill in all fields.';  // here, they have not filled in all fields.  Set an error.
    }
    else
    {

// MAKE CONNECTION
include ('db_connect.php'); 

$displayname = $_POST['displayname'];
$displaytype = $_POST['displaytype'];
$description = $_POST['description'];
$address = $_POST['address'];
$address2 = $_POST['address2'];
$city = $_POST['city'];
$state = $_POST['state'];
$postal = $_POST['postal'];
$country = $_POST['country'];
$website = $_POST['website'];
$website = $_POST['imgdata'];

$imgdata = file_get_contents($_FILES['replacewithfieldname']['tmp_name']);

mysql_query("INSERT INTO users (displayname, displaytype, description, address, address2, city, state, postal, country, website, imgdata) 
VALUES ('$displayname', '$displaytype', '$description', '$address', '$address2', '$city', '$state', '$postal', '$country', '$website', '$imgdata')")or die (mysql_error()); 

    }

}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="description" content="Mesquite Texas Country Christmas" />
<meta name="keywords" content="Mesquite, Texas, Country Christmas" />
<meta name="author" content="NA" />
<link rel="stylesheet" type="text/css" href="/stylesheet.css" media="screen" title="FBC" />
<script type="text/javascript" src="drop_down.js"></script>
<title>A Mesquite Country Christmas</title>
</head>
<body>

<div id="wrap">

<a href="/index.html">
<img id="frontphoto" src="/images/header.png" width="760" height="237" alt="Mesquite Country Christmas" border="0"></a>

<div id="menu">

<h2 class="hide">Menu:</h2>

<ul id="avmenu">
<li><a href="/index.html">Home</a></li>
<li><a href="/christmasstory.html">The Christmas Story</a></li>
<li><a href="/directions.html">Directions</a></li>
<li><a href="#">Information</a><ul>
      <li><a href="/information.html">Display Facts & Info</a></li>
      <li><a href="/faq.html">FAQ</a></li>
      <li><a href="/playlist.html">2008 Playlist</a></li>
      <li><a href="#">Christmas History</a></li>
  </ul></li>
<li><a href="#">Photos</a>
  <ul>
      <li><a href="/2007photos.html">2007</a></li>
  </ul></li>
<li><a href="#">Videos</a>
  <ul>
      <li><a href="/2007videos.html">2007</a></li>
  </ul></li>
<li><a href="/guestbook.php">Guestbook</a></li>
<li><a href="/webcam.html">Web Cam</a></li>
<li><a href="/webradio.html">Internet Radio</a></li>
<li><a href="http://www.noradsanta.org/" TARGET="_blank">Track Santa</a></li>
<li><a href="/projects.html">Projects & How Tos</a></li>
<li><a href="/links.html">Links</a></li>
<li><a href="/contact_us.html">Contact Us</a></li>
</ul>

<center><a href="http://www.toysfortots.org/" TARGET="_blank"><img src="/images/toys_for_tots.jpg" border="0" width="110" height="153" vspace="10"></a></center>

<center><a href="http://christmas.bronners.com/2007/house/534.html"><img src="http://christmas.bronners.com/voteforme/vote.jpg" border="0" width="110" height="153" alt="christmas decorations" vspace="10"></a></center>

</div>

<div id="content">

<div class="fadebox">

<h2> Submit your Lights</h2>

<hr />

<p><img src="/images/christmas-lights.jpg" width="153" height="208" alt="Submit Lights" align="left" border="0" hspace="10"> Thank you for submitting your home to the Mesquites Country Christmas display finder database.  There are some things you should know before submitting to make your experience a pleasant one.</p><p>First of all, we do not release your personal information to anyone for any reason.  The only exception to this, obviously, is the address of the display, the website (if it has one), and a picture of the display.</p><p>Secondly, please have a picture available to use for your display.  You won't be able to submit your display without one.  When submitting a picture, please make sure that the file is less than one megabyte (1 Meg or 1024k), and if possible, resize it to 640x480. Please also be sure your picture is in JPG format.</p><p>Your display will not immediately be entered into the Christmas Light Finder database.  We will first have to approve your submission.  This is so that those unscrupulous folks out there can't submit unacceptable pictures (not family friendly).  Please allow up to 48 hours to be included into the database, though, the time before insertion is usually much shorter than that.</p>

</div>

<div class="fadebox">

<h2> Ready to submit? Fill out the form below:</h2>

<hr />

<br />

<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table>
<tr>
  <td>Display Name*</td><td><input name="displayname" size="40" type="text"></td></tr>

<tr> 
  <td>Display Type*</td><td><select name="displaytype"><option value="Residential">Residential</option><option value="Neighborhood">Neighborhood</option><option value="Commercial">Commercial</option><option value="City/Government">City/Government</option><option value="Sponsored">Sponsored</option></select></td></tr>

<tr><td>Description*</td><td><textarea name="description" cols="30" rows="5"></textarea></td></tr>

<tr><td>Address*</td><td><input name="address" size="40" type="text"></td></tr><tr><td>Address 2</td><td><input name="address2" size="40" type="text"></td></tr>

<tr><td>City*</td><td><input name="city" size="30" type="text" value="Mesquite"></td></tr>

<tr><td>State/Province*</td><td><input name="state" size="30" type="text" value="Texas"></td></tr>

<tr><td>Postal Code*</td><td><select name="postal"><option value="75149">75149</option><option value="75150">75150</option><option value="75180">75180</option><option value="75181">75181</option><option value="75185">75185</option><option value="75187">75187</option></select></td></tr>

<tr><td>Country*</td><td><input name="country" size="30" type="text" value="United States"></td></tr>

<tr><td>Website</td><td><input name="website" size="50" value="http://" type="text"></td></tr>

<tr><td>Picture*</td><td><input type="file" name="imgdata" size="35"></td></tr>

<tr>
<td colspan="2" align="right" class="errorText">
<?PHP
// then we check for the error message
if (isset($error)) {
   echo $error . '<br />';
}
?> 
</td>
</tr>

<tr><td  colspan="2" style="border-top: 1px solid black;" align="right">

<br />

<input name="submitted" value="Submit Now" type="submit"></td></tr>

</tbody></table></form>

<br>* Fields are required.	<br>



   </div>
</div>

<div id="footer">
© 2007 Mesquite Country Christmas

<br />
<br />

<script type="text/javascript"><!--
google_ad_client = "pub-8048181801684156";
//468x60, created 1/8/08
google_ad_slot = "0360766123";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>


</div>

</div>
</body>
</html>

I don't think this is working how I like. I have manually entered a image into the DB for testing. How would I display the image from the database? I think having the image stored in a file then having the URL stored in the DB would be easier and preferred. How would this be done?

 

-Thanks

this is just my opinion, but I store images and other files in the file system and link to them using the db.

 

With my host, I only have 100MB db's, so storing images and such inside it will take the space up real quick. Plus I *think* that it would be quicker to store in a filesystem and let the db just grab text and such. I may be wrong, but I think storing in a filesystem would be better.

 

 

 

Hope it helps,

 

 

Nate

this is just my opinion, but I store images and other files in the file system and link to them using the db.

 

With my host, I only have 100MB db's, so storing images and such inside it will take the space up real quick. Plus I *think* that it would be quicker to store in a filesystem and let the db just grab text and such. I may be wrong, but I think storing in a filesystem would be better.

 

 

 

Hope it helps,

 

 

Nate

 

 

this is what i am wanting to do but cant find anything information about this to do this...

 

 

here is the basics of an upload script... there may be a few tweaks you need to make such as set the file path below, and replace my connect_db(); function with your own db connection method. You will also need to adjust the query to insert the info into your db.

 

I insert into the db, the full path of the file. So that way when I want to use it I run my query to grab the path and use it like so....

 

<img src="<?=$path_to_file ?>" >

 

<form action="<?=$_SERVER['PHP_SELF'] ?>" method="post" >
<input type="file" name="image" id="image" />
<input type="submit" name="submit" value="Upload File">

<?php
$file=$_FILES['image']['tmp_name']; // its an image, get its temp name
$type = $_FILES['image']['type'];

if(is_uploaded_file($file))  // make sure the file is uploaded before we proceed
{
if($type == 'image/gif' || $type == 'image/jpeg') // check and see if the file type is an image
{
	$image_name=$_FILES['image']['name']; // get image name
	$uploaddir='/path/to/your/images/'; // set the path to the image dir 
	$uploaddir. = $image_name; // set upload path including the filename	
}
else
{ // set error message because file was not an image
	$error='The file you have attempted to upload is not an image. For security reasons, only images are allowed';
}

if(move_uploaded_file($_FILES['image']['tmp_name'], $uploaddir)) 
{ // if file was moved into the directory 
	$message.= 'Successfully uploaded file '.$image_name ;
}
else 
{ // set error message saying that file was not uploaded
	$error='There was a problem uploading the file';
}
	connectdb(); // connnect to db
	$query="INSERT INTO TABLE_NAME (image_path) VALUES ('$image_name')"; // setup our query
	$result=mysql_query($query) or die(mysql_error()); // run our query
	// set our message to say it was successful
	$message='Successfully added info to database';  

}
}
?>

 

Hope this helps & works for you with no problem :)

 

 

Nate

Thank you for your help, what would I change my database type to for the field that will be storing the URL?

 

Also can this script be combine with the rest of the insert script like this:

 

<?php

// here, we check if the form has been submitted, because we need to handle
// redirection before we handle outputting the HTML stuff.

if (isset($_POST['submitted']))
{
    if (empty($_POST['displayname']) || empty($_POST['displaytype']) || empty($_POST['description']) || empty($_POST['address']) || empty($_POST['city']) || empty($_POST['state']) || empty($_POST['postal']) || empty($_POST['country']) || empty($_POST['imgdata']))
    {
        $error = '*Please fill in all fields.';  // here, they have not filled in all fields.  Set an error.
    }
    else
    {

// MAKE CONNECTION
include ('db_connect.php'); 

$displayname = $_POST['displayname'];
$displaytype = $_POST['displaytype'];
$description = $_POST['description'];
$address = $_POST['address'];
$address2 = $_POST['address2'];
$city = $_POST['city'];
$state = $_POST['state'];
$postal = $_POST['postal'];
$country = $_POST['country'];
$website = $_POST['website'];

mysql_query("INSERT INTO users (displayname, displaytype, description, address, address2, city, state, postal, country, website, imgdata) 
VALUES ('$displayname', '$displaytype', '$description', '$address', '$address2', '$city', '$state', '$postal', '$country', '$website')")or die (mysql_error()); 

$file=$_FILES['image']['tmp_name']; // its an image, get its temp name
$type = $_FILES['image']['type'];

if(is_uploaded_file($file))  // make sure the file is uploaded before we proceed
{
if($type == 'image/gif' || $type == 'image/jpeg') // check and see if the file type is an image
{
	$image_name=$_FILES['image']['name']; // get image name
	$uploaddir='/local/submitted/'; // set the path to the image dir 
	$uploaddir. = $image_name; // set upload path including the filename	
}
else
{ // set error message because file was not an image
	$error='The file you have attempted to upload is not an image. For security reasons, only images are allowed';
}

if(move_uploaded_file($_FILES['image']['tmp_name'], $uploaddir)) 
{ // if file was moved into the directory 
	$message.= 'Successfully uploaded file '.$image_name ;
}
else 
{ // set error message saying that file was not uploaded
	$error='There was a problem uploading the file';
}
	connectdb(); // connnect to db
	$query="INSERT INTO TABLE_NAME (image_path) VALUES ('$image_name')"; // setup our query
	$result=mysql_query($query) or die(mysql_error()); // run our query
	// set our message to say it was successful
	$message='Successfully added info to database';  

}
}

header('Location: user.php');

    }

}

?>

 

-Thanks

This is what I have done but I get this error

 

Parse error: syntax error, unexpected '=' in /home/mesquit1/public_html/local/submit.php on line 41

 

<?php

// here, we check if the form has been submitted, because we need to handle
// redirection before we handle outputting the HTML stuff.

if (isset($_POST['submitted']))
{
    if (empty($_POST['displayname']) || empty($_POST['displaytype']) || empty($_POST['description']) || empty($_POST['address']) || empty($_POST['city']) || empty($_POST['state']) || empty($_POST['postal']) || empty($_POST['country']) || empty($_POST['imgdata']))
    {
        $error = '*Please fill in all fields.';  // here, they have not filled in all fields.  Set an error.
    }
    else
    {

// MAKE CONNECTION
include ('db_connect.php'); 

$displayname = $_POST['displayname'];
$displaytype = $_POST['displaytype'];
$description = $_POST['description'];
$address = $_POST['address'];
$address2 = $_POST['address2'];
$city = $_POST['city'];
$state = $_POST['state'];
$postal = $_POST['postal'];
$country = $_POST['country'];
$website = $_POST['website'];

mysql_query("INSERT INTO users (displayname, displaytype, description, address, address2, city, state, postal, country, website, imgdata) 
VALUES ('$displayname', '$displaytype', '$description', '$address', '$address2', '$city', '$state', '$postal', '$country', '$website')")or die (mysql_error()); 

$file=$_FILES['image']['tmp_name']; // its an image, get its temp name
$type = $_FILES['image']['type'];

if(is_uploaded_file($file))  // make sure the file is uploaded before we proceed
{
if($type == 'image/gif' || $type == 'image/jpeg') // check and see if the file type is an image
{
	$image_name=$_FILES['image']['name']; // get image name
	$uploaddir='/local/submitted/'; // set the path to the image dir 
	$uploaddir. = $image_name; // set upload path including the filename	
}
else
{ // set error message because file was not an image
	$error='The file you have attempted to upload is not an image. For security reasons, only images are allowed';
}

if(move_uploaded_file($_FILES['image']['tmp_name'], $uploaddir)) 
{ // if file was moved into the directory 
	$message.= 'Successfully uploaded file '.$image_name ;
}
else 
{ // set error message saying that file was not uploaded
	$error='There was a problem uploading the file';
}
	$query="INSERT INTO users (image_path) VALUES ('$image_name')"; // setup our query
	$result=mysql_query($query) or die(mysql_error()); // run our query
	// set our message to say it was successful
	$message='Successfully added info to database';  

}
}

header('Location: user.php');

    }

}

?>

You would want to place the query inside my code so that you only insert the data once you have confirmed that the file actually uploaded. Otherwise you may end up with a db entry for a non-existent image.

 

is this line 41 in your script??

 

$uploaddir. = $image_name; // set upload path including the filename	

 

if so, then try changing it to

$uploaddir.=$image_name; // set upload path including the filename	

 

Lemme know what that brings.

 

Nate

 

Ok I took your advice and made the the changes you suggested and I combined the query to the end so their is only one.I am not getting any errors now but I have not tested the form. Do you know what I need to change the type to in the database for the field that will hold the URL?

 

Also is their a way to rename the image?

 

Here is the update code.

 

<?php

// here, we check if the form has been submitted, because we need to handle
// redirection before we handle outputting the HTML stuff.

if (isset($_POST['submitted']))
{
    if (empty($_POST['displayname']) || empty($_POST['displaytype']) || empty($_POST['description']) || empty($_POST['address']) || empty($_POST['city']) || empty($_POST['state']) || empty($_POST['postal']) || empty($_POST['country']) || empty($_POST['image']))
    {
        $error = '*Please fill in all fields.';  // here, they have not filled in all fields.  Set an error.
    }
    else
    {

// MAKE CONNECTION
include ('db_connect.php'); 

$displayname = $_POST['displayname'];
$displaytype = $_POST['displaytype'];
$description = $_POST['description'];
$address = $_POST['address'];
$address2 = $_POST['address2'];
$city = $_POST['city'];
$state = $_POST['state'];
$postal = $_POST['postal'];
$country = $_POST['country'];
$website = $_POST['website'];

$file=$_FILES['image']['tmp_name']; // its an image, get its temp name
$type = $_FILES['image']['type'];

if(is_uploaded_file($file))  // make sure the file is uploaded before we proceed
{
if($type == 'image/gif' || $type == 'image/jpeg') // check and see if the file type is an image
{
	$image_name=$_FILES['image']['name']; // get image name
	$uploaddir='/local/submitted/'; // set the path to the image dir 
	$uploaddir.=$image_name; // set upload path including the filename		
}
else
{ // set error message because file was not an image
	$error='The file you have attempted to upload is not an image. For security reasons, only images are allowed';
}

if(move_uploaded_file($_FILES['image']['tmp_name'], $uploaddir)) 
{ // if file was moved into the directory 
	$message.= 'Successfully uploaded file '.$image_name ;
}
else 
{ // set error message saying that file was not uploaded
	$error='There was a problem uploading the file';
}
	$query="INSERT INTO users (displayname, displaytype, description, address, address2, city, state, postal, country, website, image_path) VALUES ('$displayname', '$displaytype', '$description', '$address', '$address2', '$city', '$state', '$postal', '$country', '$website', '$image_name')"; // setup our query
	$result=mysql_query($query) or die(mysql_error()); // run our query
	// set our message to say it was successful
	$message='Successfully added info to database';  

}
}

header('Location: user.php');

    }

?>

make the field type a varchar(255)

 

$image_name=$_FILES['image']['name']; // get image name

 

I kept the same file name, but if you wanna change it from what was uploaded.... change the $_FILES...... part to whatever algorithm you want. you may want to make the name part of the users name or however you want to do it.

 

 

Nate

I would like to make the image names unique when they are stored in the DB like the first one would be something like 001.jpg then the next would be 002.jpg and so on.

 

Now is this possible without making it auto-increment in the DB because my id field is already auto-increment and you can only have one auto-increment per table?

 

Also I just tested the form, I get no errors but it doesn't insert the info.

I don't think my query is right, something looks wrong. Also for testing purposes I manually entered a URL into the database. When I do get this working will it enter it as a relative path or absolute path?

 

Here is the query

 

$query="INSERT INTO users WHERE (displayname, displaytype, description, address, address2, city, state, postal, country, website, image_path) VALUES ('$displayname', '$displaytype', '$description', '$address', '$address2', '$city', '$state', '$postal', '$country', '$website', '$image_name')"; // setup our query
	$result=mysql_query($query) or die(mysql_error()); // run our query
	// set our message to say it was successful
	$message='Successfully added info to database';  

 

Like I said I do not get any errors but nothing is entered.

Ok I have changed my query to this but it still isn't inserting and I am getting no errors.

 

mysql_query("INSERT INTO users (displayname, displaytype, description, address, address2, city, state, postal, country, website, image_path) VALUES ('$displayname', '$displaytype', '$description', '$address', '$address2', '$city', '$state', '$postal', '$country', '$website', '$image_name')"); // setup our query
	$result=mysql_query($query) or die(mysql_error()); // run our query

	// set our message to say it was successful
	$message='Successfully added info to database';  

 

Now this submit form is inserting information that also holds the registration information id, email, password etc. So would I need to add a WHERE statement to the query. Once this is finished this submit form page will be secured by sessions so the only way you will be able to use this form is if you are registered and logged in.

 

I am out of ideas, anyone got an idea why this isn't working? I would like to get the form working because I lock it down. It was working before I started adding the image stuff.

 

-Thanks

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.