Current location: Hot Scripts Forums » Programming Languages » PHP » Loading an image into database with customerID


Loading an image into database with customerID

Reply
  #1 (permalink)  
Old 02-23-04, 03:24 PM
pecobrown pecobrown is offline
New Member
 
Join Date: Feb 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Loading an image into database with customerID

Hi everyone,

I will very quickly explain what im doing. I have a 2 files called addcustomer.php & fileupload.php

Two tables called customers & images

Im new to php and I got a script for helping to upload images into a database table called images. This works fine on its own. The only problem I have is that I need to add the customerID with the INSERT command into the sql query. ( i think ! )

This is the table that the customerID and image details are going to .

CREATE TABLE `image` (
`image_id` int(10) unsigned NOT NULL auto_increment,
`customerID` tinyint(11) NOT NULL default '0',
`image_type` varchar(50) NOT NULL default '',
`image` longblob NOT NULL,
`image_size` bigint(20) NOT NULL default '0',
`image_name` varchar(255) NOT NULL default '',
`image_date` datetime NOT NULL default '0000-00-00 00:00:00',
UNIQUE KEY `image_id` (`image_id`)
) TYPE=MyISAM ;

The page addcustomer.php is passing the companyName to fileupload.php

Once I have this i can then get the customerID from the database.
The only help I need is getting the customerID into the database
Any ideas would be great, also if you think im doing anything wrong please mention also.

Thanks in advance , pecobrown

Fileupload.php
<?php

// database connection
$conn = mysql_connect("localhost", "username", "password") OR DIE (mysql_error());
@mysql_select_db ("mydatabase", $conn) OR DIE (mysql_error());

//carry over companyName from previous page and then get customerID from the database

$companyName = $_POST['companyName'];

$sql = mysql_query("SELECT customerID FROM tblcustomers WHERE companyName = '$companyName' ");
$getid = mysql_fetch_array($sql);
$customerID = $getid['customerID'];

// Do this process if user has browse the file and click the submit button to add image
if ($_FILES) {
$image_types = Array ("image/bmp",
"image/jpeg",
"image/pjpeg",
"image/gif",
"image/x-png");

$userfile = addslashes (fread (fopen ($_FILES["userfile"] ["tmp_name"], "r"), filesize ($_FILES["userfile"]["tmp_name"])));
$file_name = $_FILES["userfile"]["name"];
$file_size = $_FILES["userfile"]["size"];
$file_type = $_FILES["userfile"]["type"];

// this will add image details ,but also customerID at the same time
if (in_array (strtolower ($file_type), $image_types)) {
$sql = "INSERT INTO image (image_type, image, image_size, image_name, image_date) ";
$sql.= "VALUES (";
$sql.= "'{$file_type}', '{$userfile}', '{$file_size}', '{$file_name}', NOW())";
@mysql_query ($sql, $conn);
Header("Location:".$_SERVER["PHP_SELF"]);
exit();
}
}
?>
<html>
<head>
<title>Storing Images in DB with customerID</title>
<link href="./assets/css/wplanner.css" rel="stylesheet" type="text/css">
</head>
<body class="smalltextlink">
<form method="post" enctype="multipart/form-data">
<span class="smalltext">Select Image File:</span>
<input type="file" name="userfile" size="40"><input type="submit" value="submit">
</form>
</body>
</html>

// Example PHP Script, demonstrating Storing Image in Database
// Detailed Information can be found at http://www.codewalkers.com
Reply With Quote
  #2 (permalink)  
Old 02-26-04, 03:32 AM
the_mole001's Avatar
the_mole001 the_mole001 is offline
Newbie Coder
 
Join Date: Feb 2004
Location: Australia
Posts: 96
Thanks: 0
Thanked 0 Times in 0 Posts
Hello pecobrown,

I'm not 100% sure I read what you were asking 100% correctly but if you wanted to enter the id witht he rest of the data wouldn't you try something like:

PHP Code:

if (in_array (strtolower ($file_type), $image_types)) {

$sql "INSERT INTO image (customerID, image_type, image, image_size, image_name, image_date) "
$sql.= "VALUES (";
$sql.= "'$customerID', '{$file_type}', '{$userfile}', '{$file_size}', '{$file_name}', NOW())"
Just an idea if thats what you were asking,

Peter
Reply With Quote
  #3 (permalink)  
Old 02-27-04, 01:11 PM
pecobrown pecobrown is offline
New Member
 
Join Date: Feb 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Cool

Thank you Peter , you are a star !!!
As I said , im very new to php and this wasnt my script

You have helped me a great deal and I can thank you enough !

Nice one

Ross
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP Form to update a MySQL database? Scoobler PHP 9 09-04-08 01:41 AM
mySQL database - Image size reduction within DB trigger_my_passion PHP 0 01-29-04 04:24 PM
Share database over the Internet nitinkedia The Lounge 2 11-20-03 02:07 PM
Share database over the internet nitinkedia PHP 0 07-11-03 12:22 AM


All times are GMT -5. The time now is 09:04 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.