Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How do I insert simultaneously insert multiple checkbox values and filenames using PHP into MySQL DB

Posted on 2009-05-16
7
Medium Priority
?
332 Views
Last Modified: 2013-12-12
Hi,

I am trying to insert an new row for each element in a checkbox array.  Multiple file uploads are submitted via the same form and these filenames are being written to the database at the same time.  

While I've been able to write both the filenames and the checkbox values to the DB  I'm still having issues with the loop itself and with the checkbox values being inserted more than once.

Hope the above makes sense!

<!-- FORM SAMPLE -->
 
<input type="checkbox" name="language[]" value="en" />EN
<input type="checkbox" name="language[]" value="de" />DE
<input type="checkbox" name="language[]" value="fr" />FR
 
<!-- The file upload fields only display after selecting the relevant checkbox -->
 
<input name="files[]" type="file" />File EN
<input name="files[]" type="file" />File DE
<input name="files[]" type="file" />File FR
 
 
 
<!-- PHP MYSQL -->
 
$checkboxArray=$HTTP_POST_VARS['language'];
$xml_upload_dir= 'xml/';
 
 
 if(isset($_FILES['files']['tmp_name'])){
        for($i=0; $i < count($_FILES['files']['tmp_name']);$i++)		
        {
 if(@copy($_FILES['files']['tmp_name'][$i],$xml_upload_dir.'/'.$_FILES['files']['name'][$i])) {
$fileName = $_FILES['files']['name'][$i];
foreach($checkboxArray as $language)
{
$query="INSERT INTO TABLENAME(entryID, language, fileName) VALUES ('$entryID', '$language', '$fileName')"; 
$result=mysql_query($query);
        }
    }
	
}

Open in new window

0
Comment
Question by:rasoodock
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 14

Expert Comment

by:racek
ID: 24404250

INSERT INTO TABLENAME(entryID, language, fileName) 
VALUES 
('$entryID', '$language', '$fileName'),
('$entryID', '$language', '$fileName'),
('$entryID', '$language', '$fileName')

Open in new window

0
 
LVL 14

Expert Comment

by:racek
ID: 24404254
you can have several times values in one insert ... without foreach...
0
 

Author Comment

by:rasoodock
ID: 24404287
Hi racek,

Thanks, but it will not necessarily be a maximum of three fields / checkboxes submitted.  Also re. the foreach, I will still need this to extract the values within the checkbox array.

The checkboxes / file upload fields are dynamically created.


0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 19

Expert Comment

by:NerdsOfTech
ID: 24409907
looks like you have 2 loops instead of one. So you are probably getting:

file1
file1
file1
file2
file2
file2
file3
file3
file3

Try just one loops and take the $i for the index of the checkbox array instead :)

Try and post errors if any.

<!-- FORM SAMPLE -->
 
<input type="checkbox" name="language[]" value="en" />EN
<input type="checkbox" name="language[]" value="de" />DE
<input type="checkbox" name="language[]" value="fr" />FR
 
<!-- The file upload fields only display after selecting the relevant checkbox -->
 
<input name="files[]" type="file" />File EN
<input name="files[]" type="file" />File DE
<input name="files[]" type="file" />File FR
 
 
 
<!-- PHP MYSQL -->
 
$checkboxArray=$HTTP_POST_VARS['language'];
$xml_upload_dir= 'xml/';
 
 
if(isset($_FILES['files']['tmp_name']))
{
 for($i=0; $i < count($_FILES['files']['tmp_name']);$i++)                
 {
  if(@copy($_FILES['files']['tmp_name'][$i],$xml_upload_dir.'/'.$_FILES['files']['name'][$i]))
  {
   $fileName = $_FILES['files']['name'][$i];
   $language = $checkboxArray[$i];
    $query="INSERT INTO TABLENAME(entryID, language, fileName) VALUES ('$entryID', '$language', '$fileName')"; 
    $result=mysql_query($query);
   }
  }
 }   
}

Open in new window

0
 
LVL 19

Accepted Solution

by:
NerdsOfTech earned 2000 total points
ID: 24409915
Oops one to many }.. USE THIS VERSION INSTEAD.
<!-- FORM SAMPLE -->
 
<input type="checkbox" name="language[]" value="en" />EN
<input type="checkbox" name="language[]" value="de" />DE
<input type="checkbox" name="language[]" value="fr" />FR
 
<!-- The file upload fields only display after selecting the relevant checkbox -->
 
<input name="files[]" type="file" />File EN
<input name="files[]" type="file" />File DE
<input name="files[]" type="file" />File FR
 
 
 
<!-- PHP MYSQL -->
 
$checkboxArray=$HTTP_POST_VARS['language'];
$xml_upload_dir= 'xml/';
 
 
if(isset($_FILES['files']['tmp_name']))
{
 for($i=0; $i < count($_FILES['files']['tmp_name']);$i++)                
 {
  if(@copy($_FILES['files']['tmp_name'][$i],$xml_upload_dir.'/'.$_FILES['files']['name'][$i]))
  {
   $fileName = $_FILES['files']['name'][$i];
   $language = $checkboxArray[$i];
   $query="INSERT INTO TABLENAME(entryID, language, fileName) VALUES ('$entryID', '$language', '$fileName')"; 
   $result=mysql_query($query);
  }
 }   
}

Open in new window

0
 

Author Comment

by:rasoodock
ID: 24411040
This works fine to an extent but still having problems with inserting the checkbox values which appear to behave very inconsistently.

For instance generally (though NOT always) if the first two are selected (e.g EN and DE) these values are inserted without a problem.

However if non-consecutive checkboxes are checked e.g  EN and FR  or if EN is omitted the values are not inserted.

Thanks for your help!

0
 

Author Comment

by:rasoodock
ID: 24415869
Apologies!  The checkbox inconsistency was due to a jquery hide/reveal function.  Other your solution works perfectly.  

Many thanks again.
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
In this article, I’ll talk about multi-threaded slave statistics printed in MySQL error log file.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question