php - Storing a list from form to database as an array

764

I'm trying to load a multi numbers(rows) to a database from a form. Anyone can help with the code?

$n = $_POST['txtname'];

foreach ($a as $value) {

$sql = "INSERT INTO Sheet1 VALUES($value)";
859

Answer

Solution:

You need to initialise an array with the data before using the foreach loop.

You should use the following format:

//Obtain values from form text boxes
$name = $_POST['Name'] //This must match the name input on your form

//Populate values into array
$arrayData = array('$name');  //Can also populate with multiple values..

//Query
foreach ($arrayData as $value) {
    $sql = "INSERT into table_name (table_column_name) VALUES($value)";
}

Sources: http://www.w3schools.com/sql/sql_insert.asp

http://bbrown.kennesaw.edu/papers/php2.html

People are also looking for solutions to the problem: html - PHP mail function doesn't complete sending of e-mail

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.