sql >> Database >  >> RDS >> Mysql

Hoe een gebruikersformulier te registreren met de naam van alle dynamische velden in php

// 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.")");
    }
    ?>



  1. Sessievariabelen vs. Mysql-tabel

  2. mySQL-telling levert slechts één resultaat op, tenzij group by . wordt gebruikt

  3. SQL Server IF versus IIF():wat is het verschil?

  4. Hoe mysqlimport te gebruiken om het resultaat van mysqldump --databases in te lezen