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 7644963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:49:05+00:00 2026-05-31T09:49:05+00:00

Values submitted by a form are stored in $array. $array= array(123,James,New York); MySQL table(Customer)

  • 0

Values submitted by a form are stored in $array.

$array= array("123","James","New York");

MySQL table(Customer)

ID-----NAME-----CITY

I am new to PHP.I want to insert the array data into relevant columns in the MySQL table.

mysql_query("INSERT INTO customer (ID,NAME,CITY) 
                    VALUES ('123','James','New York')" );

can be used however the number of array elements can change in each form submission.

How can I do it using PHP?

Note:

In select table page the user selects the table from the drop down menu then the customized form is loaded. So the Column names depend on the table the user selects. I can get the values submitted in the form in “$array”. But the problem is number of values change each time.

mysql_query(INSERT INTO customer VALUES ($array)); //this format cannot be used

Select table code

<?php
include("functions.inc");
connectDatabase();
$queryseltable= "SHOW TABLES" or die("error query");

$result=mysql_query($queryseltable);


?>

<link href="style.css" rel="stylesheet" type="text/css" />

<form action="process.php" method="post">

<div id="wrapper">

    <table>
      <tr>
        <td id="right">Select Table:</td>
        <td id="input">

        <select name="table">

        <?php

        while($row=mysql_fetch_array($result)){


            extract($row);
            echo "<option>".$Tables_in_database ."</option>";        
        }
        ?>

        </select>

        </td>
      </tr>

      <tr>
        <td></td>
        <td><input type="submit" value="Select" /></td>
      </tr>

    </table>
</div>

</form>
<?php
?>

Process.php code

<link href="style.css" rel="stylesheet" type="text/css" />

<form action="insert.php" method="post">

<?php

include("functions.inc");

connectDatabase();

$table= $_POST['table'];

$_SESSION['table'] = $table;

$query= "SELECT * FROM $table";

$result= mysql_query($query) or die("query failed");

$count=mysql_num_fields($result);

$fieldname = mysql_field_name($result, 0);

?>

<div id="wrapper">

    <table>

        <?php

        for($x=0; $x<$count;$x++){

                $fieldname = mysql_field_name($result, $x);
        echo "  
                <tr>
                     <td id=\"right\">$fieldname</td>
                     <td id=\"input\"><input type=\"text\" name=\"test[]\" /></td>
                </tr>
      ";


      }

      ?>
      <tr>
        <td></td>
        <td><input type="submit" value="Insert" /></td>
      </tr>

    </table>
</div>

</form>
  • 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-05-31T09:49:06+00:00Added an answer on May 31, 2026 at 9:49 am

    Use PDO prepared statements, like this:

    $pdo = new PDO($dns); // where $dns is your connection string
    $q = $pdo->prepare('INSERT INTO customer (ID, NAME, CITY) VALUES (?, ?, ?)');
    $q->execute(array("123","James","New York"));
    

    Read here about PDO

    Updated:

    If you really need this, you can pass array through POST, like

    <form>
        <input type="hidden" name="table[column1]" value="value1" />
        <input type="hidden" name="table[column2]" value="value2" />
        ...
        <input type="hidden" name="table[columnN]" value="valueN" />
    </form>
    

    with named columns, because your form generating when you KNOW what the columns are.

    In PHP you would get:

    $array = $_POST['table'];
    // Now, $array === array('column1' => 'value1', 'column2' => 'value2', ..., 'columnN' => 'valueN');
    

    And you can do this:

    $pdo = new PDO($dns); // where $dns is your connection string
    $q = $pdo->prepare(sprintf(
        'INSERT INTO customer (%s) VALUES (%s)',
        implode(', ', array_keys($array)), // column names separated by ', '
        implode(', ', array_fill(0, count($array), '?')) // ? marks for placing values
    ));
    $q->execute($array);
    

    Hope that helps you some how. But SHOULD KNOW THAT THIS WAY IS VEERY UNSECURED, PLEASE DO NOT THIS UNTIL YOU REALLY NEED IT

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I print the name and values of a submitted form. Thanks for
I have a form that once submitted some of its result are stored in
I have this form: <form name=commentform id=commentform action=comment.php method=post enctype=multipart/form-data> Your Name: <textarea maxlength=60
Since I am using get I expect to see the submitted values appended to
char *values = 3 1 4 15; vector<int> array; I want to populate the
I have values stored as strings in a DataTable where each value could really
I inserted some values into a table. There is a column whose value is
I am passing values from a form to a processing page. There are more
I have a form, like so: <form action= method=post> <select name=pageType> <option value=main>Main Page</option>
OK this is my script:- <form action=results.php method=post> <?php mysql_select_db($database, $databasename) or die(Opps some

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.