smonkcaptain Posted June 28, 2010 Share Posted June 28, 2010 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 https://forums.phpfreaks.com/topic/206013-checkbox-upload-to-mysql-database/ Share on other sites More sharing options...
ldb358 Posted June 28, 2010 Share Posted June 28, 2010 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 https://forums.phpfreaks.com/topic/206013-checkbox-upload-to-mysql-database/#findComment-1078001 Share on other sites More sharing options...
smonkcaptain Posted June 28, 2010 Author Share Posted June 28, 2010 Thankyou for the reply! I'm not great at PHP, therefore could i ask what you mean by 'inset the $tag variable in your database as text' Do you mean make a field for each variable (accident, airtoair...etc) in the MYSQL database? Link to comment https://forums.phpfreaks.com/topic/206013-checkbox-upload-to-mysql-database/#findComment-1078004 Share on other sites More sharing options...
ldb358 Posted June 28, 2010 Share Posted June 28, 2010 no you should just create one column and store the $tag variable in it and then you'll have a list of tags that you can later search Link to comment https://forums.phpfreaks.com/topic/206013-checkbox-upload-to-mysql-database/#findComment-1078018 Share on other sites More sharing options...
smonkcaptain Posted June 28, 2010 Author Share Posted June 28, 2010 Hey, I'm getting this error on Submit from the upload form: 'Parse error: syntax error, unexpected T_ARRAY, expecting T_STRING or T_VARIABLE or '$' in E:\domains\j\jimmyleaman.net\user\htdocs\upload.php on line 28' Link to comment https://forums.phpfreaks.com/topic/206013-checkbox-upload-to-mysql-database/#findComment-1078463 Share on other sites More sharing options...
ldb358 Posted June 29, 2010 Share Posted June 29, 2010 okay post the code and ill take a look Link to comment https://forums.phpfreaks.com/topic/206013-checkbox-upload-to-mysql-database/#findComment-1078532 Share on other sites More sharing options...
smonkcaptain Posted June 29, 2010 Author Share Posted June 29, 2010 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 https://forums.phpfreaks.com/topic/206013-checkbox-upload-to-mysql-database/#findComment-1078781 Share on other sites More sharing options...
kickstart Posted June 29, 2010 Share Posted June 29, 2010 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 https://forums.phpfreaks.com/topic/206013-checkbox-upload-to-mysql-database/#findComment-1078785 Share on other sites More sharing options...
smonkcaptain Posted June 29, 2010 Author Share Posted June 29, 2010 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 https://forums.phpfreaks.com/topic/206013-checkbox-upload-to-mysql-database/#findComment-1078796 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.