douniwan_0025 2015-07-29 09:06
浏览 37
已采纳

保留从php中的数组中的表单发送的多个值

   <html>
            <body>
    <form method="post" action="#">
        <h1> AB_NO</h1>
        <input type=text name="excel"> //input values that need to be checked for presence in database.

        <input type=submit name="submit">

        </form>
            <?php
    session_start();
    if(isset($_POST['submit']))
        {
    $host="localhost";
    $user="root";
    $password="";
    $db="green";
    $table="manitable";

    mysql_connect("$host","$user","$password") or die("Cannot Connect");
    mysql_select_db("$db") or die("Cannot select DB!");

    $var=$_POST['excel'];

    $sql="SELECT * FROM manitable WHERE (ab_no = '$var')";

    $result=mysql_query($sql);
    $data_item=array();
    $items=array();

    if (mysql_num_rows($result) == 1) {
    print 'Product Exists' ;

        while ($row = mysql_fetch_array($result)) {

        $data_item['doc_no'] = $row['doc_no'];
        $data_item['ab_no'] = $row['ab_no'];
        $data_item['od_id'] = $row['od_id'];


        $items[] = $data_item;

    }
        print_r($items);

    }
    else {
    print 'Sorry, this Product is not present' ;
    }
    }

    ?>
    </body>
        </html>  

The above code is the code I am using.

The form field with name=excel takes in different strings at a time. On each value entered I want it to be stored in the $items array. The value entered in form field is checked if it is present in database. If present it must be stored in the $items array along with the other column values. Initially a value is entered in the field and the value along with the corresponding column entries are stored in the items array.

The items array at an index value of 0 will hold the corresponding values from the form value that is sent. Now when a second value is again passed through the form I want it to be stored in the array as a second entry in the items array.

My manitable table in database has the following columns:

  • doc_no
  • ab_no
  • od_id

The output is:

 Product ExistsArray ( [0] => Array ( [doc_no] => 202344223341312 [ab_no] => SMLP3105153342 [od_id] => ODRD3028479929633865301 ) )

I want the new ab_no entries which are matched on comparison with db values to be added to the [1], [2] and so on indices of the items array.

Thanks in advance.

  • 写回答

2条回答 默认 最新

  • duandu1966 2015-07-29 09:45
    关注

    You will need to use Sessions if you want to store the value after submit.

    First start a session if there is none or just leave it like you already have it now.

    Recommended way for versions of PHP >= 5.4.0

    if (session_status() == PHP_SESSION_NONE) {
        session_start();
    }
    

    Source: http://www.php.net/manual/en/function.session-status.php

    For versions of PHP < 5.4.0

    if(session_id() == '') {
        session_start();
    }
    

    Then store your $data_item in the session instead of the $items array.

    $_SESSION['items'][] = $data_item;
    $items = $_SESSION['items'];
    

    Then you can print_r() your $items.

    And don't forget to leave your action out of the form like Sverri said.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码