Jump to content

Checkbox upload to MYSQL database


smonkcaptain

Recommended Posts

Hey all,

 

Attached is a picture of the checkbox section of my 'Upload Form'. I'm wanting users to be able to check multiple checkboxes (if needed) to categorise there uploaded photograph.

 

I'm wanting this information to be uploaded to a MYSQL database which is already setup. The selected category/ies need to be somehow uploaded into a 'category' field on the MYSQL database so that it can be searched at a later date.

 

Below is some code of the checkboxes on the form:

 

<input name="accident" type="checkbox" value="accident" /><font style="font-size:10px">  Accident -</font><font style="font-size:9px; color:#000; font-family:Arial, Helvetica, sans-serif"> [incident-related photographs]</font>
        </div>
        
        <div id="checkbox">
       <input name="airport" type="checkbox" value="airport"/><font style="font-size:10px">  Airport -</font><font style="font-size:9px; color:#000; font-family:Arial, Helvetica, sans-serif"> [Terminal, tower, apron...etc]</font>
        </div>
        
       <div id="checkbox">
       <input name="category" type="checkbox" value="airtoair"/><font style="font-size:10px">  Air to Air -</font><font style="font-size:9px; color:#000; font-family:Arial, Helvetica, sans-serif"> [Photograph from one of another]</font>
       </div>

 

 

and below is the current PHP code i'm using to upload the rest of the data to the MYSQL database:

 

<?php 

//This is the directory where images will be saved 
$target = "upload/"; 
$target = $target .basename( $_FILES['photo']['name']); 

//This gets all the other information from the form 
$genre=$_POST['genre'];
$aircrafttype=$_POST['aircrafttype']; 
$airline=$_POST['airline']; 
$airport=$_POST['airport']; 
$registration=$_POST['registration'];
$connumber=$_POST['connumber'];
$category=$_POST['category'];
$day=$_POST['day'];
$month=$_POST['month'];
$year=$_POST['year'];
$remarks=$_POST['remarks']; 
$photo=($_FILES['photo']);

$date=$day.'-'.$month.'-'.$year;

// Connects to your Database 
mysql_connect("77.68.105.107", "jimmyleama2", "a340a340") or die(mysql_error()) ; 
mysql_select_db("jimmyleama2") or die(mysql_error()) ; 

//Writes the information to the database 
mysql_query("INSERT INTO `photographs` (`Genre`,`Aircraft Type`, `Airline`, `Airport/Airfield`, `Registration`,`Construction #`, `Category`, `Date`, `photo`, `Remarks`) VALUES ('$genre','$aircrafttype', '$airline', '$airport', '$registration', '$connumber', '$category', '$date', '$photo', '$remarks')")or die(mysql_error()); 

//Writes the photo to the server 
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
{ 

//Tells you if its all ok 
echo "Congratulations, your upload has been successful. ". basename( $_FILES['photo']['name'])." has been uploaded, and your information has been added to the database.";
echo "</br> <img src=\"upload/". basename( $_FILES['photo']['name']). "\"/>"; 
} 
else { 

//Gives and error if its not 
echo "Sorry, there was a problem uploading your file."; 
} 
?> 

 

 

I'd appriciate any replies, and if any further information is needed, just ask :)

 

Thanks, Jimmy.

Link to comment
Share on other sites

change the name of them to:

<input name="tag[]" type="checkbox" value="accident" />
<input name="tag[]" type="checkbox" value="airport"/>
<input name="tag[]" type="checkbox" value="airtoair"/>

then you can loop through it like so:

 

$tags = new Array();
foreach($_POST['tag'] as $tag){
   if($tag){
       tags[] = $tag;
   } 
}
$tag = implode(',',$tags);

 

then inset the $tag variable in your database as text

Link to comment
Share on other sites

Here is the HTML code, inside the form:

 

