foreach mysql insert
Ik ben bezig met een site waarop o.a. een portfolio komt. De klant moet zelf via een zelfgebouwt CMS systeem portfolio items kunnen toevoegen. Nu heb ik een script waarbij dynamisch het aantal input fields voor afbeeldingen word bepaalt. Dit werkt allemaal prima, behalve dat hij alleen de eerste input field invoert in de database...
Is er iemand die ziet waar het probleem zit?
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
include('functions/connection.php');
include("functions/XXXX");
include("functions/XXXX");
//voor de foto upload
//VOOR DE ADMIN CHECK
$sql="XXXXXXXX";
$result=mysql_query($sql);
$data=mysql_fetch_array($result);
$gebruiker=$_SESSION['username'];
if(!isset($_GET['x'])){
$aantal=2;
}else{
$aantal= $_GET['x'];
}
?>
<html>
<head>
</head>
<body>
<h3>portfolio item toevoegen</h3>
<fieldset class="dashboard">
<legend class="dashboard">Beheer</legend>
<?php
if (isset($_REQUEST['submit'])) {
$name = $_POST["name"];
$description = $_POST["description"];
$video = $_POST["video"];
$fieldname = 'imgfile';
$active_keys = array();
foreach($_FILES[$fieldname]['name'] as $key => $filename) {
if(!empty($filename)) {
$active_keys[] = $key;
}
}
foreach($active_keys as $key) {
//foto uploaden
$path_thumbs = "upload/thumbs";
$path_big = "upload/images";
//the new width of the resized image.
$img_thumb_width = 150; // in pixel
//Do you want to limit the extensions of files uploaded (yes/no)
$extlimit = "no";
//allowed Extensions
$limitedext = array(".gif",".jpg",".png",".jpeg",".bmp");
$file_type = $_FILES['imgfile']['type'][$key];
$file_name = $_FILES['imgfile']['name'][$key];
$file_size = $_FILES['imgfile']['size'][$key];
$file_tmp = $_FILES['imgfile']['tmp_name'][$key];
//check file extension
$ext = strrchr($file_name,'.');
$ext = strtolower($ext);
if (($extlimit == "yes") && (!in_array($ext,$limitedext))) {
echo "Verkeerde extensie. <br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";
exit();
}
//get the file extension.
$file_ext = end(explode(".", $file_name));
//create a random file name
$rand_name = md5(time());
$rand_name= rand(0,999999999);
//get the new width variable.
$ThumbWidth = $img_thumb_width;
//keep image type
if($file_size){
if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){
$new_img = imagecreatefromjpeg($file_tmp);
}elseif($file_type == "image/x-png" || $file_type == "image/png"){
$new_img = imagecreatefrompng($file_tmp);
}elseif($file_type == "image/gif"){
$new_img = imagecreatefromgif($file_tmp);
}
//list width and height and keep height ratio.
list($width, $height) = getimagesize($file_tmp);
$imgratio=$width/$height;
if ($imgratio>1){
$newwidth = $ThumbWidth;
$newheight = $ThumbWidth/$imgratio;
}else{
$newheight = $ThumbWidth;
$newwidth = $ThumbWidth*$imgratio;
}
//function for resize image.
if (function_exists(imagecreatetruecolor)){
$resized_img = imagecreatetruecolor($newwidth,$newheight);
}else{
die("Error: Please make sure you have GD library ver 2+");
}
imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
//save image
ImageJpeg ($resized_img,"$path_thumbs/$rand_name.$file_ext");
ImageDestroy ($resized_img);
ImageDestroy ($new_img);
move_uploaded_file ($file_tmp, "$path_big/$rand_name.$file_ext");
}
}
//foto's toevoegen
foreach($active_keys as $key) {
mysql_query("INSERT INTO images (link,thumb,name) VALUES ('$path_big/$rand_name.$file_ext','$path_thumbs/$rand_name.$file_ext','$name')");
}
//algemene data invoegen
$sql = "INSERT INTO projecten (name,description,video) VALUES ('$name','$description','$video')";
if( ($result = mysql_query($sql)) ) {
echo '<p><img src="images/icons/accept.gif" alt"" /> Portfolio item succesvol aangemaakt!</p>';
$page = "index.php?page=portfolio";
$sec = "1";
//header("Refresh: $sec; url=$page");
} else {
echo "ERROR: ".mysql_error();
}
} else {
?>
<div id="container">
<form action="" enctype="multipart/form-data" method="post" class="niceform" name="UD">
<dl>
<dt><label for="name">Titel:</label></dt>
<dd><input type="text" name="name" id="name" size="32" maxlength="128" /></dd>
</dl>
<dl>
<dt><label for="description">Beschrijving:</label></dt>
<dd><textarea name="description" id="description" rows="8" cols="65"></textarea></dd>
</dl>
<?php for($i=0; $i < $aantal; $i++) { ?>
<dl>
<dt><label for="imgfile">Kies foto:</label></dt>
<dd><input name="imgfile[]" id="imgfile[]" type="file" /></dd>
</dl>
<?php } ?>
<dl>
<dt><label for="video">(Embed) Video:</label></dt>
<dd><textarea name="video" id="video" rows="8" cols="65"></textarea></dd>
</dl>
<dd><input type="submit" name="submit" id="submit" value="voeg toe" /></dd>
</form>
</div>
</fieldset>
<?php
}
?>
</body>
</html>
include('functions/connection.php');
include("functions/XXXX");
include("functions/XXXX");
//voor de foto upload
//VOOR DE ADMIN CHECK
$sql="XXXXXXXX";
$result=mysql_query($sql);
$data=mysql_fetch_array($result);
$gebruiker=$_SESSION['username'];
if(!isset($_GET['x'])){
$aantal=2;
}else{
$aantal= $_GET['x'];
}
?>
<html>
<head>
</head>
<body>
<h3>portfolio item toevoegen</h3>
<fieldset class="dashboard">
<legend class="dashboard">Beheer</legend>
<?php
if (isset($_REQUEST['submit'])) {
$name = $_POST["name"];
$description = $_POST["description"];
$video = $_POST["video"];
$fieldname = 'imgfile';
$active_keys = array();
foreach($_FILES[$fieldname]['name'] as $key => $filename) {
if(!empty($filename)) {
$active_keys[] = $key;
}
}
foreach($active_keys as $key) {
//foto uploaden
$path_thumbs = "upload/thumbs";
$path_big = "upload/images";
//the new width of the resized image.
$img_thumb_width = 150; // in pixel
//Do you want to limit the extensions of files uploaded (yes/no)
$extlimit = "no";
//allowed Extensions
$limitedext = array(".gif",".jpg",".png",".jpeg",".bmp");
$file_type = $_FILES['imgfile']['type'][$key];
$file_name = $_FILES['imgfile']['name'][$key];
$file_size = $_FILES['imgfile']['size'][$key];
$file_tmp = $_FILES['imgfile']['tmp_name'][$key];
//check file extension
$ext = strrchr($file_name,'.');
$ext = strtolower($ext);
if (($extlimit == "yes") && (!in_array($ext,$limitedext))) {
echo "Verkeerde extensie. <br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";
exit();
}
//get the file extension.
$file_ext = end(explode(".", $file_name));
//create a random file name
$rand_name = md5(time());
$rand_name= rand(0,999999999);
//get the new width variable.
$ThumbWidth = $img_thumb_width;
//keep image type
if($file_size){
if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){
$new_img = imagecreatefromjpeg($file_tmp);
}elseif($file_type == "image/x-png" || $file_type == "image/png"){
$new_img = imagecreatefrompng($file_tmp);
}elseif($file_type == "image/gif"){
$new_img = imagecreatefromgif($file_tmp);
}
//list width and height and keep height ratio.
list($width, $height) = getimagesize($file_tmp);
$imgratio=$width/$height;
if ($imgratio>1){
$newwidth = $ThumbWidth;
$newheight = $ThumbWidth/$imgratio;
}else{
$newheight = $ThumbWidth;
$newwidth = $ThumbWidth*$imgratio;
}
//function for resize image.
if (function_exists(imagecreatetruecolor)){
$resized_img = imagecreatetruecolor($newwidth,$newheight);
}else{
die("Error: Please make sure you have GD library ver 2+");
}
imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
//save image
ImageJpeg ($resized_img,"$path_thumbs/$rand_name.$file_ext");
ImageDestroy ($resized_img);
ImageDestroy ($new_img);
move_uploaded_file ($file_tmp, "$path_big/$rand_name.$file_ext");
}
}
//foto's toevoegen
foreach($active_keys as $key) {
mysql_query("INSERT INTO images (link,thumb,name) VALUES ('$path_big/$rand_name.$file_ext','$path_thumbs/$rand_name.$file_ext','$name')");
}
//algemene data invoegen
$sql = "INSERT INTO projecten (name,description,video) VALUES ('$name','$description','$video')";
if( ($result = mysql_query($sql)) ) {
echo '<p><img src="images/icons/accept.gif" alt"" /> Portfolio item succesvol aangemaakt!</p>';
$page = "index.php?page=portfolio";
$sec = "1";
//header("Refresh: $sec; url=$page");
} else {
echo "ERROR: ".mysql_error();
}
} else {
?>
<div id="container">
<form action="" enctype="multipart/form-data" method="post" class="niceform" name="UD">
<dl>
<dt><label for="name">Titel:</label></dt>
<dd><input type="text" name="name" id="name" size="32" maxlength="128" /></dd>
</dl>
<dl>
<dt><label for="description">Beschrijving:</label></dt>
<dd><textarea name="description" id="description" rows="8" cols="65"></textarea></dd>
</dl>
<?php for($i=0; $i < $aantal; $i++) { ?>
<dl>
<dt><label for="imgfile">Kies foto:</label></dt>
<dd><input name="imgfile[]" id="imgfile[]" type="file" /></dd>
</dl>
<?php } ?>
<dl>
<dt><label for="video">(Embed) Video:</label></dt>
<dd><textarea name="video" id="video" rows="8" cols="65"></textarea></dd>
</dl>
<dd><input type="submit" name="submit" id="submit" value="voeg toe" /></dd>
</form>
</div>
</fieldset>
<?php
}
?>
</body>
</html>
kun je $key is een echo geven als die word uitgevoerd en kijken of die meerdere malen word uitgevoerd bij meerdere bestanden?
Dat moet die wel doen want alle bestanden worden wel netjes geupload.
Code (php)
1
2
3
4
5
2
3
4
5
<?php
foreach($active_keys as $key) {
mysql_query("INSERT INTO images (link,thumb,name) VALUES ('$path_big/$rand_name.$file_ext','$path_thumbs/$rand_name.$file_ext','$name')");
}
?>
foreach($active_keys as $key) {
mysql_query("INSERT INTO images (link,thumb,name) VALUES ('$path_big/$rand_name.$file_ext','$path_thumbs/$rand_name.$file_ext','$name')");
}
?>
Want hoe vaak deze loop ook loopt, het zijn allemaal dezelfde records.
Hou $vars buiten de quotes.
het zit er goed in alleen het is maar 1 entry, dus hij vult niet alles in maar alleen de eerste....
De tweede regel overschrijft gewoon de eerste.
En een fatsoenlijke naam begint nooit met een cijfer.
Gewijzigd op 03/11/2010 15:05:52 door - SanThe -
so.... back ontopic :)