【问题标题】:How to check if row exists on database CodeIgniter如何检查数据库 CodeIgniter 上是否存在行
【发布时间】:2019-03-28 19:07:30
【问题描述】:

所以我在使用这个查询时遇到了一些麻烦,因为这是第一次我不是检查一行中的值,而是检查数据库中是否存在一行本身。

这是我面临的问题:

更新:

public function individualDiscountVerification($id){

        // Get postID
        $data['item']           =   $this->PublicStore_model->readPostID($id);
        $data['relationship']   =   $this->PublicStore_model->getPostRelationship($id);

        // Get Last Post ID
        $postID         =   $id;
        $activityTitle  =   $data['item']->title;

        // Verify Data
        $itemInCart = $this->PublicCart_model->verifyUserCartItem($postID);

        // Redirect if row does not exist -- HERE IS WHERE I NEED HELP
        if(!$itemInCart){

            // Set message
            $this->session->set_flashdata('error', 'You first need to add it to you cart');

            // Redirect
            redirect('store/read/'.$id);


        // Redirect if seller is the same as the current userID
        } elseif($this->session->userdata('user_id') == $data['relationship']->user_id) {

            // Set message
            $this->session->set_flashdata('error', 'You can not add discounts to your own products');

            // Redirect
            redirect('store/read/'.$id);

        } else {

            // Verify discount exists
            $discount       =   $this->input->post('discount_code');
            $discountCode   =   $this->PublicCart_model->verifySingleDiscount($discount);

            if(!$discountCode){

                // Set message
                $this->session->set_flashdata('error', 'The discount has expired or is invalid');

                // Redirect
                redirect('store/read/'.$id);

            } else {

                // Get Last ID Data
                $discountID     =   $discountCode->discount_id;

                // Get Discount Type
                $discountType   =   $this->PublicCart_model->getIndividualDiscountID($discountID);

                // Verify data
                $verifyItemDiscount =   $this->PublicCart_model->verifySingleItemDiscountCode($postID, $discountID);

                if($discountType->type == 'percent' && $verifyItemDiscount != NULL?:'' && $discountID == $data['relationship']->discount_id){

                    // Update Post Array
                    $postData   =   array(
                        'price'         =>  round($data['relationship']->price * ((100 - $discountCode->amount) / 100), 2),
                        'discount_id'   =>  $discountID,
                    );
                    // Update Post Array
                    $this->PublicCart_model->updateUserCartItem($postID, $postData);

                    // Set message
                    $this->session->set_flashdata('success', 'A percent discount type has been applied to your final price');

                    // Redirect
                    redirect('store/read/'.$data['item']->post_id);

                } elseif($discountID != $data['relationship']->discount_id){

                    // Set message
                    $this->session->set_flashdata('error', 'Percent discount is not attached to this product');

                    // Redirect
                    redirect('store/read/'.$data['item']->post_id);

                } elseif($discountType->type == 'float' && $verifyItemDiscount != NULL?:'' && $discountID == $data['relationship']->discount_id){

                    // Update Post Array
                    $originalprice  =   $data['relationship']->price;
                    $amount = $discountCode->amount;
                    $postData   =   array(
                        'price'         => $originalprice - $amount,
                        'discount_id'   =>  $discountID,
                    );
                    // Update Post Array
                    $this->PublicCart_model->updateUserCartItem($postID, $postData);

                    // Set message
                    $this->session->set_flashdata('success', 'A float discount type has been applied to your final price');

                    // Redirect
                    redirect('store/read/'.$data['item']->post_id);

                } elseif($discountID != $data['relationship']->discount_id){

                    // Set message
                    $this->session->set_flashdata('error', 'Float discount is not attached to this product');

                    // Redirect
                    redirect('store/read/'.$data['item']->post_id);
                }

                // Activity Array
                $activityData   =   array();
                // Insert Activity
                $this->Activity_model->add($activityData);

            }
        }
    }

这是模型中的方法:

更新:

  /*
  *
  * IS IN USER'S CART? -- FROM HERE AND BELOW IS FOR THE INDIVIDUAL ITEMS DISCOUNT
  *
  */
  public function verifyUserCartItem($postID){
    $query = $this->db->get($this->relationship, array(
      'post_id'   =>  $postID,
      'friend_id' =>  $this->session->userdata('user_id'),
      'type'      =>  $this->cartType,
    ));

    if($query->num_rows() > 0){
      return $query->row_array();
    } else {
      return null;
    }

  }
  /*
  *
  * DOES THE DISCOUNT EXISTS?; IF YES, WHAT TYPE OF DISCOUNT IT IS?
  *
  */
  public function verifySingleDiscount($discount){
    $query  =  $this->db->get_where($this->discounts, array(
      'code'  =>  $discount,
    ));
    return $query->row();
  }

  public function getIndividualDiscountID($discountID){
    $query  = $this->db->get_where($this->relationship, array(
      'discount_id' =>  $discountID,
      'status'      =>  $this->published,
    ));
    return $query->row();
  }
  /*
  *
  * IS THE DISCOUNT ATTACHED TO THE POST?; IF YES, UPDATE IT
  *
  */
  public function verifySingleItemDiscountCode($postID, $discountID){
    $query  = $this->db->get_where($this->relationship, array(
      'post_id'     =>  $postID,
      'discount_id' =>  $discountID,
      'status'      =>  $this->published,
    ));
    return $query->row();
  }

  public function updateUserCartItem($postID, $postData){
    $this->db->select('*');
    $this->db->where('friend_id', $this->session->userdata('user_id'));
    $this->db->where('post_id', $postID);
    $this->db->where('type', $this->cartType);
    $this->db->update($this->relationship, $postData);
  }

注意:如您所见,我已经在使用 isset,但这会弄乱以下代码块(如果需要,我将放置整个函数),这些代码块未在此处发布;我已经使用了if emptyif var === false,但我仍然遇到同样的错误。

【问题讨论】:

  • 同样的错误是什么?
  • 这不是完全相同的错误,但我只是想检查行是否存在,否则将我重定向到特定页面,但如果我使用 isset() 函数,它会检索错误我想要,但它与以下代码块混淆了。你想让我编辑问题并放入整个控制器功能吗?
  • 试过但没用,它现在跳过检查购物车项目是否已经在数据库中。

标签: php codeigniter codeigniter-3


【解决方案1】:

verifyUserCartItem 函数中尝试以下操作:

if($query->num_rows() > 0){
  return $query->row_array();
} else {
  return null;
}

if(!$itemInCart){ 而不是if(isset($itemInCart)){

【讨论】:

  • 不工作,如果我这样做,它会“跳过”检查该项目是否已经在购物车中.....再次检查我更新的代码以了解我的意思。跨度>
  • 我不明白。如果$itemInCartnull,则表示数据库中没有对应的行。所以重定向。这不是你想要的吗?
  • 是的,这正是我想要检索“您首先需要添加到您的购物车”的消息,它实际上是检索错误消息“此产品未附加百分比折扣”应该在检查购物车项目是否在用户的购物车中时显示....而不是当用户的购物车中没有购物车项目时....我对此感到头疼,感谢您的帮助。
  • 所以表示$query->num_rows()大于0。建议你做echo $query->num_rows()来保证你的数据是否正确。
【解决方案2】:

例如:

var_dump($this->db->where('id', $id)->get('TABLE_NAME')->num_rows());

【讨论】:

    猜你喜欢
    • 2019-08-16
    • 2011-05-16
    • 1970-01-01
    • 2012-06-08
    • 1970-01-01
    • 2010-10-24
    • 2012-10-15
    相关资源
    最近更新 更多