Reputation: 11
I'm having a problem with my MySQL insert. When I upload a file, data like "filename", "time", and "gid" are inserted correctly. But "title" is empty.
Why is the "title" not being inserted?
<?php
$max_no_img = 10; // Maximum number of images value to be set here
echo "<form method=post action='' enctype='multipart/form-data'>";
echo "<table border='0' width='400' cellspacing='0' cellpadding='0' align=center>";
for ($i = 1; $i <= $max_no_img; $i++) {
echo " <tr><td><input type='text' name='title[]'></td><td><input type=file name='images[]' class='bginput'></td></tr>";
}
echo "<input type='hidden' name='gid' value='$info[id]'>";
echo "</table>";
echo "<br /> <center><input type=submit value='Dodaj sliku'></center>";
echo "</form>";
while (list($key, $value) = each($_FILES['images']['name'])) {
// echo $key;
// echo "<br />";
// echo $value;
// echo "<br />";
if (!empty($value)) { // this will check if any blank field is entered
$time = time();
$gid = addslashes($_POST['gid']);
$title = $_POST['title'];
$filename = rand(1, 100000) . $value; // filename stores the value
$filename = str_replace(" ", "_", $filename); // Add _ inplace of blank space in file name, you can remove this line
$add = "slike/$filename"; // upload directory path is set
// echo $_FILES['images']['type'][$key]; // uncomment this line if you want to display the file type
// echo "<br />"; // Display a line break
copy($_FILES['images']['tmp_name'][$key], $add);
mysql_query("INSERT INTO slike (title,slika,time,gid) VALUES ('$title','$filename','$time','$gid')") or die(mysql_error());
echo "Uspesno pritisnite <a href='/admin/?page=gall&id=$info[id]'>ovde</a>";
// upload the file to the server
chmod("$add", 0777); // set permission to the file.
}
}
}
}
Upvotes: 1
Views: 242
Reputation: 29178
Just as your script handles multiple uploaded images, each image has a corresponding title input.
<input type='text' name='title[]'>
So, the "title" data will be posted as an array:
Array (
[0] => title1
[1] => title2
[2] => title3
[3] => title4
)
You'll need to handle the array of titles appropriately, by using a key that corresponds to the array of uploaded images to reference each associated title. From the looks of your code, you may be able to use the $key
variable that is defined in your while
loop, like so:
$title = $_POST['title'][$key];
Note: There also seem to be a couple of extra closing braces }
in your PHP code.
Upvotes: 1