Skip to main content
Inspiring
February 28, 2007
Question

OT: A PHP Punt! Inserting arrays into data tables....

  • February 28, 2007
  • 17 replies
  • 925 views
OK - I've beat my head to a pulp.

I have an array of paired strings. I want to add the whole array to a data
table, in alternating fields.

I have this -

mysql_select_db($database_cmaConn, $cmaConn);

$fields = "'keyJargon','keyDef'";
$values = "'".implode(array_values($toAdd), "','")."'";

function mysql_insert($table,$ToAdd) {
$q = 'INSERT INTO `'.$table.'` ('.$fields.') VALUES ('.$values.')';
$res = mysql_query($q)OR die(mysql_error());

return true;

} //end function

if (isset($_POST['Submit'])) {

$jargonArray=explode("\n",$_POST['upload']);
// this splits the input data into consecutive elements of the array, one
for each new line.

$i=0;
foreach($jargonArray as $key => $value) {
if (strlen($value) > 0) {
$jargon[] = explode("\x09",$value);
//this splits the tab-delimited input data into adjacent array elements.

mysql_insert('cma_keywords',$jargon[$i][0][1]);
$i++;
} // endif

}

But it's broke. What am I doing wrong?

--
Murray --- ICQ 71997575
Adobe Community Expert
(If you *MUST* email me, don't LAUGH when you do so!)
==================
http://www.dreamweavermx-templates.com - Template Triage!
http://www.projectseven.com/go - DW FAQs, Tutorials & Resources
http://www.dwfaq.com - DW FAQs, Tutorials & Resources
http://www.macromedia.com/support/search/ - Macromedia (MM) Technotes
==================



This topic is closed to new replies. Start a new post to keep the conversation going.

17 replies

Inspiring
March 1, 2007
Maybe I should have said "Gelcaps problems". 8)

There were quote problems on the field values, which I fixed in the code I
posted.

--
Murray --- ICQ 71997575
Adobe Community Expert
(If you *MUST* email me, don't LAUGH when you do so!)
==================
http://www.dreamweavermx-templates.com - Template Triage!
http://www.projectseven.com/go - DW FAQs, Tutorials & Resources
http://www.dwfaq.com - DW FAQs, Tutorials & Resources
http://www.macromedia.com/support/search/ - Macromedia (MM) Technotes
==================


"Joe Makowiec" <makowiec@invalid.invalid> wrote in message
news:Xns98E656BB584C6makowiecatnycapdotrE@216.104.212.96...
> On 01 Mar 2007 in macromedia.dreamweaver.appdev, Murray *ACE* wrote:
>
>> Yes - that works with slight modification -
>>
>> $q = 'INSERT INTO cma_keywords (keyJargon,keyDef) VALUES ('."'";
>> $q .= trim($jargon[0]);
>> $q .= "','";
>> $q .= trim($jargon[1]);
>> $q .= "');";
>
> Glad it worked. I knew I was tired when I wrote the post last night, and
> got sloppy.
>
>> Encaps problems....
>
> Huh?
>
> --
> Joe Makowiec
> http://makowiec.net/
> Email: http://makowiec.net/email.php


Inspiring
March 1, 2007
On 01 Mar 2007 in macromedia.dreamweaver.appdev, Murray *ACE* wrote:

> Yes - that works with slight modification -
>
> $q = 'INSERT INTO cma_keywords (keyJargon,keyDef) VALUES ('."'";
> $q .= trim($jargon[0]);
> $q .= "','";
> $q .= trim($jargon[1]);
> $q .= "');";

Glad it worked. I knew I was tired when I wrote the post last night, and
got sloppy.

> Encaps problems....

Huh?

--
Joe Makowiec
http://makowiec.net/
Email: http://makowiec.net/email.php
Inspiring
March 1, 2007
Yes - that works with slight modification -

$q = 'INSERT INTO cma_keywords (keyJargon,keyDef) VALUES ('."'";
$q .= trim($jargon[0]);
$q .= "','";
$q .= trim($jargon[1]);
$q .= "');";

Encaps problems....

--
Murray --- ICQ 71997575
Adobe Community Expert
(If you *MUST* email me, don't LAUGH when you do so!)
==================
http://www.dreamweavermx-templates.com - Template Triage!
http://www.projectseven.com/go - DW FAQs, Tutorials & Resources
http://www.dwfaq.com - DW FAQs, Tutorials & Resources
http://www.macromedia.com/support/search/ - Macromedia (MM) Technotes
==================


"Joe Makowiec" <makowiec@invalid.invalid> wrote in message
news:Xns98E65158497AEmakowiecatnycapdotrE@216.104.212.96...
> On 01 Mar 2007 in macromedia.dreamweaver.appdev, Murray *ACE* wrote:
>
>> So, if there were 25 pairs of word/definitions, I would just create
>> a $q that listed each explicitly?
>>
>> Wouldn't it make more sense to have the insert in the loop?
>
> It is inside the loop. See if this is clearer:
>
> mysql_select_db($database_cmaConn, $cmaConn);
>
> if (isset($_POST['Submit'])) {
> $jargonArray=explode("\n",$_POST['upload']);
> // this splits the input data into consecutive elements of the array,
> one for each new line.
>
> foreach($jargonArray as $key => $value) {
> if (strlen($value) > 0) {
> $jargon = explode("\x09",$value);
> //this splits the tab-delimited input data into adjacent array elements.
> // Create the query string
> // Should wind up looking like
> // INSERT INTO `cma_keywords` (keyJargon,keyDef) VALUES (val1,val2);
> $q = 'INSERT INTO `cma_keywords` (keyJargon,keyDef) VALUES (';
> $q .= $jargon[0];
> $q .= ',';
> $q .= $jargon[1];
> $q .= ');';
> // Do the query
> $res = mysql_query($q) OR die(mysql_error());
> // Do something with $res here - check if insert went OK
> } // endif strlen
> } // end foreach
> } // endif isset
>
>
>
> --
> Joe Makowiec
> http://makowiec.net/
> Email: http://makowiec.net/email.php


Inspiring
March 1, 2007
Ahh - I see. Lemme try it!

--
Murray --- ICQ 71997575
Adobe Community Expert
(If you *MUST* email me, don't LAUGH when you do so!)
==================
http://www.dreamweavermx-templates.com - Template Triage!
http://www.projectseven.com/go - DW FAQs, Tutorials & Resources
http://www.dwfaq.com - DW FAQs, Tutorials & Resources
http://www.macromedia.com/support/search/ - Macromedia (MM) Technotes
==================


"Joe Makowiec" <makowiec@invalid.invalid> wrote in message
news:Xns98E65158497AEmakowiecatnycapdotrE@216.104.212.96...
> On 01 Mar 2007 in macromedia.dreamweaver.appdev, Murray *ACE* wrote:
>
>> So, if there were 25 pairs of word/definitions, I would just create
>> a $q that listed each explicitly?
>>
>> Wouldn't it make more sense to have the insert in the loop?
>
> It is inside the loop. See if this is clearer:
>
> mysql_select_db($database_cmaConn, $cmaConn);
>
> if (isset($_POST['Submit'])) {
> $jargonArray=explode("\n",$_POST['upload']);
> // this splits the input data into consecutive elements of the array,
> one for each new line.
>
> foreach($jargonArray as $key => $value) {
> if (strlen($value) > 0) {
> $jargon = explode("\x09",$value);
> //this splits the tab-delimited input data into adjacent array elements.
> // Create the query string
> // Should wind up looking like
> // INSERT INTO `cma_keywords` (keyJargon,keyDef) VALUES (val1,val2);
> $q = 'INSERT INTO `cma_keywords` (keyJargon,keyDef) VALUES (';
> $q .= $jargon[0];
> $q .= ',';
> $q .= $jargon[1];
> $q .= ');';
> // Do the query
> $res = mysql_query($q) OR die(mysql_error());
> // Do something with $res here - check if insert went OK
> } // endif strlen
> } // end foreach
> } // endif isset
>
>
>
> --
> Joe Makowiec
> http://makowiec.net/
> Email: http://makowiec.net/email.php


Inspiring
March 1, 2007
On 01 Mar 2007 in macromedia.dreamweaver.appdev, Murray *ACE* wrote:

> So, if there were 25 pairs of word/definitions, I would just create
> a $q that listed each explicitly?
>
> Wouldn't it make more sense to have the insert in the loop?

It is inside the loop. See if this is clearer:

mysql_select_db($database_cmaConn, $cmaConn);

if (isset($_POST['Submit'])) {
$jargonArray=explode("\n",$_POST['upload']);
// this splits the input data into consecutive elements of the array, one for each new line.

foreach($jargonArray as $key => $value) {
if (strlen($value) > 0) {
$jargon = explode("\x09",$value);
//this splits the tab-delimited input data into adjacent array elements.
// Create the query string
// Should wind up looking like
// INSERT INTO `cma_keywords` (keyJargon,keyDef) VALUES (val1,val2);
$q = 'INSERT INTO `cma_keywords` (keyJargon,keyDef) VALUES (';
$q .= $jargon[0];
$q .= ',';
$q .= $jargon[1];
$q .= ');';
// Do the query
$res = mysql_query($q) OR die(mysql_error());
// Do something with $res here - check if insert went OK
} // endif strlen
} // end foreach
} // endif isset



--
Joe Makowiec
http://makowiec.net/
Email: http://makowiec.net/email.php
Inspiring
March 1, 2007
So, if there were 25 pairs of word/definitions, I would just create a $q
that listed each explicitly?

Wouldn't it make more sense to have the insert in the loop?

--
Murray --- ICQ 71997575
Adobe Community Expert
(If you *MUST* email me, don't LAUGH when you do so!)
==================
http://www.dreamweavermx-templates.com - Template Triage!
http://www.projectseven.com/go - DW FAQs, Tutorials & Resources
http://www.dwfaq.com - DW FAQs, Tutorials & Resources
http://www.macromedia.com/support/search/ - Macromedia (MM) Technotes
==================


"Joe Makowiec" <makowiec@invalid.invalid> wrote in message
news:Xns98E5D9D4A93E3makowiecatnycapdotrE@216.104.212.96...
> On 28 Feb 2007 in macromedia.dreamweaver.appdev, Murray *ACE* wrote:
>
>> OK - I've beat my head to a pulp.
>>
>> I have an array of paired strings. I want to add the whole array to
>> a data table, in alternating fields.
>>
>> I have this -
>>
>> mysql_select_db($database_cmaConn, $cmaConn);
>>
>> $fields = "'keyJargon','keyDef'";
>> $values = "'".implode(array_values($toAdd), "','")."'";
>>
>> function mysql_insert($table,$ToAdd) {
>> $q = 'INSERT INTO `'.$table.'` ('.$fields.') VALUES
>> ('.$values.')'; $res = mysql_query($q)OR die(mysql_error());
>>
>> return true;
>>
>> } //end function
>>
>> if (isset($_POST['Submit'])) {
>>
>> $jargonArray=explode("\n",$_POST['upload']);
>> // this splits the input data into consecutive elements of the
>> array, one for each new line.
>>
>> $i=0;
>> foreach($jargonArray as $key => $value) {
>> if (strlen($value) > 0) {
>> $jargon[] = explode("\x09",$value);
>> //this splits the tab-delimited input data into adjacent array
>> elements.
>>
>> mysql_insert('cma_keywords',$jargon[$i][0][1]);
>> $i++;
>> } // endif
>>
>> }
>>
>> But it's broke. What am I doing wrong?
>
> Don't have a boatload of time at the moment, and I know what grief we
> went through yesterday getting stuff /into/ that jargon array, but...
> You don't need it.
>
> Try this:
>
> mysql_select_db($database_cmaConn, $cmaConn);
>
> foreach($jargonArray as $key => $value) {
> if (strlen($value) > 0) {
> $jargon = explode("\x09",$value);
> //this splits the tab-delimited input data into adjacent array elements.
> $q = 'INSERT INTO `cma_keywords` (keyJargon,keyDef) VALUES (';
> $q .= $jargon[0];
> $q .= ',';
> $q .= $jargon[1];
> $q .= ');';
> $res = mysql_query($q) OR die(mysql_error());
> // Do something with $res here - check if insert went OK
> } // endif
> }// end foreach
>
> Untested. Yeah, I know - it's case specific. I'm tired.
>
> --
> Joe Makowiec
> http://makowiec.net/
> Email: http://makowiec.net/email.php


Inspiring
March 1, 2007
On 28 Feb 2007 in macromedia.dreamweaver.appdev, Murray *ACE* wrote:

> OK - I've beat my head to a pulp.
>
> I have an array of paired strings. I want to add the whole array to
> a data table, in alternating fields.
>
> I have this -
>
> mysql_select_db($database_cmaConn, $cmaConn);
>
> $fields = "'keyJargon','keyDef'";
> $values = "'".implode(array_values($toAdd), "','")."'";
>
> function mysql_insert($table,$ToAdd) {
> $q = 'INSERT INTO `'.$table.'` ('.$fields.') VALUES
> ('.$values.')'; $res = mysql_query($q)OR die(mysql_error());
>
> return true;
>
> } //end function
>
> if (isset($_POST['Submit'])) {
>
> $jargonArray=explode("\n",$_POST['upload']);
> // this splits the input data into consecutive elements of the
> array, one for each new line.
>
> $i=0;
> foreach($jargonArray as $key => $value) {
> if (strlen($value) > 0) {
> $jargon[] = explode("\x09",$value);
> //this splits the tab-delimited input data into adjacent array
> elements.
>
> mysql_insert('cma_keywords',$jargon[$i][0][1]);
> $i++;
> } // endif
>
> }
>
> But it's broke. What am I doing wrong?

Don't have a boatload of time at the moment, and I know what grief we
went through yesterday getting stuff /into/ that jargon array, but...
You don't need it.

Try this:

mysql_select_db($database_cmaConn, $cmaConn);

foreach($jargonArray as $key => $value) {
if (strlen($value) > 0) {
$jargon = explode("\x09",$value);
//this splits the tab-delimited input data into adjacent array elements.
$q = 'INSERT INTO `cma_keywords` (keyJargon,keyDef) VALUES (';
$q .= $jargon[0];
$q .= ',';
$q .= $jargon[1];
$q .= ');';
$res = mysql_query($q) OR die(mysql_error());
// Do something with $res here - check if insert went OK
} // endif
}// end foreach

Untested. Yeah, I know - it's case specific. I'm tired.

--
Joe Makowiec
http://makowiec.net/
Email: http://makowiec.net/email.php