【问题标题】:php multiple array into mysqlphp多个数组到mysql
【发布时间】:2016-01-06 03:51:57
【问题描述】:

我是编程新手,我有一个项目陷入困境。 我有一个表单,我可以在需要时选择添加更多行(克隆行)。这是表格:

<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>

当我提交这个表单时,它给了我多个数组,所以它们就在这里。

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
        )

)

如何将这些数据插入到 mysql 中? 这是我的第一篇文章,如果我写错了,请见谅。

【问题讨论】:

  • 你试过的sql查询在哪里?
  • 为了准确回答这个问题,我们需要查看您的数据库结构,并且了解您迄今为止尝试过的内容也会有所帮助。我投票结束这个问题,因为它不清楚。如果您可以编辑您的帖子以添加有关您尝试过的内容以及您遇到的具体问题的详细信息,我们可以投票重新提出问题。如果您的问题仅仅是您不知道如何使用数据库,我建议您寻求教程——不知道如何编码不是我们可以客观地帮助您解决的问题。

标签: php mysql arrays


【解决方案1】:

在您的 PHP 脚本中拉出 post 脚本 -

$var= $_POST['cod'];

然后使用foreach循环

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

为你的插入做这样的事情

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

    VALUES ('".$$var[0]."');";

【讨论】:

  • 请不要在您的回答中要求支持。此外,您的帖子没有回答问题,您正在推测需要做什么,因为问题含糊不清。请记住这一点——糟糕的问题通常会导致糟糕的答案。寻找有具体和具体答案的问题。 -1 这个。
【解决方案2】:
<?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);

?>

【讨论】:

  • 您好,付款人,我测试了您的样品,效果很好。非常感谢您的时间和帮助。
猜你喜欢
  • 2016-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-10
相关资源
最近更新 更多