html form to php mysql db insert for image

Thread Solved

Join Date: Oct 2007
Posts: 1
Reputation: dwatycha is an unknown quantity at this point 
Solved Threads: 0
dwatycha dwatycha is offline Offline
Newbie Poster

html form to php mysql db insert for image

 
0
  #1
Apr 24th, 2009
relative php noob needs help, have tried 3 or 4 differnet ways to get an image along with other post data from an html form to save into a mysql db. other data saves fine, no luck at all with image. php and form code below. HELP. Thanks in advance.

<?php

include 'config.php';
include 'opendb.php';

$pass = $_POST['blogpass'];
$title = $_POST['title'];
$post = $_POST['post'];
$category = $_POST['category'];
$blogimage = $_POST['blogimage'];

if(isset($_POST['add']))
{

if ($pass == "password")
{
$query = "INSERT INTO tablename (title, post, category) VALUES ('$title', '$post', '$category')";
mysql_query($query) or die('Error, insert query failed');

$handle = fopen($blogimage,'r');
$file_content = fread($handle,filesize($blogimage));
fclose($handle);
$encoded = chunk_split(base64_encode($file_content)); 
$sql = "UPDATE braindump SET blogimage='$encoded' where title='$title' and category='$category'"; 
mysql_query($sql);

}


$query = "FLUSH PRIVILEGES";
mysql_query($query);

include 'closedb.php';
echo "Your Post has been submitted";
}
else
{
?>
<form method="post" enctype="multipart/form-data" OnSubmit="setTimeout('clear_form()', 2000); return true" action="<?php $_SERVER["PHP_SELF"];?>">
<table width="100%" border="0" cellpadding="0">
	<tr>
		<td colspan="2" width="100%" valign="top">
			<p align="center" class="style4">Blog Form
			</p>
		</td>
	</tr>
	<tr>
		<td width="20%" valign="top">
			<p align="center" class="style4">Post Title
			</p>
		</td>
		<td width="80%">
			<p align="left">
				<select name="category" size="7">
							<option value="Current Events">Current Events</option>
							<option value="Economy">Economy</option>
							<option value="Green">Green</option>
							<option value="Kitchen Sink">Kitchen Sink</option>
							<option value="Politics">Politics</option>
							<option value="Pop Culture">Pop Culture</option>
							<option value="SciTech">SciTech</option>
				</select><br /><br />
				<input name="title" type="text" size="100" maxlength="130" /><br /><br />
			</p>
		</td>
	</tr>
	<tr>
		<td width="20%">
			<p align="center" class="style4">Blog Post
			</p>
		</td>
		<td width="80%">
			<p align="left"><textarea name="post" cols="75" rows="15"></textarea>
			</p>
		</td>
	</tr>
	<tr>
		<td width="20%" valign="top">
			<p align="center" class="style4">Blog Post Password
			</p>
		</td>
		<td width="80%">
			<p align="left"><input name="blogpass" type="text" size="100" maxlength="130" /><br /><br />
			</p>
		</td>
	</tr>
	<tr>
		<td width="20%" valign="top">
			<p align="center" class="style4">Image Attach
			</p>
		</td>
		<td width="80%">
			<p align="left"><input name="blogimage" type="file" /><br /><br />
			</p>
		</td>
	</tr>
	<tr>
		<td width="20%" valign="top">&nbsp;
		</td>
		<td width="80%">
			<p align="left"><input name="add" type="submit" id="add" value="Submit" /><br /><br />
			</p>
		</td>
	</tr>
</table>
</form>
Last edited by Ezzaral; Apr 24th, 2009 at 1:32 pm. Reason: Added [code] [/code] tags. Please use them to format any code that you post.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 800
Reputation: Airshow is on a distinguished road 
Solved Threads: 113
Airshow's Avatar
Airshow Airshow is offline Offline
Practically a Posting Shark

Re: html form to php mysql db insert for image

 
0
  #2
Apr 24th, 2009
dwatycha,

For file upload, you really need to do some background reading. The PHP Manual is a good place to start:
http://www.php.net/manual/en/features.file-upload.php

But if you want to avoid a lot of heartache, then there's no shame in using a ready-written downloadable php blog script. There are many to choose from and most will represent at least a few hundred man-hours of effort, maybe a man-year or more! Notwithstanding your undoubted talent, you will find it hard to compete with that, and will be able to devote your time to customising the appearance of your blog rather than worrying about all that nauseating code. stuff

You will find many free and pay-for blog scripts in the web, eg. here, and many other good download sites.

Airshow
Last edited by Airshow; Apr 24th, 2009 at 5:39 pm. Reason: Typos
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC