0

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));
}
alexisdevarennes
  • 5,437
  • 4
  • 24
  • 38
  • 1
    Sounds like a horrible idea, 1500 inputs into 1 column? What are you trying to achieve with this? – Jonast92 Nov 09 '13 at 17:40
  • why you have 1500 input??you have to think that better – Emilio Gort Nov 09 '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. – alexisdevarennes Nov 09 '13 at 17:48
  • For one thing, remove the `"` at the end after your semi-colon. (Instant error throw) – Funk Forty Niner Nov 09 '13 at 17:56
  • I don't know why I added it here, it's no there in my form. – alexisdevarennes Nov 09 '13 at 17:58
  • Stranger things have happened ;-) @user2973474 – Funk Forty Niner Nov 09 '13 at 17:59
  • You would need a `foreach` method for this. [**This answer**](http://stackoverflow.com/a/11059031/1415724) may help as will [**this one**](http://stackoverflow.com/a/7303589/1415724) yet not entirely sure that those methods will help you 100% but will surely help to shed a bit of light on the subject. Google "foreach form post mysql php" for possible results. @user2973474 – Funk Forty Niner Nov 09 '13 at 18:05
  • OK, so I created this but instead of inserting the values its just adding 0s. foreach ($_POST['combination'] as $combination) { $sql="INSERT INTO alphabet_combination (combination) VALUES ('$combination')"; } if (!mysqli_query($con,$sql)) { die('Error: ' . mysqli_error($con)); } – alexisdevarennes Nov 09 '13 at 18:28

2 Answers2

1

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);
}
Community
  • 1
  • 1
speccode
  • 1,562
  • 9
  • 11
0

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>
Nick John Bueno
  • 111
  • 1
  • 1
  • 8
  • 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. – alexisdevarennes Nov 09 '13 at 18:38