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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