代码之家  ›  专栏  ›  技术社区  ›  sheila.ar

创建多维数组

  •  0
  • sheila.ar  · 技术社区  · 5 年前

    怎么样,我在a中输入了一系列数字,用逗号(,)分隔 当我们按下send按钮时,这些数字中的每一个都被输入到bd中,具有不同的id。 例子: 我们进入 values = 5,6,7,8,9

    在bd中,它们是这样保存的:

    Id  values
    1      5
    2      6 
    3      7
    4      8
    5      9
    

    我想加一个 例子: 进入 值=5,6,7,8,9

    我们进入 cost = 100

    我想在进入这个系列时[5,6,7,8,9] 对他们来说,成本是100

    也就是说,在bd中以这种方式保存:

    Id   values cost
    1   5      100
    2   6      100
    3   7      100
    4   8      100
    5   9      100
    

    代码:

    索引文件

    <html>
    
    <head>
    <title></title>
    <meta name="robots" content="noindex, nofollow" />
    </head>
    
    <body>
    
    <form method="POST" action="process_values.php">
    
        <p><textarea rows="20" name="values" cols="40"></textarea></p>
    
        <input type="text" name="cost" value="" placeholder="cost">
    
        <p><input type="submit" value="Submit" name="B1"></p>
    </form>
    
    </body>
    
    </html>
    

    处理值.php

    <?php
    error_reporting(-1);
    ini_set("display_errors", 1);
    
    
    $value = $_POST["values"];        //We receive textarea values
    
    $valore = chop($value);    // Eliminate line breaks and space, but only at the end of the chain
    
    $val = nl2br($valore);         // We add the line breaks <br />
    
    $array_datos = explode(",", $val);   // Create an array with the received data using as a separator (,)
    
    
        foreach ($array_datos as $value)         // We create a foreach loop
        {
    
          include("$_SERVER[DOCUMENT_ROOT]/CN/connexion.php");
    
        $_SAVE_SQL = "INSERT INTO nombre_tabla (values) VALUES ('".$value."')";
        $mysqli->query($_SAVE_SQL);
    
        }
    
    ?>
    <html>
    
    
    <body>
    
    </table>
    
    <table border="1" width="600" id="table1">
        <tr>
            <td>ID</td>
            <td>VALUES</td>
        </tr>
    <?PHP
    /*******************************************************************************
    * CONSULTING DATA TO THE BD
    *******************************************************************************/
    if ($res = $mysqli->query("SELECT * FROM table_name ORDER BY id ASC", MYSQLI_USE_RESULT)) {
      /*******************************************************************************
      * The while loop that runs through each record and shows each field in the table.
      *******************************************************************************/
      while ($row = mysqli_fetch_array($res)){
        echo "
            <tr>
                <td>".$row['id']."</td>
                <td>".$row['values']."</td>
            </tr>
    
        ";
      }
    }
    ?>
    
    </body>
    
    </html>
    
    1 回复  |  直到 5 年前
        1
  •  0
  •   Raj Parekh    5 年前

    上表中的值是5,6,7,8,9,下表中的值是成本。

    我想你需要一些

    Id  values cost
    1   5      100
    2   6      100
    3   7      100
    4   8      100
    5   9      100
    

    把它加到下面 Values

    $cost = $_POST["cost"];

    只需修改SQL查询(考虑到数据库中已添加列开销): $_SAVE_SQL = "INSERT INTO nombre_tabla (values,cost) VALUES ('".$value."',$cost)";

        2
  •  0
  •   Jose Rojas    5 年前

    首先得到后变量的成本值,如下所示:

     $value = $_POST["values"];  
     $cost = $_POST["cost"];
    

    然后,您可以遍历这些值并将成本附加到每个值,如下所示:

    $statement = "INSERT INTO (values, cost) VALUES ";
    foreach($values as $item){
    
        $statement .= " ('".$item."', ".$cost.")" ;
    
    }
    
    $statement = substr($statement, 0, strlen($statement));
    $mysqli->query($statement);