1

I need to build INSERT statements from an array but I am stuck this is what I have so far but it obviously isn't right:

INSERT INTO company (, Name, Address, City, State, Zip, Country, Website, NOL
INSERT INTO additionallocations (, CompanyID, Address, City, State, Zip, Country

This is my array and how I am building the statement:

$fieldsArray = array(
                    'company' => array(
                            'Name' => $Name,
                            'Address' => $Address,
                            'City' => $City,
                            'State' => $State,
                            'Zip' => $Zip,
                            'Country' => $Country,
                            'Website' => $Website,
                            'NOL' => $NOL,
                        ),
                    'additionallocations' => array(
                            'CompanyID' => $CompanyID,
                            'Address' => $Address,
                            'City' => $City,
                            'State' => $State,
                            'Zip' => $Zip,
                            'Country' => $Country,
                        )
                    );




        foreach($fieldsArray as $table => $rows) {
            $sql = "INSERT INTO " . $table . " (";
            foreach($rows as $column => $value){
                $sql .= ", " .$column;
            }
        }

        echo $sql;

Here is what I would like to end up with:

INSERT INTO company (Name, Address, City, State, Zip, Country, Website, NOL) VALUES
                             ('$Name', '$Address', '$City', '$State', '$Zip', '$Country', '$Website', '$NOL');

I need to find a way to keep building this out with the VALUES and get rid of the first comma in after the opening parentheses. Anyone have any suggestions?

2
  • You said you needed this with the values, how will your values be passed? Commented Feb 9, 2014 at 4:35
  • What do you mean? I would like to use the variable names in the VALUES section like this INSERT INTO company (Name, Address, City, State, Zip, Country, Website, NOL) VALUES ('$Name', '$Address', '$City', '$State', '$Zip', '$Country', '$Website', '$NOL'); Commented Feb 9, 2014 at 4:37

2 Answers 2

2
foreach($fieldsArray as $table => $rows) 
{
    $sql = "INSERT INTO " . $table . "(";
    foreach($rows as $column => $value)
    {    
         $sql .= $column.", ";
    }
    $sql = rtrim($sql, ", ");
    $sql .= ") VALUES (";

    foreach($rows as $column => $value)
    {    
         $sql .= "'".$value."', ";
    }
    $sql = rtrim($sql, ", ");
    $sql .= ")";
    echo $sql; 
}
Sign up to request clarification or add additional context in comments.

1 Comment

rtrim($sql, ", "); should be $sql = rtrim($sql, ", ");
0

I got it working with this:

foreach($fieldsArray as $key => $table){            
    $keys = array_keys($table);
        $values = null;
        $x = 1;

        foreach($keys as $value) {
            $values .= "?";
            if($x < count($keys)) {
                $values .= ', ';
            }
            $x++;
        }

    echo $sql = "INSERT INTO {$key} (`" . implode('`, `', $keys) . "`) VALUES ({$values})" . "<br/>";

}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.