How To Register user Form with all dynamic fields

2019-09-25 04:47发布

<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条回答
来,给爷笑一个
2楼-- · 2019-09-25 05:45
// 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.")");
    }
    ?>
查看更多
登录 后发表回答