我如何使用多aray插入我的数据PHP

时间:2017-02-23 09:45:40

标签: php sql

<form action="insert.php" method="post">
<!-- Student 1 -->
Student Name :<input type="text" name="stname[]">
Phone:<input type="text" name="phone[]">
Sex:<input type="text" name="sex[]">
Address:<input type="text" name="address[]">

<!-- Student 2 -->
Student Name :<input type="text" name="stname[]">
Phone:<input type="text" name="phone[]">
Sex:<input type="text" name="sex[]">
Address:<input type="text" name="address[]">
</form>

如何将其插入我的表tbl_student?

2 个答案:

答案 0 :(得分:0)

我认为你应该从$ _POST获取数据作为数组。

foreach($_POST['stname'] as $i => $name) {
  $phone = $_POST['phone'][$i];
  $sex = $_POST['sex'][$i];
  $address = $_POST['address'][$i];
  // Do something with data
}

答案 1 :(得分:0)

你也可以尝试这个

if (isset($_POST)) {
    $data = $_POST;

    $names = $data['stname'];

    foreach($names as $key => $n) {
        $name = $n;
        $phone = $data['phone'][$key];
        $sex = $data['sex'][$key];
        $add = $data['address'][$key];

        echo 'name: ' . $name;
        echo 'phone: ' . $phone;
        echo 'sex: ' . $sex;
        echo 'add: ' . $add;
        echo '<br>';
    }
}