Jump to content

upload form help


applebiz89

Recommended Posts

hi there,

 

Im running into a dead brainer here.  I want to allow users to upload an article, and in this article have text and an image. So the article will have a title, image and content.

 

Hard to describe, but my problem is this: Uploading these from a form, so that in the database they appear in just one row together, so the image and text is linked to the title of the article. How could I go about doing this, as the ID's of the articles will automatically be generated, im not sure how i could code is so that all of these are specific to their id's.

 

I have this image upload script already from another use site i have developed and have start to re work it, but wondering how i could adapt this with a form with more than just the image upload in it. Sorry if sounds bit complicated, hard to word what im thinking

 

thanks in advance

 

 

<?phprequire("include/conn.php");include "include/session.php";// Check to see if the type of file uploaded is a valid image typefunction is_valid_type($file){               $valid_types = array("image/jpg", "image/jpeg", "image/bmp", "image/gif");        if (in_array($file['type'], $valid_types))                return 1;        return 0;}function showContents($array){        echo "<pre>";        print_r($array);        echo "</pre>";}$TARGET_PATH = "images/";// Get POSTed variables$image = $_FILES['image'];$image['name'] = mysql_real_escape_string($image['name']);$TARGET_PATH .= $image['name'];// Make sure all the fields from the form have inputsif ($image['name'] == "" ){        $_SESSION['error'] = "All fields are required";        header("Location: addvox.php");        exit;}// Verify file is an imageif (!is_valid_type($image)){        $_SESSION['error'] = "You must upload a jpeg, gif, or bmp";        header("Location: addvox.php");        exit;}// move file into the directory and the path name into the databaseif (move_uploaded_file($image['tmp_name'], $TARGET_PATH)){        $sql = "UPDATE vox SET image = '" . $image['name'] . //where id = 'VOX_ID';        $result = mysql_query($sql) or die ("Could not insert data into DB: " . mysql_error());        header("Location: user-page.php");        exit;}else{                $_SESSION['error'] = "Could not upload file.  Check read/write persmissions on the directory";                exit;}?>

 

Link to comment
Share on other sites

i would have a db table like

articleID | title | articleText | image

 

with the image column storing the filename of the image, i.e. image1.jpg

 

I would then wait for the image script to execute and then if that is successful insert the article details into the DB.

i.e. swap the last part of the upload script to something like

 

// move file into the directory and the path name into the database// also must execute if no image has been uploadedif (move_uploaded_file($image['tmp_name'], $TARGET_PATH)){        $sql = @mysql_query("INSERT INTO xxx SET rarara = rara, image = {$image['name']}");if ($sql){$success = "successful!";}else{$error = "error!";}        exit;}else{                $_SESSION['error'] = "Could not upload file.  Check read/write persmissions on the directory";                exit;}

 

 

then you will have to ensure the script is capable of allowing a user to upload an article without an image, i.e. set the mysql image field to default to "default.jpg" and then ensure default.jpg exists in the images folder.

Link to comment
Share on other sites

I don't use PHPmyadmin, I'm sure there is a default value option for each column?

if you were writing the create table query in SQL, it would look something like

CREATE TABLE articles (

  articleID int(11) NOT NULL auto_increment,

  name varchar(50) NOT NULL default '',

  articleText TEXT NOT NULL default '',

  image varchar(150) NOT NULL default 'default.jpg',

  PRIMARY KEY  (articleID)

)

 

... or if you've already created the table, you can alter it with sql

ALTER TABLE articles MODIFY image varchar(150) NOT NULL DEFAULT 'default_image.jpg';

Link to comment
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.