简体   繁体   English

PHP的多个数组到MySQL

[英]php multiple array into mysql

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? 如何将这些数据插入mysql? This is my first post so sorry if I wrote anything wrong. 这是我的第一篇文章,对不起,如果我写错了什么。

On your PHP script pull the post script through - 在您的PHP脚本上,将发布脚本拉至-

$var= $_POST['cod'];

Then use a foreach loop 然后使用foreach循环

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]."');";
<?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);

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM