Jump to content

Looping 2 foreach loops and using the results in one mysql query


PMacca_1987

Recommended Posts

Hi everyone,

 

I am having a problem with the below code.

The idea is that I use the foreach loops to output the data of the array which in turn is used in an INSERT statement, the first array is used to determine which column the data is inserted into and the other is used to provide the data to go into that column.

 

There is the same volume of data in each array being passed in, the pseudo for what I am trying to achieve is as follows:

 

loop 1 (columnarray)

loop 2 (dataarray)

 

INSERT INTO <tablename> (columnarray[1]) VALUES (dataarray[1])

INSERT INTO <tablename> (columnarray[2]) VALUES (dataarray[2])

 

and to end the loop when there are no fields remaining.

 

The code below currently inserts some data into the database but just inserts the data array data into the same field on different rows which is incorrect.

 

If you need any other info from my code please give me a shout, this has been frustrating me for a while now! and any help would be appreciated greatly!

 

Thanks in advance

 

 

 

<?php ob_start("ob_gzhandler"); session_start(); require "connect.php";?>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
   
	<?php
           
        $queryfield ="SELECT FieldName FROM ".$_SESSION['Username']."FieldNames";
        $resultfield = @mysql_query($queryfield, $connection)
        or die ("Unable to perform query<br>$queryfield");

        $row = mysql_fetch_row($resultfield);
        
        
                       foreach($row as $FldNames){

        foreach($_POST['FieldName'] as $key => $val) {
  
         $data = array('description' => $val['description']);
           
        $query = "INSERT INTO ".$_SESSION['Username']." (".$FldNames.") VALUES ('".$val."')";
        $result = @mysql_query($query, $connection)
                or die ("Unable to perform query<br>$query");
}
           
} 
?>

$name_array = an array of field names

$value_array = an array of values ( in your case $fldNames)

 

<?php

$name_array = array('field_1','field_2','field_3','field_4');
$value_array = array('value_1','value_2','value_3','value_4');

$sql = "INSERT INTO `table` (";
foreach($name_array as $name){
	$sql .= "`".$name."`, ";
}
$sql = substr($sql,0,-1).") VALUES (";
foreach($value_array as $value){
	$sql .= "`".$name."`, ";
}
$sql = substr($sql,0,-1).")";

?>

Well, for one, you shouldn't be using the back-ticks at all, but especially not in VALUES.

 

$name_array = array('field_1','field_2','field_3','field_4');
$fields = implode('`,`'$name);
if (!empty($fields)) $fields = sprintf('`%s`', $fields);

<?php

$name_array = array('field_1','field_2','field_3','field_4');
$value_array = array('value_1','value_2','value_3','value_4');

// He meant like this
$sql = "INSERT INTO `table` (`".implode("`,`",$name_array)."`) VALUES ('".implode("','",$value_array)."')";

?>

 

Backticks are part of the mysql standard? Albeit you shouldnt need them, but not that you shouldnt use them.

Backticks are part of the mysql standard? Albeit you shouldnt need them, but not that you shouldnt use them.

It's not really a standard per-say. Using them just allows you to use ridiculous table/column names like `h e l l 0` or `select` (which is a MySQL reserved keyword). Not recommended to name your tables like that.

Hello again,

Im still working through some of the resolutions here but I thought I would clear up the backticks conversation for you or at least tell you where they came from!

 

I completed the action that I required in php myadmin and then clicked the create PHP button,

 

This gave the code that was shown on the page  :shy:

 

Thats right guys.. I cheated!

 

Thanks for the help, really appreciate it.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.