【问题标题】:Part of the code gets skipped部分代码被跳过
【发布时间】:2014-05-21 17:47:23
【问题描述】:

我正在尝试编写自己的小博客。 为此,我使用 CodeIgniter 框架。

在我的博文中,我还想保存一个类别和一些关键字。
我需要能够检查一个类别/关键字是否已经存在,如果不是需要创建一个新的。
现有/新的 ID 被保存以在“连接”表中使用。

我有以下数据库设置。

帖子 - id (AI)、标题、日期、内容
类别 - id (AI)、类别
post_categories - 帖子、类别
关键字 - id (AI)、关键字
post_keywords - 帖子、关键字
*AI = 自动增量

表格帖子、post_categories 和 post_keywords 得到更新,表格类别和关键字保持为空。连接表中的类别和关键字值不正确。

在对代码进行故障排除时,我在每个步骤中都回显了每个变量。
由于某种原因,整个 if/else 结构检查一个类别或关键字是否已经存在被跳过。

/**
 * Insert post into database
 * Check if category already exists, if not add it
 * Check if keywords already exist, if not add them
 * Insert post-category and post-keyword links
 * @param array $data
 * @return boolean success
 */
function create($data) {
    print_r($data);

    try {
        // posts
        $this->db->insert('posts', array(
            'title' => $data['title'],
            'date' => $data['date'],
            'content' => $data['content']
        ));

        $postId = $this->db->insert_id();

        echo '<p>Inserted new post (' . $data['title'] . ') at ' . $postId . '</p>';

        // category

        $category = $data['category'];

        $query = $this->db->get_where('categories', array('category' => $category));

        $cat = 666;
        if ($this->db->count_all_results() == 1) {
            foreach ($query->result() as $row) {
                $cat = $row->id;
                echo '<p>cat = ' . $cat . '</p>';
            }
        } else {
            $this->db->insert('categories', array(
                'category' => $category
            ));
            $cat = $this->db->insert_id();
            echo '<p>cat = ' . $cat . '</p>';
        }

        echo '<p>Inserted new category (' . $category . ') at ' . $cat . '</p>';
        if($cat == 666) { echo ':('; }

        $this->db->insert('post_categories', array(
            'post' => $postId,
            'category' => $cat
        ));

        // keywords

        $keywords = $data['keywords'];

        foreach ($keywords as $keyword) {
            $query = $this->db->get_where('keywords', array('keyword' => $keyword));

            $key = 666;
            if ($this->db->count_all_results() == 1) {
                foreach ($query->result() as $row) {
                    $key = $row->id;
                    echo '<p>key = ' . $key . '</p>';
                }
            } else {
                $this->db->insert('keywords', array(
                    'keyword' => $keyword
                ));
                $key = $this->db->insert_id();
                echo '<p>key = ' . $key . '</p>';
            }

            echo '<p>Inserted new keyword (' . $keyword . ') at ' . $cat . '</p>';
            if($key == 666) { echo ':('; }

            $this->db->insert('post_keywords', array(
                'post' => $postId,
                'keyword' => $key
            ));
        }

        return true;
    } catch (Exception $e) {
        print '<p>ERROR:</p>' . $e;

        return false;
    }
}

也在我的控制器中

echo '<p>' . ($this->m_posts->create($post) ? 'no errors :)' : 'error! :(') . '</p>';

结果

Array ( [title] => Foo Bar [date] => 2014-05-22 [content] => lorum ipsum dolore si amet [category] => Category [keywords] => Array ( [0] => Some [1] => Keywords ) )
Inserted new post (Foo Bar) at 2

Inserted new category (Category) at 666
:(

Inserted new keyword (Some) at 666
:(

Inserted new keyword (Keywords) at 666    
:(

no errors :)

在我的数据库中

POSTS

stdClass Object ( [id] => 1 [title] => Title [date] => 2014-05-22 [content] => okokokokokokok ) 
stdClass Object ( [id] => 2 [title] => Foo Bar [date] => 2014-05-22 [content] => lorum ipsum dolore si amet ) 

CATEGORIES

POST_CATEGORIES

stdClass Object ( [post] => 1 [category] => 666 ) 
stdClass Object ( [post] => 2 [category] => 666 ) 

KEYWORDS

POST_KEYWORDS

stdClass Object ( [post] => 1 [keyword] => 666 ) 
stdClass Object ( [post] => 2 [keyword] => 666 ) 
stdClass Object ( [post] => 2 [keyword] => 666 ) 

注意:我使用 99 作为临时值来测试变量是否被填充,因为我首先认为变量的值没有离开 if/else 的范围。但是在 if/else 中回显变量的值不会打印任何内容。它似乎跳过了整个 if/else 部分。

【问题讨论】:

  • 它并没有插入到 $cat = 99 的 post_categories 中?
  • @Stefan 是的,它使用临时值 (99)
  • 那么 $cat = $row()-&gt;id; 不应该是 $cat = $row-&gt;id; 吗?你能在那里做一个print_r($row); exit; 看看你是否最终到达那里?
  • @Stefan Damn,可能是我错过了,我重新排列了一些代码。今天晚些时候我会看看。我会及时通知你的!
  • @Stefan 这确实是一个错误,但那条线甚至没有被触及。这就是为什么我没有收到任何错误。用代码和结果中的一些编辑(回声)更新了我的帖子

标签: php mysql database codeigniter


【解决方案1】:

将代码调整为:

  • 在if语句中查询它自己的num_rows
  • 如果您希望有 1 条记录,请直接执行 $query-&gt;row() 而不是 foreach 循环。

    $query = $this->db->get_where('categories', array('category' => $category));
    
    $cat = 666;
    if ($query->num_rows() == 1) {
        $row = $query->row(); 
        $cat = $row->id;
        echo '<p>cat = ' . $cat . '</p>';
    } else {
        $this->db->insert('categories', array(
            'category' => $category
        ));
        $cat = $this->db->insert_id();
        echo '<p>cat = ' . $cat . '</p>';
    }
    

【讨论】:

  • 这似乎成功了。非常感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 2014-10-21
  • 1970-01-01
  • 2020-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-18
相关资源
最近更新 更多