Jump to content

Archived

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

Mikkel

Dynamic text and image (blob) upload to MySql

Recommended Posts

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.
[code]
<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>[/code]

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?

[code]<?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!");
}
?> [/code]

Share this post


Link to post
Share on other sites
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.
[table][tr][td]<input type="file" size="20" name="UploadFile" id="q3">[/td][/tr][/table]
To send that data to your database I'm not sure of but hopefully that will help you out with that part of it.

Share this post


Link to post
Share on other sites
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. :)

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.