How To Register user Form with all dynamic fields name in php

强颜欢笑 提交于 2019-12-13 09:28:05

问题


<form method="post">
      User Name : <input type="text" name="values[]"><br/>
      User Mobile: <input type="text" name="values[]"><br/>
      User Email: <input type="text" name="values[]"><br/>
     <input type="submit" name="submit" values="submit">
  </form>

My Code/Logic

<?php
    $con=mysql_connect('localhost','root','');
    mysql_query('testing_user',$con); 
    if(isset($_POST['submit']))
    {
    $x=$_POST['values'];
    $data_values=array();
    for($i=0;$i<count($x);$i++)
    {
    echo $x[$i]."<br/>";
    array_push($data_values,$x[$i]);
    }
    //print_r($data_values[0]);
    $name=$data_values[0];
    $mobile=$data_values[1];
    $email=$data_values[2];
    mysql_query("insert into user_data values('','$name','$mobile','$email')");

    }
 ?> 

Check My Logic Please Write New Php Logic to register user record in mysql. i need Your Help ! Thank You


回答1:


// Logic One Using For Loop

    <?php
    error_reporting(0);
    $con=mysql_connect('localhost','root','') or die('Not Connect');
    mysql_select_db('multiple',$con);
    if (isset($_POST["submit"])){
    $field_name = $_POST['values'];
    $values = "";
    for ($i = 0; $i < sizeof($field_name); $i++) {
         $values .= "('".$field_name[$i]."')";
       if ($i != sizeof($field_name) - 1) {
           $values .= ", ";
       }
    }
    $sql = mysql_query("INSERT INTO php_test VALUES (" . $values.")");
    }
    }
    ?>

// Logic Two Using Foreach Loop

    <?php
    error_reporting(0);
    $con=mysql_connect('localhost','root','') or die('Not Connect');
    mysql_select_db('multiple',$con);
    if (isset($_POST["submit"])){
    $field_name = $_POST['values'];
    $values = "";
    $i=0;
    foreach($field_name as $searchval) {
      $values.="('".$searchval."')";
      if($i!=sizeof($field_name)-1){
         $values.=", ";
      }
      $i++;
    }
    $query =mysql_query("INSERT INTO php_test VALUES (".$values.")");
    }
    ?>


来源:https://stackoverflow.com/questions/43020011/how-to-register-user-form-with-all-dynamic-fields-name-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!