CodeIgniter UPDATE查询执行两次

2022-05-09 00:00:00 php codeigniter-2

我正在使用CodeIgniter 2.2。

每次访问页面时,我都必须用+1更新数据库。代码可以工作,但每次都会递增+2。

示例:如果total views=2,则点击页面后total views应为3,但数据库中的值为4

我确信我在控制器中只调用了模型add_one_to_view_image一次。

控制器

   function view(){

    $view_id =  $this->uri->segment(3);

    if($view_id){
        $this->property->add_one_to_view_image($view_id);
        $prop['prop_detail'] = $this->property->get_single_property_for_view($view_id);
        $prop['prop_imgs'] = $this->property->get_single_property_images($view_id);
        $prop['amenities'] = $this->property->get_single_property_amenities($view_id);
        $prop['latest'] =  $this->home->get_latest_properties();

        $size = sizeof($prop['latest']);
        for($k=0; $k< $size; $k++){
        $Image_name = $this->property->get_property_first_image($prop['latest'][$k]->property_id);
            if($Image_name){
            $prop['latest'][$k]->image=$Image_name[0]->name;
            }else {
                $prop['latest'][$k]->image="";
            }
        }


    $this->load->view('property_view', $prop); 
    }

}

模型

  function add_one_to_view_image($id){

  echo "We in add one to views";
  $this->db->where('property_id', $id);
  $this->db->set('views', 'views+1', FALSE);
  $this->db->update('property_views'); 
 }

我在模型中添加了一条ECHO语句。而且它只打印一次..

查看

