Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am currently producing a form via mysql / php. I have well over 1500 inputs with unique values and I would like to insert them into a mysql table (1 value / row)

I am creating the form this way:

echo "<input type='text' name='combination[]' value='justsometext". $row['name'] ."'><br />";

I have around 1500 inputs like this and I would like to insert them into one column, how do I go about?

I am using the following code to insert, but it is only inserting 0s instead of the actual values:

foreach ($_POST['combination'] as $combination) {
$sql="INSERT INTO alphabet_combination (combination)
VALUES
('$combination')";
}
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
share|improve this question
1  
Sounds like a horrible idea, 1500 inputs into 1 column? What are you trying to achieve with this? –  Jonast92 Nov 9 '13 at 17:40
    
why you have 1500 input??you have to think that better –  Emilio Gort Nov 9 '13 at 17:43
    
I am generating these inputs based on values from another database, so every input has different values. I have a column named combination and I want every input value to be stored in it, so basically 1500 inputs would generate 1500 rows with different values. –  user2973474 Nov 9 '13 at 17:48
    
For one thing, remove the " at the end after your semi-colon. (Instant error throw) –  Fred -ii- Nov 9 '13 at 17:56
    
I don't know why I added it here, it's no there in my form. –  user2973474 Nov 9 '13 at 17:58

2 Answers 2

up vote 1 down vote accepted

First of all: How can I prevent SQL injection in PHP?

Second: 1500 inputs? Wow... You have to double check if your php.ini configuration will handle it.

If you really want to put 1500 values in one column - maybe you should consider to serialize array and keep it that way?

In prepared statement this will look like this:

$combinations = serialize($_POST['combination']);

$q = mysqli_prepare($con, 'INSERT INTO alphabet_combination (combination) VALUES (?)');
mysqli_stmt_bind_param($q, 's', $combinations);
mysqli_stmt_execute($q);

If you want for each value single INSERT so after submit in database will be next 1500 rows:

$q = mysqli_prepare($con, 'INSERT INTO alphabet_combination (combination) VALUES (?)');
mysqli_stmt_bind_param($q, 's', $combination);

foreach ($_POST['combination'] as $combination) {
    mysqli_stmt_execute($q);
}
share|improve this answer
    
This did it, thank you very much! This is perfect. –  user2973474 Nov 10 '13 at 3:52

Try this code after submission of your form:

extract($_POST);
foreach ($combination as $comb) {
    //if your table has only one field inside:
    $query = mysql_query("INSERT INTO tablename VALUES('".$comb."')");
}

Another example for another idea along with the form:

<?php
if( isset($_POST['btnsubmit']) ){
    extract($_POST);
    foreach ($sample as $samp) {
        $query = mysql_query("INSERT INTO tablename VALUES('".$samp."')");
    }
}
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Sample</title>
</head>
<body>
    <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
        <input type="text" name="sample[]" value="hello1"><br>
        <input type="text" name="sample[]" value="hello2"><br>
        <input type="text" name="sample[]" value="hello3"><br>
        <input type="text" name="sample[]" value="hello4"><br>
        <input type="text" name="sample[]" value="hello5"><br>
        <input type="submit" name="btnsubmit" value="Submit">
    </form>
</body>
</html>
share|improve this answer
    
This seems to be working however it is only inserting the value from the last input. So I end up with 1499 rows with a 0 in it and row 1500 with the correct value, i.e. thistextwascombined. –  user2973474 Nov 9 '13 at 18:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.