Jump to content

Recommended Posts

Hi,

 

I have a script that uploads an image to a remote directory, But I need to know how you get the Image string added to the Database.

 

The table is called "Ideas" and it will also insert data such as Title and Description, But thats not the issue, Its getting the image into the DB and calling it back out

 

Any help appreciated, Cheers

Link to comment
https://forums.phpfreaks.com/topic/91866-adding-an-image-to-a-database/
Share on other sites

When you grab information from PHP after an uploaded you'll have available the $_FILES[] superglobal.

I believe what you're after is $_FILES['PUT YOUR FIELD NAME HERE']['name']. Put that into a variable and stick it in your database, simple!

 

See http://uk3.php.net/features.file-upload for further information.

Simple  ??? ?

 

I'm struggling with the concept of that and where to put it

 

I've got my script setup, Its inserting all the Variables into the DB Table other than that of the image string or anything, I've tried a few chops and changes as shown on that UK2.php.net/ site

 

<?php
include ("dbconnect.php");


# Connect to PHPMyAdmin DB
$db = mysql_connect($hostname, $db_user, $db_password);
mysql_select_db($dbname,$db);
?>

<?php #Script idea-submit.php 
//Collect Forms' Post Values
$title = $_POST['title'];
$description = $_POST['description'];
$category = $_POST['category'];
$img = $_FILES['image']['name'];

//Validation Check
//Check Ideas' Title
if (!empty($_POST['title'])) {
$title = ($_POST['title']);
}else{
$title = NULL;
echo "<p>You need to enter your ideas' title</p>";
}

//Check Description
if (!empty($_POST['description'])) {
$description = ($_POST['description']);
}else{
$description = NULL;
echo "<p>You need to enter your ideas' description </p>";
}

// If everything is filled out print the message.
if($title && $description) {

{ 
// If all is ok, Insert into DB
$sql = "INSERT INTO $db_table2(title,description,category,image) values ('$title','$description','$category','$img')"; 
// Incase needed($result = mysql_query($sql ,$db));
($result = mysql_query($sql ,$db) or die(mysql_error()));

echo "Thank you, For your ideas<br /> <br />
You entered:</b> <b>Title</b> : $title<br /><br /> <b>Description</b>: $description";


}
?>

<?php 


?>
<?php 
} 
?> 

 

Is my code

Its that $img that I need to output a string to the Database! But thinking about it I'm also not uploading that image anywhere either, Whats the best way to include an Upload for the image aswell so that the database then links to the URL String of the Upload?

 

Many Thanks

the echo of $img didnt output anything at all out to the screen

 

So if thats the case I'll put the code for the ideas.php page on here, Which is the form that this collects the data from

 

It is as follows (I'm not even 100% sure that submitting this form as it is, Is uploading the file, I need it to go to an folder titled 'ideas' and the URL String to be output into the Database

 

<?php
session_start(); 
include ("dbconnect.php");

if(!isset($_SESSION[username])){ 
$message = <<<HTML
This page is for Registered users only <br /> <br />
Register to the site, <br /> <br />
By following the link in the menu on the left

HTML;

}else{ 
  //Protected Area sits below
  $ideas_form = <<<FORM
  <form action="idea-submit.php" method="post" enctype="multipart/form-data" name="idea" id="idea">
<fieldset>
<div>
  <label for ="ideatitle=">Idea Title :
  <input type="text" name="title" id="title" class="txt"  />
  </label>
</div>
<div>
  <label>Idea Description :
  <textarea name="description" id="description" class="txt" /></textarea>
  </label>
</div>
<div>
  <label>Idea category :
  <select name="category" id="category">
    <option>Sports</option>
    <option>Retail</option>
    <option>Clothing</option>
  </select>
</div>
<!-- The data encoding type, enctype, MUST be specified as below -->
<form enctype="multipart/form-data" action="__URL__" method="POST">
    <!-- MAX_FILE_SIZE must precede the file input field -->
    <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
    <!-- Name of input element determines name in $_FILES array -->
    Send this file: <input name="file" type="file" id="image" /><br><br>
    <input type="submit" value="Submit Your Idea" class="btn"/>
</form>

FORM;

} 
?>

Right, Here goes

 

I've managed to look at that and pair up the "name" from the html page, Paired that up with that in the PHP Page

 

and the echo is "ideas/"

 

The code is as follows :

The Database is now picking up the string of "filename.jpg" in the Database, But I want it to reference to "/ideas/filename.jpg"

So when I pull it in from the Database it is shown Dynamically

 

<?php

include ("dbconnect.php");


# Connect to PHPMyAdmin DB
$db = mysql_connect($hostname, $db_user, $db_password);
mysql_select_db($dbname,$db);
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php #Script idea-submit.php By Stuart Blackett - d4066435@tees.ac.uk
//Collect Forms' Post Values
$title = $_POST['title'];
$description = $_POST['description'];
$category = $_POST['category'];
$img = $_FILES['image']['name'];

//Validation Check
//Check Ideas' Title
if (!empty($_POST['title'])) {
$title = ($_POST['title']);
}else{
$title = NULL;
echo "<p>You need to enter your ideas' title</p>";
}

//Check Description
if (!empty($_POST['description'])) {
$description = ($_POST['description']);
}else{
$description = NULL;
echo "<p>You need to enter your ideas' description </p>";
}
//Handle the Image
//Set Images Upload Directory
$uploaddir = "/ideas"; 
// Upload Image
if(is_uploaded_file($_FILES['image']['tmp_name']))
{
move_uploaded_file($_FILES['image']['tmp_name'],$uploaddir.''.$_FILES['file']['name']);
}

if($title && $description) {

{ 
// If all is ok, Insert into DB
$sql = "INSERT INTO $db_table2(title,description,category,image) values ('$title','$description','$category','$img')"; 
// Incase needed($result = mysql_query($sql ,$db));
($result = mysql_query($sql ,$db) or die(mysql_error()));

echo "Thank you, For your ideas<br /> <br />
You entered:</b> <b>Title</b> : $title<br /><br /> <b>Description</b>: $description"; 
echo $uploaddir;
}
}
?>

</body>
</html>

 

I tried editing this post this morning to update you on how I was getting on, But it wouldnt let me  :'( :'(

Going by your use of $_FILES['image']['tmp_name'] (note image NOT file as it should be) your file is not being properly moved from it's temporary hold?

 

Also, surely to have the full path to the image, all you need is to change your $img to include the directory before you insert it into your SQL.

e.g.

$img = $uploaddir."/".$img;

 

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.