Jump to content


Photo

Dynamic text and image (blob) upload to MySql


  • Please log in to reply
2 replies to this topic

#1 Mikkel

Mikkel
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 24 August 2006 - 07:06 AM

I'm a PHP/MySql rookie, and these last few weeks I've been working on a problem.

I need to make a site that is fully dynamic for the end user. I'm able to make PHP forms to submit text to the database, but is there also a way to incorporate image upload in such a form?

I've experimented with BLOB storage, and have managed to store images in the database via a PHP script, but so far haven't been able to retrieve them as jpg's in the browser. I've tried so many tutorials, but nothing has worked.

My ultimate wish is to have 1 single form for the user to enter a Headline, Text, and Name, and optionally, browse their HD for a image to be uploaded to the DB. This image will then be posted on the front page along with the other rows (text).

Is this even possible with PHP?

Would be immensly grateful for ANY help.  :)


This is the form located in news_form.php.
<form id="form1" name="form1" method="post" action="save_form.php">
<input name="headline" type="text" style='width: 98%' maxlength="50"/>
<textarea name="text" style="width: 98%; height: 100px"></textarea>
<input name="name" type="text" style='width: 40%' maxlength="50"/>
<input type="reset" name="Reset" value="Empty form" />
<input type="submit" name="Submit" value="Publish" />
</form>

This is the PHP currently in save_form.php. Everything works brilliantly so far, but what if I wanted to be able to upload an JPG file in the same form, along with the text?

<?PHP  
   
  $headline = addslashes(htmlentities($_POST['headline'])); 
  $text = addslashes(htmlentities($_POST['text']));   
  $name = addslashes(htmlentities($_POST['name']));
  

if($headline!="" and $text!="" and $name!=""){ 

    $dbconnection = mysql_connect('localhost','*****','****'); 
    mysql_select_db('******',$dbconnection);
	$created_time = date("Y.m.d H.i.s"); 
     
    $sql = "Insert into news(headline,text,name,created_time) Values('$headline','$text','$name','$created_time')"; 
    $dbResults = mysql_query($sql); 
     
    if($dbResults) { 
        echo("<b>Congratulations, your news have been posted.</b> <br/>"); 
        echo("<p><a href='../../index.php'>Click here</a> to read your news, or <a href='../../kontrollpanel.php'>go back to the control panel.</a></p>"); 
    }else { 
        echo("<b>Oops, something went wrong! Contact webmaster.</b>"); 
    } 
     
}else{ 
    echo("You need to fill out the whole form!"); 
} 
?>


#2 jwk811

jwk811
  • Members
  • PipPipPip
  • Advanced Member
  • 714 posts

Posted 24 August 2006 - 07:24 AM

Are you looking to add a file uploader in a form? I don't know that much about php but if this is what you're looking for the input type is file. Then that will become apart of the form for the users to upload files. The html or php part in the form for the file upload will look something like this.
<input type="file" size="20" name="UploadFile" id="q3">

To send that data to your database I'm not sure of but hopefully that will help you out with that part of it.

#3 Mikkel

Mikkel
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 24 August 2006 - 07:28 AM

Yes, I'm looking for a way of putting file (image) upload into a form, along with text upload. It's really the PHP upload (to the DB) script itself that I'm having trouble with - the stuff that would go into save_form.php.

Thanks alot though. :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users