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 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作