<label for="category" style="margin-top:-140px">Categories:</label><hr /><font style="font-size:9px; font-family:Arial, Helvetica, sans-serif; color:#000; font-style:italic">Photo-Related</font>
        <label style="margin-top:142px"><br /><hr /></label><font style="font-size:9px; font-family:Arial, Helvetica, sans-serif; color:#000; font-style:italic">Aircraft-Related</font>
        </td>
        <td width="300" style="padding-top:15px">
        <div id="down">
        <div id="checkbox">
       <input name="tag[]" type="checkbox" value="accident" /><font style="font-size:10px">  Accident -</font><font style="font-size:9px; color:#000; font-family:Arial, Helvetica, sans-serif"> [incident-related photographs]</font>
        </div>
        
        <div id="checkbox">
       <input name="tag[]" type="checkbox" value="airport"/><font style="font-size:10px">  Airport -</font><font style="font-size:9px; color:#000; font-family:Arial, Helvetica, sans-serif"> [Terminal, tower, apron...etc]</font>
        </div>
        
       <div id="checkbox">
       <input name="tag[]" type="checkbox" value="airtoair"/><font style="font-size:10px">  Air to Air -</font><font style="font-size:9px; color:#000; font-family:Arial, Helvetica, sans-serif"> [Photograph from one of another]</font>
       </div>
       
       <div id="checkbox">
       <input name="tag[]" type="checkbox" value="aircraftcabin"/><font style="font-size:10px">  Aircraft Cabin -</font><font style="font-size:9px; color:#000; font-family:Arial, Helvetica, sans-serif"> [interior cabin photographs]</font>
       </div>
       
       <div id="checkbox">
       <input name="tag[]" type="checkbox" value="flightdeck"/><font style="font-size:10px">  Flight Deck -</font><font style="font-size:9px; color:#000; font-family:Arial, Helvetica, sans-serif"> [Aircraft cockpit photographs]</font>
       </div>

 

 

 

and the Upload.php script:

 

<?php 

//This is the directory where images will be saved 
$target = "upload/"; 
$target = $target .basename( $_FILES['photo']['name']); 

//This gets all the other information from the form 
$genre=$_POST['genre'];
$aircrafttype=$_POST['aircrafttype']; 
$airline=$_POST['airline']; 
$airport=$_POST['airport']; 
$country=$_POST['country'];
$registration=$_POST['registration'];
$connumber=$_POST['connumber'];
$day=$_POST['day'];
$month=$_POST['month'];
$year=$_POST['year'];
$remarks=$_POST['remarks']; 
$photo=($_FILES['photo']);

$date=$day.'-'.$month.'-'.$year;

$tags = new Array();
foreach($_POST['tag'] as $tag){
   if($tag){
       tags[] = $tag;
   }
}
$tag = implode(',',$tags);


// Connects to your Database 
mysql_connect("77.68.105.107", "jimmyleama2", "a340a340") or die(mysql_error()) ; 
mysql_select_db("jimmyleama2") or die(mysql_error()) ; 

//Writes the information to the database 
mysql_query("INSERT INTO `photographs` (`Genre`,`Aircraft Type`, `Airline`, `Country`, `Airport/Airfield`, `Registration`,`Construction #`, `tag`, `Date`, `photo`, `Remarks`) VALUES ('$genre','$aircrafttype', '$airline', '$country', '$airport', '$registration', '$connumber', '$tag', '$date', '$photo', '$remarks')")or die(mysql_error()); 

//Writes the photo to the server 
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{ 

//Tells you if its all ok 
echo "Congratulations, your upload has been successful. ". basename( $_FILES['photo']['name'])." has been uploaded, and your information has been added to the database."; 
} 
else { 

//Gives and error if its not 
echo "Sorry, there was a problem uploading your file."; 
} 
?> 

Link to comment
Share on other sites

Hi

 

Idea is that you seperate the categories onto a seperate table. So you would have a table of photographs and another table with 1 or more rows per photograph, 1 for each category for that photograph.

 

This also means that you can add an extra category very easily with no need for any table changes.

 

You don't appear to have saved the fields that you have tried to process as "tag".

 

Think the main cause for the error you have is a missing $

 

foreach($_POST['tag'] as $tag){
   if($tag){
       [color=red]$[/color]tags[] = $tag;
   }
}

 

All the best

 

Keith

Link to comment
Share on other sites

Hi

 

Idea is that you seperate the categories onto a seperate table. So you would have a table of photographs and another table with 1 or more rows per photograph, 1 for each category for that photograph.

 

This also means that you can add an extra category very easily with no need for any table changes.

 

You don't appear to have saved the fields that you have tried to process as "tag".

 

Think the main cause for the error you have is a missing $

 

foreach($_POST['tag'] as $tag){
   if($tag){
       [color=red]$[/color]tags[] = $tag;
   }
}

 

All the best

 

Keith

 

Just the correction of the PHP code given had made it work!

 

Thankyou all very much for your help!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.