0

I'm new to programming and I have a project where I'm stuck. I have a form where I have the option to add more lines when needed (clone the line). Here is the form:

<form method="post" action="values.php">
<table>
  <tr>
    <th>Code</th>
    <th>Description</th>
    <th>Quantity</th>
    <th>Price</th>
</tr>
    <tr class="clone">
    <td><input type="text" name="cod[]" id="cod" class='input' /></td>
    <td><input type="text" name="desc[]" id="desc" class='inputp'/></td>
    <td><input type="text" name="qt[]" id="qt" class='input' /></td>
    <td><input type="text" name="price[]" id="price" class='input' /></td>
</tr>
</table>
<p>
    <a href="#" class="add" rel=".clone">Add Line</a>
</p>
   <input type="submit" value=" Submit " />
</form>

when I submit this form it gives me multiple arrays, so here they are.

Array
(
    [cod] => Array
        (
            [0] => 10
            [1] => 12
        )

    [desc] => Array
        (
            [0] => description for the code 10
            [1] => description for the code 12
        )

    [qt] => Array
        (
            [0] => 1
            [1] => 20
        )

    [price] => Array
        (
            [0] => 100.00
            [1] => 200.00
        )

)

How do I insert this data it into mysql? This is my first post so sorry if I wrote anything wrong.

2
  • Where is your sql query that you have tried? Commented Oct 8, 2015 at 13:31
  • 1
    In order to answer this question accurately, we need to see your database structure, and it would also help to know what you've tried so far. I am voting to close this question as unclear. If you can edit your post to add details about what you've tried and what you are specifically having problems with, we can vote to re-open the question. If your problem is simply that you do not know how to use a database, I suggest you seek tutorials -- not knowing how to code is not a problem we can objectively help you with. Commented Oct 8, 2015 at 14:24

2 Answers 2

0

On your PHP script pull the post script through -

$var= $_POST['cod'];

Then use a foreach loop

foreach ($var as $vars) {
    // use the numbers to access parts of the array
    $var[0];
    $var[1];
}

And do something like this for your insert

$query="INSERT INTO ????? (client_number)

    VALUES ('".$$var[0]."');";
Sign up to request clarification or add additional context in comments.

1 Comment

Please do not ask for upvotes in your answer. Also, your post does not answer the question, you're speculating about what needs to be done because the question is vague. Keep that in mind -- bad questions usually result in bad answers. Look for questions that have concrete and specific answers. -1 for this one.
-1
<?php

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
$db_selected = mysql_select_db('your_db_name', $link);
foreach($_POST['cod'] $key=>$value){
  $code = $value;
  $desc = $_POST['desc'][$key];
  $qt = $_POST['qt'][$key];
  $price = $_POST['price'][$key];
  $sql = "INSERT INTO table_name(`column_for_code`,`column_for_desc`,`column_for_qt`,`column_for_price`) VALUES('$code','$desc','$qt','$price')";
  mysql_query($sql);
}
mysql_close($link);

?>

1 Comment

Hi Payer, I tested your sample and worked perfectly. Thank you very very much for your time and help on it.

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.