<!DOCTYPE html>
            <html lang="en-US">
            <head>

                <?php $this->load->view('header_links'); ?>


                <title><?php echo $prop_detail[0]->title; ?> | lanka Property</title>
                <meta name="author" content="Sathyabaman - lankaproperty.com">
                <meta name="description" content="<?php echo $prop_detail[0]->full_description; ?>">
                <meta name="keywords" content="home, land, apartment, house, room, Commercial Building, buy, sell, rent, lease, sri lanka">


            </head>
            <body>
            <div id="wrapper-outer" >
                <div id="wrapper">
                    <div id="wrapper-inner">
                        <?php $this->load->view('header_top_user_bar'); ?>
                        <?php $this->load->view('header_logo'); ?>
                        <?php $this->load->view('navigation'); ?>


                        <!-- CONTENT -->
                        <div id="content"><div class="container">
                <div id="main">
                    <div class="row">
                        <div class="span9">
                            <h1 class="page-header"><?php echo $prop_detail[0]->title; ?></h1>

                            <div class="carousel property">
                                <div class="preview">
                                        <li class="active">
                                            <img src="<?php echo base_url(); ?>/upload_images/<?php echo $prop_imgs[0]->name; ?>" alt="">
                                        </li>

                                </div><!-- /.preview -->

                                <div class="content">

                                    <a class="carousel-prev" href="#">Previous</a>
                                    <a class="carousel-next" href="#">Next</a>
                                    <ul>

                                        <?php foreach ($prop_imgs as $img) : ?>
                                        <li class="active">
                                            <img src="<?php echo base_url(); ?>/upload_images/<?php echo $img->name; ?>" alt="">
                                        </li>
                                        <?php endforeach; ?>

                                    </ul>
                                </div>
                                <!-- /.content -->
                            </div>
                            <!-- /.carousel -->



                            <div class="property-detail">
                                <div class="pull-left overview">
                                    <div class="row">
                                        <div class="span3">
                                            <h2>Overview</h2>

                                    <?php foreach ($prop_detail as $dtl) : ?>

                                            <table>

                                                <tr>


                                                    <?php   if($dtl->status == 0){?>
                                                    <td style="font-size: 120%;"><strong>Under Approval</strong></td>
                                                    <?php } if($dtl->status == 1){?>
                                                    <td style="color:green; font-size: 200%;"><strong>Available!</strong></td>
                                                    <?php } if($dtl->status == 2){?>
                                                    <td style="color:red; font-size: 200%;"><strong>Expired!</strong></td>
                                                    <?php } if($dtl->status == 3){?>
                                                    <td style="color:red; font-size: 200%;"><strong>Deleted!</strong></td>
                                                    <?php } if($dtl->status == 4){?>
                                                    <td style="color:red; font-size: 200%;"><strong>Suspended!</strong></td>
                                                    <?php } ?>

                                                </tr>

                                                <tr>
                                                    <th></th>
                                                    <td><br/></td>
                                                </tr>
                                                 <tr>
                                                    <th>Property ID :</th>
                                                    <td> PPTID<?php echo $dtl->property_id; ?></td>
                                                </tr>

                                                <tr>
                                                    <th>Price :</th>
                                                    <td><?php if($dtl->price == 0){ echo "negotiable"; }
                                                                else {
                                                                    $price_text = (string)$dtl->price; // convert into a string
                                                                    $arr = str_split($price_text, "3"); // break string in 3 character sets
                                                                    $price_new_text = implode(",", $arr);  // implode array with comma

                                                                    echo 'Rs. '.$price_new_text.'/='; 

                                                                } ?>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <th>Property type :</th>
                                                    <td><?php echo $dtl->type_name; ?></td>
                                                </tr>
                                                <tr>
                                                    <th>Property size :</th>
                                                    <td><?php echo $dtl->size; ?>  <?php echo $dtl->size_type; ?></td>
                                                </tr>
                                                <tr>
                                                    <th>Contract type :</th>
                                                    <td><?php echo $dtl->contract_type; ?></td>
                                                </tr>

                                                <?php if($dtl->bed){ ?>
                                                <tr>
                                                    <th>Bedrooms :</th>
                                                    <td><?php echo $dtl->bed; ?></td>
                                                </tr>
                                                <?php } if($dtl->bath){ ?>
                                                <tr>
                                                    <th>Bathrooms :</th>
                                                    <td><?php echo $dtl->bath; ?></td>
                                                </tr>
                                                <?php } if($dtl->address){?>
                                                <tr>
                                                    <th>address :</th>
                                                    <td><?php echo $dtl->address; ?></td>
                                                </tr>
                                                <?php } ?>
                                                <tr>
                                                    <th>Area :</th>
                                                    <td><?php echo $dtl->area; ?></td>
                                                </tr>
                                                <tr>
                                                    <th>City :</th>
                                                    <td><?php echo $dtl->city; ?></td>
                                                </tr>

                                                <?php if($dtl->Contact_name){ ?>
                                                <tr>
                                                    <th>Contact Name :</th>
                                                    <td><?php echo $dtl->Contact_name; ?></td>
                                                </tr>
                                                <?php } ?>

                                                <tr>
                                                    <th>Contact No 1 :</th>
                                                    <td><strong><?php echo chunk_split($dtl->phone_home, 3, ' '); ?></strong></td>
                                                </tr>

                                                <?php if($dtl->phone_hand){ ?>
                                                <tr>
                                                    <th>Contact No 2 :</th>
                                                    <td><strong><?php echo chunk_split($dtl->phone_hand, 3, ' '); ?></strong></td>
                                                </tr>
                                                <?php } ?>

                                                <tr><td colspan="2">
                                                <br/>
                                                <a class="btn btn-primary btn-large list-your-property">
                                                 Contact : <?php echo chunk_split($dtl->phone_home, 3, ' '); ?></a>
                                                </td></tr>

                                            </table>
                                        </div>
                                        <!-- /.span2 -->
                                    </div>
                                    <!-- /.row -->
                                </div>

                                <p><?php echo nl2br($dtl->full_description); ?>.</p>


                            <?php endforeach; ?>
            <br/><br/>
                                <h2>General amenities</h2>

                                <div class="row">
                                    <ul class="span2">
                                        <li class="<?php if($amenities[0]->Gym==1){echo 'checked'; }else{echo 'plain';} ?>">
                                            Gym
                                        </li>
                                        <li class="<?php if($amenities[0]->Air_conditioning==1){echo 'checked'; }else{echo 'plain';} ?>">
                                            Air Conditioning
                                        </li>
                                        <li class="<?php if($amenities[0]->internet==1){echo 'checked'; }else{echo 'plain';} ?>">
                                            Internet
                                        </li>
                                        <li class="<?php if($amenities[0]->Wifi==1){echo 'checked'; }else{echo 'plain';} ?>">
                                            Wifi
                                        </li>
                                        <li class="<?php if($amenities[0]->Lift==1){echo 'checked'; }else{echo 'plain';} ?>">
                                            Lift
                                        </li>
                                        <li class="<?php if($amenities[0]->Furnished==1){echo 'checked'; }else{echo 'plain';} ?>">
                                            Furnished
                                        </li>

                                    </ul>
                                    <ul class="span2">
                                        <li class="<?php if($amenities[0]->Television==1){echo 'checked'; }else{echo 'plain';} ?>">
                                            Television
                                        </li>
                                        <li class="<?php if($amenities[0]->Hot_Water==1){echo 'checked'; }else{echo 'plain';} ?>">
                                            Hot Water
                                        </li>
                                        <li class="<?php if($amenities[0]->Smoking_allowed==1){echo 'checked'; }else{echo 'plain';} ?>">
                                            Smoking Allowed
                                        </li>
                                        <li class="<?php if($amenities[0]->Pets_allowed==1){echo 'checked'; }else{echo 'plain';} ?>">
                                            Pets Allowed
                                        </li>
                                        <li class="<?php if($amenities[0]->Garage==1){echo 'checked'; }else{echo 'plain';} ?>">
                                            Garage
                                        </li>
                                        <li class="<?php if($amenities[0]->Secure_parking==1){echo 'checked'; }else{echo 'plain';} ?>">
                                            Secure parking
                                        </li>

                                    </ul>
                                    <ul class="span2">
                                        <li class="<?php if($amenities[0]->Swimming_Pool==1){echo 'checked'; }else{echo 'plain';} ?>">
                                            Swimming Pool
                                        </li>
                                        <li class="<?php if($amenities[0]->Tennis_court==1){echo 'checked'; }else{echo 'plain';} ?>">
                                            Tennis court
                                        </li>
                                        <li class="<?php if($amenities[0]->Balcony==1){echo 'checked'; }else{echo 'plain';} ?>">
                                            Balcony
                                        </li>
                                        <li class="<?php if($amenities[0]->Fenced==1){echo 'checked'; }else{echo 'plain';} ?>">
                                            Fenced
                                        </li>
                                        <li class="<?php if($amenities[0]->Back_up_Generator==1){echo 'checked'; }else{echo 'plain';} ?>">
                                            Back Up Generator
                                        </li>
                                        <li class="<?php if($amenities[0]->Garden==1){echo 'checked'; }else{echo 'plain';} ?>">
                                            Garden
                                        </li>

                                    </ul>
                                    <ul class="span2">
                                        <li class="<?php if($amenities[0]->Waterfront==1){echo 'checked'; }else{echo 'plain';} ?>">
                                            Waterfront
                                        </li>
                                        <li class="<?php if($amenities[0]->Ocean_View==1){echo 'checked'; }else{echo 'plain';} ?>">
                                            Ocean View
                                        </li>
                                        <li class="<?php if($amenities[0]->Security==1){echo 'checked'; }else{echo 'plain';} ?>">
                                            Security
                                        </li>
                                        <li class="<?php if($amenities[0]->Water_Tank==1){echo 'checked'; }else{echo 'plain';} ?>">
                                            Water Tank
                                        </li>

                                    </ul>
                                </div>

                                <!--
                                <h2>Map</h2>

                                <div id="property-map"></div> -->

                            </div>

                        </div>
                        <div class="sidebar span3">




            <div class="widget contact">
                <div class="title">
                    <h2 class="block-title">Contact Owner</h2>
                </div><!-- /.title -->

                <div class="content">
                    <form method="post" id="frm_contact_owner">

                        <div class="alert alert-success" id="co_sucess_message"  style="display: none">
                            <button type="button" class="close" data-dismiss="alert">×</button>
                            <strong>Message successfully send to owner!. <br/>Thank you!</strong>
                        </div>
                        <input type="hidden" name="co_property_id" value="<?php echo $dtl->property_id; ?>">

                        <div class="control-group">
                            <label class="control-label" >
                                Name
                                <span class="form-required" title="This field is required.">*</span>
                                <span class="form-required" id="co_name_error" title="This field is required."></span>
                            </label>
                            <div class="controls">
                                <input type="text" name="co_name" id="co_name">
                            </div><!-- /.controls -->
                        </div><!-- /.control-group -->

                        <div class="control-group">
                            <label class="control-label" >
                                Email
                                <span class="form-required" title="This field is required.">*</span>
                                <span class="form-required" id="co_email_error" title="This field is required."></span>
                            </label>
                            <div class="controls">
                                <input type="text"  name="co_email" id="co_email">
                            </div><!-- /.controls -->
                        </div><!-- /.control-group -->

                        <div class="control-group">
                            <label class="control-label" >
                                Message
                                <span class="form-required" title="This field is required.">*</span>
                                <span class="form-required" id="co_message_error" title="This field is required."></span>
                            </label>

                            <div class="controls">
                                <textarea name="co_message" id="co_message"></textarea>
                            </div><!-- /.controls -->
                        </div><!-- /.control-group -->

                        <div class="form-actions">
                            <input type="submit" id="co_send" class="btn btn-primary arrow-right" value="Send">
                             <img id="co_ajax_image" src="<?php echo base_url(); ?>html/assets/img/ajax_loader.gif" alt="" style="float: left; display:none">
                        </div><!-- /.form-actions -->
                    </form>
                </div><!-- /.content -->
            </div><!-- /.widget -->


            <div class="widget properties last">
                <div class="title">
                    <h2>Latest Properties</h2>
                </div><!-- /.title -->

                <div class="content">


                <?php foreach ($latest as $latest_pro) :?>

                    <div class="property">
                        <div class="image">
                            <?php
                            if($latest_pro->image){ 
                                $image_name = base_url().'upload_images/'.$latest_pro->image;
                            }else{
                                $image_name = base_url().'html/assets/img/tmp/property-small-5.png';
                            }
                            ?>
                            <a href="<?php echo base_url(); ?>property/view/<?php echo $latest_pro->property_id; ?>/<?php echo str_replace(' ', '_', $latest_pro->title); ?>"></a>
                            <img src="<?php echo $image_name; ?>"  alt="">
                        </div><!-- /.image -->

                        <div class="wrapper">
                            <div class="title">
                                <h3>
                                    <a href="<?php echo base_url(); ?>property/view/<?php echo $latest_pro->property_id; ?>/<?php echo str_replace(' ', '_', $latest_pro->title); ?>"><?php echo substr($latest_pro->title, 0, 16); ?>..</a>
                                </h3>
                            </div><!-- /.title -->
                            <div class="location"><?php echo $latest_pro->area; ?>, <?php echo $latest_pro->city; ?></div><!-- /.location -->
                            <div class="price">
                            <?php if($latest_pro->price == 0){ echo "negotiable"; }
                                  else {
                                    $price_text = (string)$latest_pro->price; // convert into a string
                                    $arr = str_split($price_text, "3"); // break string in 3 character sets
                                    $price_new_text = implode(",", $arr);  // implode array with comma

                                    echo 'Rs. '.$price_new_text.'/='; 

                                } ?>
                            </div><!-- /.price -->
                        </div><!-- /.wrapper -->
                    </div><!-- /.property -->

                <?php endforeach; ?>



                </div><!-- /.content -->
            </div><!-- /.properties -->
                        </div>
                    </div>
                </div>
            </div>
                </div><!-- /#content -->
            </div><!-- /#wrapper-inner -->

                <?php $this->load->view('footer'); ?>

            </div><!-- /#wrapper -->
            </div><!-- /#wrapper-outer -->

                <?php $this->load->view('footer_setting_palette'); ?>
                <?php //$this->load->view('property_filter_ajax'); ?>
                <?php $this->load->view('say_hello_ajax'); ?>
                <?php $this->load->view('property_contact_owner'); ?>

            </body>
            </html>

解决方案

我知道这完全不合逻辑,但请查看this...

我很久以前也有过同样的问题,但在头痛了一周后,我开始了解这个解决方案。

我从this链接获得了解决方案,请查看第二个答案。起初我也没有认真对待这个解决方案,但最终它对我起作用了:)

解决方案:

当您的代码运行两次时,请确保您的站点周围没有带有空src=""属性的标记或引用空url的任何css样式(如back:url();)。

相关文章