Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8080443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:24:56+00:00 2026-06-05T16:24:56+00:00

hi friends i’m creating a php page to import the data from a csv

  • 0

hi friends i’m creating a php page to import the data from a csv file into sql database..

here database table and number of fields are specified by user itself..

if user specifies 4 fields, then it is created using a for loop as follows..

<?php
include('connect.php');
$name = $_POST['table_name'];
//echo $name;

$create_tab = "CREATE TABLE $name(id varchar(15) PRIMARY KEY)";




if(mysql_query($create_tab,$con))
            {
                echo "Table <b><i>$name</i></b> created successfully... <br/> <br/>Add columns...";
            }
            else {
                die('Error1'.mysql_error());
            }



$field = $_POST['number_of_fields'];
//echo $name.$field;
echo '<form id="form1" name="form1" method="post" action="update-table.php">';
echo "<p>
          <label for='tablename'></label>
          <input type='hidden' name='tablename' id='tablename' value='$name' size='5'/>
        </p>";

echo "<p>
          <label for='fields'></label>
          <input type='hidden' name='fields' id='fields' value='$field' size='5'/>
        </p>";
echo '<table border="1" cellpadding="5" cellspacing="5">';
for ( $i = 1; $i <= $field; $i ++) {
    echo '<tr>';
    echo '<td>';
    echo "<p>
          $i
        </p>";
        echo'</td>';

    echo '<td>';
    echo "<p>
          <label for='textfield$i'></label>
          <input type='text' name='field$i' id='textfield$i' />
        </p>";
        echo'</td>';
            echo '<td>';
            echo "
            <select name='select$i' id='select$i'>
                    <option value='varchar(200)'>varchar</option>
                    <option value='int'>int</option>
                    <option value='float'>float</option>
                                        <option value='date'>date</option>
                  </select>";
                echo '</td>';
                                echo '</tr>';


    }
            echo '</table>';
?>
        <p>File Location :
          <input type="text" name="fileField" id="fileField" />
      </p>
<br/>
<INPUT type="image" name="search" src="images/alter.gif" border="0" height="75" width=120">

</form>

then table create and alter as follows..

 <?php
    include('connect.php');
$field = $_POST[fields]; 
$name = $_POST[tablename];    

for ( $i = 1; $i <= $field; $i++) {
//getting field names
  $varname = ($txtfield . $i);
    $$varname = $_POST[field.$i]; 
  // echo $$varname;
   $fi = $$varname;

//getting field types
  $selname = ($selfield . $i);
    $$selname = $_POST[select.$i]; 
     $dt = $$varname;

$sql = "ALTER TABLE $name ADD $fi $dt";

if(mysql_query($sql,$con))
            {
                echo "Field <b><i>$fi</i></b> added successfully...<br/>";
            }
            else {
                die('Error1'.mysql_error());
            }

    }


?>

as above database table and fields are crated…

i got the concept of inserting data using static variables as follows..

<?php
// data import
include('connect.php');


$field = $_POST['fileField']; //file directory

echo "<br/><br/>Import file path: ";
echo $field;

 $file = $field; 

$lines = file($file);
$firstLine = $lines[0];

foreach ($lines as $line_num => $line) {
    if($line_num==0) { continue; } //escape the header column
    $arr = explode(",",$line);
    $column1= $arr[0];
    $column2= $arr[1];
// ' escape character
    if (strpos($column2, "'") == FALSE)
{
    $column21 = $column2;
}
else{
$column21 = str_replace ("'", "\'", $column2);
}
        $column3= $arr[2];
            $column4= $arr[3];

//print data from csv
echo "<table border='1' width='800' cellpadding='5' cellspacing='2'>";
echo "<tr>";
echo "<td width='8'>";
echo $column1;
echo "</td>";
echo "<td width='100'>";
echo $column21;
echo "</td>";
echo "<td width='5'>";
echo $column3;
echo "</td>";
echo "<td width='5'>";
echo $column4;
echo "</td>";
echo "</tr>";
echo "</table>";

     $import="INSERT into $name (id,name,year1,year2) values(

     '$column1','$column21','$column3','$column4')";
     mysql_query($import) or die(mysql_error());

}

?>

now, my question is how can i make this insert statement dynamic as such it creates field names and values dynamically inside insert query from the data obtained from for loop in table create and alter query???

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T16:24:57+00:00Added an answer on June 5, 2026 at 4:24 pm

    If I understand your question correctly, it sounds like you want to combine your inserts into one query (which is very smart performance wise). SQL allows you to insert multiple rows at once like so:

    INSERT INTO table (id, first, last) VALUES(NULL, 'Ryan', 'Silvers'),(NULL, 'Oscar', 'Smith'),(NULL, 'Jessica', 'Owens')
    

    So by using creating an array of VALUES and using implode to join them you can make one query:

    //Create rows
    foreach($rows as $row) {
        $queryRows[] = "(NULL, '".$row['first']."', '".$row['last']."')";
    }
    
    //Create query
    $query = 'INSERT INTO table (id, first, last) VALUES'.implode(',', $queryRows);
    

    Now that I understand your real question, I can give you a basic overview of what you need to do to achieve this. You need to create a form that allows a user to enter data that will be inserted into the table.

    <form method="post" action="process.php">
        <input name="field1" />
        <!-- and more -->
    </form>
    

    Then you need to write a PHP script to process the form submission and insert the data into MySQL:

    <?php
        //Check form submission and validate entries, then...
    
        $stmt = $pdo->prepare('INSERT INTO table (field1) VALUES(:field1)');
        $stmt->execute(array(':field1', $_POST['field1']));
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Friends, onCreate() I am loading data from webservice using Async thread. when thread is
Friends, I'm lost here. I have this WCF Rest service returning data in json
Friends my php 5.2 codes am keeping my password like this echo '<br>'.base64_encode(mhash(MHASH_MD5,'test')); result
friends, i have created custom title bar using following titlebar.xml file with code <?xml
Friends, My apex page has several different radio groups, each one can have the
Friends, I am new to lucene full text search. i have developed page with
friends. I know, there are many questions here already on these iterators. I've read
Friends, I am able to get XML file by sing bytes, perhaps which is
Friends text box with autocomplete script (which supports multiple word completion from userstable fullname
Friends, I've a gridview in my web-page designed in the following manner:- <div id=divProducts

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.