Solved

Inserting a single DB line within a foreach statement

Posted on 2009-04-08
2
181 Views
Last Modified: 2012-05-06
Hi Experts,
I have a form where members can upload a file, I have created a parser to grab the information from the file perfectly. My problem is, my data is currently being displayed via a foreach statement (code in below snippet).

Rather than echoing the data to the screen, I want to save the data in my database as a single line only to save from having to process the file each time. If I put an insert within the foreach statement its obviously going to create more than 1 DB insert.

The values being displayed on the page here is what i need to catch in a single statement:-
echo 'player with no team: <br />';
echo 'player in team '.$k.'<br />';
echo 'Player Name:'.$player['name'].'; IP: '.$player['ip'].'; Army: '.$n->getArmyName($player['army']).'

Can anyone help?
Thanks in advance
$n = new $classname();

    if (!$n->parse($_FILES['some_identifier']['tmp_name'])) {

        die('can not read it somehow');

    }

    $teams = $n->getTeams();

    foreach($teams as $k => $team) {

        if ($k == 0) { // team 0 store the player without team

            echo 'player with no team: <br />';

        } else {

            echo 'player in team '.$k.'<br />';

        }

        foreach ($team as $k_p => $player) {

            echo 'Player Name:'.$player['name'].'; IP: '.$player['ip'].'; Army: '.$n->getArmyName($player['army']).'<br />';

        }

Open in new window

0
Comment
Question by:EzEApostle
  • 2
2 Comments
 
LVL 3

Expert Comment

by:QualitySoftwareDevelopment
ID: 24095076
You can make it as this (if I get the meaning right ;-))


$sql="INSERT INTO table_name (field1, field2, field3) VALUES (";
 

//make your first datacollect

$sql.=$datacollect.", ";
 

//make your second datacollect

$sql.=$datacollect.", ";
 

//make your third datacollect

$sql.=$datacollect."); ";

Open in new window

0
 
LVL 3

Accepted Solution

by:
QualitySoftwareDevelopment earned 500 total points
ID: 24095087
and if you need more inserts in one line you can do like this:

$sql = "INSERT INTO table_name (field1, filed2, field3)
  VALUES
  ($data1, $data2, $data3),
  ($data1, $data2, $data3),
  ($data1, $data2, $data3),
  ($data1, $data2, $data3)";

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
calculated column 12 74
Calculate values in an array 18 33
TCPDF Table of Contents - Page Numbers Don't Right Align 2 36
nagios 4 php error after installation 6 76
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now