Forum Moderators: coopster

Message Too Old, No Replies

inserting images in my db

         

rekhad

11:05 pm on Mar 15, 2006 (gmt 0)

10+ Year Member



In my database just one row is added not two,and it remains static ie doesn't change even if I upload a different image.Can someone help?

my form.php is
<form enctype="multipart/form-data"
action="new1.php" method="post">
<input type="hidden" name="MAX_FILE_SIZE"
value="300000">
<input name="userfile[]" type="file" /><br />
<input name="userfile[]" type="file" /><br />
<input type="submit" />
</form>

new1.php

<html>
<h1>Enter Name and Descriptions of Pics</h1>
<?php
$dbh=mysql_connect("localhost", "root", "password")
or die ('I cannot connect to the database.');
mysql_select_db("db");

$tot=count($_FILES['userfile']['name']);
echo"$tot<br>";

for ($i = 0; $i < $tot; $i++) {

$tmp_name = $_FILES['userfile']['tmp_name'][$i];
$pic_name = $_FILES['userfile']['name'][$i];
echo "moving $tmp_name to $pic_name<br>";
if (move_uploaded_file($tmp_name, $pic_name)) {
echo "updating database about $pic_name<br>";
$sql = "INSERT INTO `pics`( `pic_name` , `descrip`
) VALUES ('$pic_name' , '')";
echo "query is $sql<br>";
echo"$i<br>";
$result = mysql_query($sql);
echo "$result<br>";
}
}
mysql_close();
?>

omoutop

1:40 pm on Mar 16, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



since u have named both your form fields the same name, u pass an array containing all images (and their attribute).

Use a foreach loop to extract info from array and insert it into your table OR renme the form fields and use two scripts to insert them.

rekhad

3:09 pm on Mar 16, 2006 (gmt 0)

10+ Year Member



my print statement shows that two image files are uploaded,but in my database that doesn't show up.See the output...

2
moving C:/Program Files/EasyPHP1-8\tmp\php5.tmp to jordan_d3.gif
updating database about jordan_d3.gif
query is INSERT INTO `pics`( `pic_name` , `descrip` ) VALUES ('jordan_d3.gif' , '')
0

moving C:/Program Files/EasyPHP1-8\tmp\php6.tmp to jordan_d4.jpg
updating database about jordan_d4.jpg
query is INSERT INTO `pics`( `pic_name` , `descrip` ) VALUES ('jordan_d4.jpg' , '')
1

Habtom

5:01 pm on Mar 16, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I would let you know if I come across the solution.

[edited by: Habtom at 5:03 pm (utc) on Mar. 16, 2006]

Habtom

5:01 pm on Mar 16, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Would you consider dumping the file in a folder and saving the filename in the database. I haven't come across yet for a need of storing the file itself in the database.

You might be fighting the wrong war.

Habtom

rekhad

7:38 pm on Mar 16, 2006 (gmt 0)

10+ Year Member



could you show a sample program?