Hey,
Ik heb op een site een stuk code staan waarmee foto's geupload worden en naar een database geschreven kunnen worden.
Punt is dat de query niet goed (en heel omslachtig) wordt opgebouwd. Als er nu bijvoorbeeld al 10 foto's zijn geupload, en ik wil een 11e foto uploaden krijg ik een komma in de query, omdat die verkeerd wordt opgebouwd.
Kan iemand zeggen hoe dit gemakkelijker (en overzichtelijker) kan?
Alvast bedankt
Code:
PHP Code:
$form_img .= '<label>Image 1: </label> <input type="file" name="uplimg1"><br />
<label>Image 2: </label> <input type="file" name="uplimg2"><br />
<label>Image 3: </label> <input type="file" name="uplimg3"><br />
<label>Image 4: </label> <input type="file" name="uplimg4"><br />
<label>Image 5: </label> <input type="file" name="uplimg5"><br />
<label>Image 6: </label> <input type="file" name="uplimg6"><br />
<label>Image 7: </label> <input type="file" name="uplimg7"><br />
<label>Image 8: </label> <input type="file" name="uplimg8"><br />
<label>Image 9: </label> <input type="file" name="uplimg9"><br />
<label>Image 10: </label> <input type="file" name="uplimg10"><br />
<label>Image 11: </label> <input type="file" name="uplimg11"><br />
<label>Image 12: </label> <input type="file" name="uplimg12"><br />
<label>Image 13: </label> <input type="file" name="uplimg13"><br />
<label>Image 14: </label> <input type="file" name="uplimg14"><br />
<label>Image 15: </label> <input type="file" name="uplimg15"><br />
';
$htmo .= '
<p>'.$feedback.'</p>
<form method="post" enctype="multipart/form-data">
'.$form_img.' <br />
<input type="submit" name="submitfoto" value="Upload Afbeeldingen" style="margin-left: 50px;" />
</form>
';
echo $htmo;
if($_SERVER['REQUEST_METHOD'] == 'POST') {
if(isset($_POST['submitfoto'])) {
$sql = "UPDATE aanbod SET ";
//check of 1 is gevuld
if ($_FILES['uplimg1']['size'] > 0){
$sql = $sql . "foto1 = '".$_FILES['uplimg1']['name']."'";
}
//komma zetten als 2 is gevuld
if ($_FILES['uplimg2']['size'] > 0){
$sql = $sql . ", ";
}
//check of 2 is gevuld
if ($_FILES['uplimg2']['size'] > 0){
$sql = $sql . "foto2 = '".$_FILES['uplimg2']['name']."'";
}
//komma zetten als 3 is gevuld
if ($_FILES['uplimg3']['size'] > 0){
$sql = $sql . ", ";
}
//check of 3 is gevuld
if ($_FILES['uplimg3']['size'] > 0){
$sql = $sql . "foto3 = '".$_FILES['uplimg3']['name']."'";
}
//komma zetten als 4 is gevuld
if ($_FILES['uplimg4']['size'] > 0){
$sql = $sql . ", ";
}
//check of 4 is gevuld
if ($_FILES['uplimg4']['size'] > 0){
$sql = $sql . "foto4 = '".$_FILES['uplimg4']['name']."'";
}
//komma zetten als 5 is gevuld
if ($_FILES['uplimg5']['size'] > 0){
$sql = $sql . ", ";
}
//check of 5 is gevuld
if ($_FILES['uplimg5']['size'] > 0){
$sql = $sql . "foto5 = '".$_FILES['uplimg5']['name']."'";
}
//komma zetten als 6 is gevuld
if ($_FILES['uplimg6']['size'] > 0){
$sql = $sql . ", ";
}
//check of 6 is gevuld
if ($_FILES['uplimg6']['size'] > 0){
$sql = $sql . "foto6 = '".$_FILES['uplimg6']['name']."'";
}
//komma zetten als 7 is gevuld
if ($_FILES['uplimg7']['size'] > 0){
$sql = $sql . ", ";
}
//check of 7 is gevuld
if ($_FILES['uplimg7']['size'] > 0){
$sql = $sql . "foto7 = '".$_FILES['uplimg7']['name']."'";
}
//komma zetten als 8 is gevuld
if ($_FILES['uplimg8']['size'] > 0){
$sql = $sql . ", ";
}
//check of 8 is gevuld
if ($_FILES['uplimg8']['size'] > 0){
$sql = $sql . "foto8 = '".$_FILES['uplimg8']['name']."'";
}
//komma zetten als 9 is gevuld
if ($_FILES['uplimg9']['size'] > 0){
$sql = $sql . ", ";
}
//check of 9 is gevuld
if ($_FILES['uplimg9']['size'] > 0){
$sql = $sql . "foto9 = '".$_FILES['uplimg9']['name']."'";
}
//komma zetten als 10 is gevuld
if ($_FILES['uplimg10']['size'] > 0){
$sql = $sql . ", ";
}
//check of 10 is gevuld
if ($_FILES['uplimg10']['size'] > 0){
$sql = $sql . "foto10 = '".$_FILES['uplimg10']['name']."'";
}
//komma zetten als 11 is gevuld
if ($_FILES['uplimg11']['size'] > 0){
$sql = $sql . ", ";
}
//check of 11 is gevuld
if ($_FILES['uplimg11']['size'] > 0){
$sql = $sql . "foto11 = '".$_FILES['uplimg11']['name']."'";
}
//komma zetten als 12 is gevuld
if ($_FILES['uplimg12']['size'] > 0){
$sql = $sql . ", ";
}
//check of 12 is gevuld
if ($_FILES['uplimg12']['size'] > 0){
$sql = $sql . "foto12 = '".$_FILES['uplimg12']['name']."'";
}
//komma zetten als 13 is gevuld
if ($_FILES['uplimg13']['size'] > 0){
$sql = $sql . ", ";
}
//check of 13 is gevuld
if ($_FILES['uplimg13']['size'] > 0){
$sql = $sql . "foto13 = '".$_FILES['uplimg13']['name']."'";
}
//komma zetten als 14 is gevuld
if ($_FILES['uplimg14']['size'] > 0){
$sql = $sql . ", ";
}
//check of 14 is gevuld
if ($_FILES['uplimg14']['size'] > 0){
$sql = $sql . "foto14 = '".$_FILES['uplimg14']['name']."'";
}
//komma zetten als 15 is gevuld
if ($_FILES['uplimg15']['size'] > 0){
$sql = $sql . ", ";
}
//check of 15 is gevuld
if ($_FILES['uplimg15']['size'] > 0){
$sql = $sql . "foto15 = '".$_FILES['uplimg15']['name']."'";
}
$sql = $sql . " WHERE id = '".$id."'";
- Foto upload + naar DB schrijven - probleem
-
26-02-2011, 17:57 #1
- Berichten
- 348
- Lid sinds
- 16 Jaar
Foto upload + naar DB schrijven - probleem
-
-
26-02-2011, 18:11 #2
- Berichten
- 658
- Lid sinds
- 18 Jaar
Re: Foto upload + naar DB schrijven - probleem
Al die if-loopjes kunnen natuurlijk veel eenvoudiger:
PHP Code:$a = 0;
for($b = 1; $b <= 15; $b++){
if ($_FILES['uplimg'.$b]['size'] > 0){
if($a > 0){
$sql = $sql . ", ";
}
$a ++;
$sql = $sql . "foto".$b." = '".$_FILES['uplimg'.$b]['name']."'";
}
}
Ik snap je probleem alleen niet goed, het is toch juist de bedoeling dat er een komma tussen de 10e en elfde moet komen?
Hey,
Ik heb op een site een stuk code staan waarmee foto's geupload worden en naar een database geschreven kunnen worden.
Punt is dat de query niet goed (en heel omslachtig) wordt opgebouwd. Als er nu bijvoorbeeld al 10 foto's zijn geupload, en ik wil een 11e foto uploaden krijg ik een komma in de query, omdat die verkeerd wordt opgebouwd.
Kan iemand zeggen hoe dit gemakkelijker (en overzichtelijker) kan?
Alvast bedankt
Code:
PHP Code:$form_img .= '<label>Image 1: </label> <input type="file" name="uplimg1"><br />
<label>Image 2: </label> <input type="file" name="uplimg2"><br />
<label>Image 3: </label> <input type="file" name="uplimg3"><br />
<label>Image 4: </label> <input type="file" name="uplimg4"><br />
<label>Image 5: </label> <input type="file" name="uplimg5"><br />
<label>Image 6: </label> <input type="file" name="uplimg6"><br />
<label>Image 7: </label> <input type="file" name="uplimg7"><br />
<label>Image 8: </label> <input type="file" name="uplimg8"><br />
<label>Image 9: </label> <input type="file" name="uplimg9"><br />
<label>Image 10: </label> <input type="file" name="uplimg10"><br />
<label>Image 11: </label> <input type="file" name="uplimg11"><br />
<label>Image 12: </label> <input type="file" name="uplimg12"><br />
<label>Image 13: </label> <input type="file" name="uplimg13"><br />
<label>Image 14: </label> <input type="file" name="uplimg14"><br />
<label>Image 15: </label> <input type="file" name="uplimg15"><br />
';
$htmo .= '
<p>'.$feedback.'</p>
<form method="post" enctype="multipart/form-data">
'.$form_img.' <br />
<input type="submit" name="submitfoto" value="Upload Afbeeldingen" style="margin-left: 50px;" />
</form>
';
echo $htmo;
if($_SERVER['REQUEST_METHOD'] == 'POST') {
if(isset($_POST['submitfoto'])) {
$sql = "UPDATE aanbod SET ";
//check of 1 is gevuld
if ($_FILES['uplimg1']['size'] > 0){
$sql = $sql . "foto1 = '".$_FILES['uplimg1']['name']."'";
}
//komma zetten als 2 is gevuld
if ($_FILES['uplimg2']['size'] > 0){
$sql = $sql . ", ";
}
//check of 2 is gevuld
if ($_FILES['uplimg2']['size'] > 0){
$sql = $sql . "foto2 = '".$_FILES['uplimg2']['name']."'";
}
//komma zetten als 3 is gevuld
if ($_FILES['uplimg3']['size'] > 0){
$sql = $sql . ", ";
}
//check of 3 is gevuld
if ($_FILES['uplimg3']['size'] > 0){
$sql = $sql . "foto3 = '".$_FILES['uplimg3']['name']."'";
}
//komma zetten als 4 is gevuld
if ($_FILES['uplimg4']['size'] > 0){
$sql = $sql . ", ";
}
//check of 4 is gevuld
if ($_FILES['uplimg4']['size'] > 0){
$sql = $sql . "foto4 = '".$_FILES['uplimg4']['name']."'";
}
//komma zetten als 5 is gevuld
if ($_FILES['uplimg5']['size'] > 0){
$sql = $sql . ", ";
}
//check of 5 is gevuld
if ($_FILES['uplimg5']['size'] > 0){
$sql = $sql . "foto5 = '".$_FILES['uplimg5']['name']."'";
}
//komma zetten als 6 is gevuld
if ($_FILES['uplimg6']['size'] > 0){
$sql = $sql . ", ";
}
//check of 6 is gevuld
if ($_FILES['uplimg6']['size'] > 0){
$sql = $sql . "foto6 = '".$_FILES['uplimg6']['name']."'";
}
//komma zetten als 7 is gevuld
if ($_FILES['uplimg7']['size'] > 0){
$sql = $sql . ", ";
}
//check of 7 is gevuld
if ($_FILES['uplimg7']['size'] > 0){
$sql = $sql . "foto7 = '".$_FILES['uplimg7']['name']."'";
}
//komma zetten als 8 is gevuld
if ($_FILES['uplimg8']['size'] > 0){
$sql = $sql . ", ";
}
//check of 8 is gevuld
if ($_FILES['uplimg8']['size'] > 0){
$sql = $sql . "foto8 = '".$_FILES['uplimg8']['name']."'";
}
//komma zetten als 9 is gevuld
if ($_FILES['uplimg9']['size'] > 0){
$sql = $sql . ", ";
}
//check of 9 is gevuld
if ($_FILES['uplimg9']['size'] > 0){
$sql = $sql . "foto9 = '".$_FILES['uplimg9']['name']."'";
}
//komma zetten als 10 is gevuld
if ($_FILES['uplimg10']['size'] > 0){
$sql = $sql . ", ";
}
//check of 10 is gevuld
if ($_FILES['uplimg10']['size'] > 0){
$sql = $sql . "foto10 = '".$_FILES['uplimg10']['name']."'";
}
//komma zetten als 11 is gevuld
if ($_FILES['uplimg11']['size'] > 0){
$sql = $sql . ", ";
}
//check of 11 is gevuld
if ($_FILES['uplimg11']['size'] > 0){
$sql = $sql . "foto11 = '".$_FILES['uplimg11']['name']."'";
}
//komma zetten als 12 is gevuld
if ($_FILES['uplimg12']['size'] > 0){
$sql = $sql . ", ";
}
//check of 12 is gevuld
if ($_FILES['uplimg12']['size'] > 0){
$sql = $sql . "foto12 = '".$_FILES['uplimg12']['name']."'";
}
//komma zetten als 13 is gevuld
if ($_FILES['uplimg13']['size'] > 0){
$sql = $sql . ", ";
}
//check of 13 is gevuld
if ($_FILES['uplimg13']['size'] > 0){
$sql = $sql . "foto13 = '".$_FILES['uplimg13']['name']."'";
}
//komma zetten als 14 is gevuld
if ($_FILES['uplimg14']['size'] > 0){
$sql = $sql . ", ";
}
//check of 14 is gevuld
if ($_FILES['uplimg14']['size'] > 0){
$sql = $sql . "foto14 = '".$_FILES['uplimg14']['name']."'";
}
//komma zetten als 15 is gevuld
if ($_FILES['uplimg15']['size'] > 0){
$sql = $sql . ", ";
}
//check of 15 is gevuld
if ($_FILES['uplimg15']['size'] > 0){
$sql = $sql . "foto15 = '".$_FILES['uplimg15']['name']."'";
}
$sql = $sql . " WHERE id = '".$id."'";
Laatst aangepast door Lodewijk d.V. : 26-02-2011 om 18:13 Reden: Klein foutje met de quotes
-
26-02-2011, 18:51 #3
- Berichten
- 348
- Lid sinds
- 16 Jaar
Re: Foto upload + naar DB schrijven - probleem
EDIT:
Maar ik zie dat het met jouw code wel werkt :)
Super bedankt!Laatst aangepast door Ton van Alebeek : 26-02-2011 om 18:54 Reden: opgelost
Plaats een
- + Advertentie
- + Onderwerp
Marktplaats
Webmasterforum
- Websites algemeen
- Sitechecks
- Marketing
- Domeinen algemeen
- Waardebepaling
- CMS
- Wordpress
- Joomla
- Magento
- Google algemeen
- SEO
- Analytics
- Adsense
- Adwords
- HTML / XHTML
- CSS
- Programmeren
- PHP
- Javascript
- JQuery
- MySQL
- Ondernemen algemeen
- Belastingen
- Juridisch
- Grafisch ontwerp
- Hosting Algemeen
- Hardware Info
- Offtopic