从多个表单发送数据到mysql

时间:2016-04-20 04:51:26

标签: php mysql forms

我很难将多个用户记录发送到我的mysql数据库。我正在尝试创建一个小型注册应用程序,用户可以选择事件以及他们想要在其上注册的人数。

然后我会根据用户选择返回特定数量的注册表单。接下来我想将所有这些数据发送到我的桌子。

到目前为止,这是我的代码

if (isset($_POST['submit'])) {

$persons = $_POST['person_number'];
$event = $_POST['event'];

for ($i=1; $i <= $persons; $i++) {
    echo "<b>Uczestnik #" . $i;
    $content = "<form action='thankyou.php' method='post' />";
    $content .= "<div class='form-group'>
    <label for='Imię'>Imię</label>
    <input type='text' class='form-control' name='name' />
    </div>";
    $content .= "<div class='form-group'>
    <label for='Nazwisko'>Nazwisko</label>
    <input type='text' class='form-control' name='last-name' />
    </div>";
    $content .= "<div class='form-group'>
    <label for='email'>Adres Email</label>
    <input type='email' class='form-control' name='email' />
    </div>";

    echo $content;

     }
    echo "<button type='submit' class='btn btn-primary' name='submit2>Rejsestruje się</button>";
    echo "</form>";
}
?>

另外我还有另外一个问题,因为我的用户选择了几个事件我存储他们的选择和数组,然后将其发布到表单页面。我如何将它分开并作为表查询的输入,例如10个用户可以去房间a和20到房间b?

                <input type="checkbox" class="ck" name="event[]" id="event" value="<?php echo $row['name'];?>"><span>Wybierz</span>

非常感谢您的支持

1 个答案:

答案 0 :(得分:3)

回答第一个问题: -

//在Thankyou.php中请写下这些行代码

$flag = 0;
if (isset($_POST['submit2'])) {
  if(count($_POST['name']) > 0) {  
    for($k=0;$k<=count($_POST['name']);$k++) {
        if(!empty($_POST['name'][$k])) {
            $insert = "insert into users set
                       name = '".$_POST['name'][$k]."',
                       last_name = '".$_POST['last_name'][$k]."',
                       email = '".$_POST['email'][$k]."'";
             mysql_query($insert);
            $flag++;
        } 

     }    
  } 
   echo "Total number of persons has been inserted is : ". $flag;
   exit;
}



//Main Page Content
if (isset($_POST['submit'])) {
$persons = $_POST['person_number'];
$event = $_POST['event'];
$content = "<form  method='post' />";
for ($i=1; $i <= $persons; $i++) {
    $content .= "<div><b>Uczestnik #" . $i.'</div>';    
    $content .= "<div class='form-group'>
    <label for='Imię'>Imię</label>
    <input type='text' class='form-control' name='name[]' />
    </div>";
    $content .= "<div class='form-group'>
    <label for='Nazwisko'>Nazwisko</label>
    <input type='text' class='form-control' name='last_name[]' />
    </div>";
    $content .= "<div class='form-group'>
    <label for='email'>Adres Email</label>
    <input type='email' class='form-control' name='email[]' />
    </div>";  
    $content .="<br>";
     }  

     $content .="<input type='submit' class='btn btn-primary' name='submit2' value='Rejsestruje się'>";
     $content .="</form>";
     echo $content;
}