3

I have an order form. It has 10 textfields for the user to input a quantity. How do you store the inputs to an array and insert to a db field(separated by comas)? Note: It is NOT required to input on all the textfields. For instance, the inputs are 1,2,3,4..it should appear in the db field also 1,2,3,4

examples and descriptions would be great. I'm relatively new to this.

4
  • use implode to make the input a quantity a string with delimeter , then insert into the db. Commented Jan 3, 2015 at 10:38
  • @SuchitKumar Can you demonstrate? i've got no idea how to do it.sorry Commented Jan 3, 2015 at 10:41
  • see in answer i hace echoed $data you need to insert it using sql to your table ,no need to echo. @kim Commented Jan 3, 2015 at 10:50
  • everyone thanks for answering but the chosen one was the easiest to understand for me. I'll take note of them though. have a nice day. Commented Jan 3, 2015 at 11:17

3 Answers 3

3

So, let's say you have a table with four text fields - fieldOne, fieldTwo, fieldThree, fieldFour.

Your html form should look like this (i'm skipping irrelevant parts).

<form method="POST">
    <textarea name='data[fieldOne]'></textarea>
    <textarea name='data[fieldTwo]'></textarea>
    <textarea name='data[fieldThree]'></textarea>
    <textarea name='data[fieldFour]'></textarea>
</form>

Now, your PHP code:

$data = $_POST['data']; // PHP converts names like data[fieldOne] into arrays.

foreach ($data as $key => $value) {
    $fieldNames[] = "`{$key}`";    //field names for our insert statement - `fieldOne`, `fieldTwo`, etc...
    $values[":{$key}"] = strip_tags($value); 
}

$stmt = $pdo->prepare("INSERT INTO `tableName` (".implode(', ', $fieldNames).") VALUES (".implode(", ", array_keys($values)).")"; // If you're not using PDO and prepared statements you're doing it wrong, so i absolutely recommend you learning that if you haven't already.
$stmt->execute($values);

That should do the trick. Notice, using prepared statements frees you from manually escaping your data. However, if you're concerned about XSS attacks, you still should use strip_tags of a filter extension.

Sign up to request clarification or add additional context in comments.

2 Comments

Arent u missing a : in front of the array_keys($values)?
Nope, because of $values[":{$key}"]
2

HTML

<form action="my_page_that_processes_orders.php" method="post">
  <input type="text" name="order[product1]" />
  <input type="text" name="order[product2]" />
  <input type="text" name="order[product3]" />
  <input type="text" name="order[product4]" />
  <input type="submit" />
</form>

my_page_that_processes_orders.php

$data = $_POST["order"];// Advisable to perform checks for security reasons (not going to do it)

$aux = '';
foreach($data as $product => $qty) {
 // Do whatever you please with the data.
 $aux .= "$product $qty,"; // For instance.
}

$string_to_insert = rtrim($aux, ",");
// Insert it in DB or do more processing stuff.

Hope it helps.

Kind regards.

Comments

0

suppose this is your form: if you are only bothering about order array nothing else like corresponding key.

<form action="submit.php" method="post">
  <input type="text" name="order[]" />
  <input type="text" name="order[]" />
  <input type="text" name="order[]" />
  <input type="text" name="order[]" />
  <input type="submit" />
</form>

your submit.php:

     <?php 
       $data ="";
  foreach($_POST["order"] as $key=>$val){
    if(isset($val) && $val !="")
      $data.=$val.",";
  }
   $data=trim($data);
    echo $data;// use data 

Note: do not use mysql use PDO or mysqli instead. mysql is deprecated.

7 Comments

it worked thanks a lot but it leaves comas to the blank textfields. how to fix that?i have 10 textfields but if only 2 have inputs, it results to 1,2,,,,,,,,
@kim use trim for this:$data = implode(',',$_POST["order"]); echo trim($data,',');
In mysql field it still appears like that 1,2,,,,,,,,
Hi. i just tested again. If the inputs from textfield skips in between, like input 1,2,3 from textfld1,textfld2, and textfld5, it still results to 1,2,,,3
can you tell me why you are storing like this.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.