简体   繁体   English

在PHP中只获取最后一个ID

[英]getting only last id in php

I have total 7 Id in MySQL table. 我在MySQL表中共有7个ID。 and I am fetching those Id's from database 我正在从数据库中获取那些ID

code is : 代码是:

<form class="form-inline"  method="POST" action="book.php">
<div class="row-fluid home">
    <div class="span3">


    <?php
        $query=mysql_query("select * from category");
        $cat_count = 0;
        while($result=mysql_fetch_array($query))
        {
            $category_id=$result['cat_id'];
            $category_type=$result['cat_type'];
            $categroy_desc=$result['cat_desc'];
            $category_image=$result['cat_images'];
            $category_price=$result['cat_price'];
            $cat_count += 1;
            //$_SESSION["session_category_id"] = $category_id;
    ?>      
<div  class="room_selector" data-price="<?php echo $category_price; ?>" data-adults="4" data-kids="2"
<?php if ($cat_count!=1) {echo 'style="display: none;"'; } ?> >

            <h5>
                <a href="index.php?id=<?php echo $category_id; ?>" class="pull-left">
                    <i class="icon-chevron-left"></i>
                </a>                           
                    <?php echo $category_type; ?>                       
                <a href="index.php?id=<?php echo $category_id; ?>" class="pull-right ">             
                    <i class="icon-chevron-right"></i>
                </a>
            </h5>               
                <a class="iframe" href="rooms.php">             
                  <img src="data:image/png;base64,<?php echo base64_encode($category_image); ?>" alt="No Photo" />              
                </a>

               <p><?php echo $categroy_desc;?></p><br><Br><bR>
               <!--<a class="btn btn-primary btn-large book-now" name="submit" type="submit" href="book.php?id=<?php echo $category_id; ?>">
               Check Availability!</a>-->
             <?php echo $x=$category_id;
            ?>
              <input type="hidden" name="cat_id" value="<?php echo $category_id; ?>">
            <?php  $users[] = $result['cat_id'];?>
</div>

            <?php
        }
        ?> 
          <?php echo $category_id; 
        ?>

    </div>      
    <div class="span3 home_calendar">
                    <div class="form-horizontal">           
            <div class="control-group">
                <label class="control-label pull-left" for="inputEmail">Arrive</label>
                <div class="controls">
                    <input type="text" value=""  name="from" class="span2 check-in-date" value="2015/05/22" />
                </div>
            </div>
        </div>
        <div class="datepicker_from"></div>
    </div>
    <div class="span3 home_calendar">
                    <div class="form-horizontal">           
            <div class="control-group">
                <label class="control-label pull-left" for="inputEmail">Depart</label>
                <div class="controls">
                    <input type="text" value=""  name="to" class="span2 check-out-date" value="2015/05/27" />
                </div>
            </div>
        </div>
        <div class="datepicker_to"></div>
    </div>

    <div class="span3">
        <div class="form-horizontal">

            <div class="control-group">
                <label class="control-label" for="inputEmail">Rooms</label>
                <div class="controls">
                    <select class="span1 select_rooms" name="no_of_selected_room">
                        <option>1</option>
                        <option>2</option>
                        <option>3</option>
                        <option>4</option>
                        <option>5</option>
                    </select>
                </div>
            </div>          


            <div class="control-group">
                <label class="control-label" for="inputEmail">Adults per room</label>
                <div class="controls">
                    <select class="span1 select_adults" name="no_of_adults">
                        <option>1</option>
                        <option>2</option>
                        <option>3</option>                          
                    </select>
                </div>
            </div>

            <div class="control-group">
                <label class="control-label" for="inputEmail">Kids per room</label>
                <div class="controls">
                    <select class="span1 select_kids" name="no_of_kids">
                        <option>1</option>
                        <option>2</option>
                        <option>3</option>
                        <option>4</option>
                        <option>5</option>
                    </select>
                </div>
            </div>
            <div class="control-group">
                <label class="control-label" for="inputEmail">extra person</label>
                <div class="controls">
                    <select class="span1 select_kids">
                        <option>1</option>
                        <option>2</option>
                        <option>3</option>                          
                    </select>
                </div>
            </div>


            </div>
    <div id="total_price" class="price" name="price"></div>

    <input type="submit" class="btn btn-primary btn-large book-now" name="submit" value="Check  Availability!">
    <!--<a class="btn btn-primary btn-large book-now"  type="submit" href="book.php?id=">Check Availability!</a>--> 


</div>  


The $category_id is the main key to my project. $category_id是我的项目的主键。 once user will select the room then the $category_id will select based on that. 一旦用户选择了房间,则$category_id将基于该房间进行选择。

I am trying to send this id to other page using one form //book.php 我正在尝试使用一种形式//book.php将此id发送到其他页面

if(isset($_POST['submit']))
{    

 $n_cat_from = $_POST['from'];
 $n_cat_to = $_POST['to'];
 $n_no_of_selected_rooms = $_POST['no_of_selected_room'];
 $n_no_of_adults = $_POST['no_of_adults'];
 $n_no_of_kids = $_POST['no_of_kids'];

 $current_Category_id = $_POST['cat_id'];

 echo "id;;;;;;;;;;;;" . $current_Category_id;
}       
?>

When I try to echo that id I am getting only last id in the database (after user selecting different id also ) 当我尝试echoid时,我仅获得数据库中的最后一个ID(在用户选择其他ID之后)

where I am wrong ? 我哪里错了? why it is taking last Id ? 为什么要使用最后一个ID? help me 帮我

if you want to store multiple values in a hidden input with the same name, you have to add [] after the name, like this: 如果要在具有相同名称的隐藏输入中存储多个值,则必须在名称后添加[] ,如下所示:

<input type="hidden" name="cat_id[]" value="<?php echo $category_id; ?>">
        <?php  $users[] = $result['cat_id'];?>

this way, you should have an array in book.php: 这样,您应该在book.php中有一个数组:

$_POST[cat_id] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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