简体   繁体   中英

Retaining multiple values sent from form in an array in 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.

Forms that have action="#" will not be submitted. The # in URLs is called a fragment , and are used (among other things) to refer to elements in the document that have id attributes on them.

Since you are handling the form in the same file you should just omit the action attribute all-together:

<form method="post"></form>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM