Codeigniter Noob问题

时间:2011-11-20 17:04:32

标签: php codeigniter

我是CI中的菜鸟,我无法在数据库中插入数据,这是我所做的全部,它没有显示错误,但我没有得到结果,

admin.php(控制器)

public function postnews(){

        $ip = $_SERVER['REMOTE_ADDR'];
            if ($ip == "my ip address"){

                session_start();

                if (!isset($_SESSION['admin'])){
                    $this->load->view('admin-login');
                    die(0);
                    }

                if ($_SESSION['admin'] != 'loged'){
                    $this->load->view('admin-login');
                    die(0);
                    }

                if ($_SESSION['admin'] == 'loged'){

                    if (isset($_POST['title'])and isset($_POST['main-poster']) and isset($_POST['type']) and isset($_POST['year']) and isset($_POST['language'])and isset($_POST['platform'])and isset($_POST['publisher'])and isset($_POST['size'])and isset($_POST['graphics'])and isset($_POST['little-info'])and isset($_POST['full-info'])and isset($_POST['posters'])and isset($_POST['screenshots'])and isset($_POST['trailers'])and isset($_POST['gameplays'])and isset($_POST['author'])){

                        $title = $_POST['title'];
                        $main_poster = $_POST['main-poster'];
                        $type = $_POST['type'];
                        $year = $_POST['year'];
                        $language = $_POST['language'];
                        $platform = $_POST['platform'];
                        $publisher = $_POST['publisher'];
                        $size = $_POST['size'];
                        $graphics = $_POST['graphics'];
                        $little_info = $_POST['little-info'];
                        $full_info = $_POST['full-info'];
                        $posters = $_POST['posters'];
                        $screenshots = $_POST['screenshots'];
                        $trailers = $_POST['trailers'];
                        $gameplays = $_POST['gameplays'];
                        $autor = $_POST['author'];
                        $date = date("d.m.Y");

                        $this->load->model('Gamesmodel');
                        echo $this->Gamesmodel->PostArticle($title, $main_poster, $type, $year, $language, $platform, $publisher, $size, $graphics, $little_info, $full_info, $posters, $screenshots, $trailers, $gameplays, $autor, $date);

                    }else{
                        $this->load->view('postnews');
                    }

                }

            } else {
                $this->load->view('404.htm');
                die(0);
            }

    }

gamemodel.php模型

<?php
class Gamesmodel extends CI_Model {

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }

    function PostArticle($title, $main_poster, $type, $year, $language, $platform, $publisher, $size, $graphics, $little_info, $full_info, $posters, $screenshots, $trailers, $gameplays, $autor, $date)
    {
        $sql = "INSERT INTO game-articles (id, title, type, year, language, platform, publisher, size, graphics, little-info, full-info, posters, screenshots, trailers, gameplays, date, author) VALUES ('' ,".$this->db->escape($title).",".$this->db->escape($main_poster).",".$this->db->escape($type).",".$this->db->escape($year).",".$this->db->escape($language).",".$this->db->escape($platform).",".$this->db->escape($publisher).",".$this->db->escape($size).",".$this->db->escape($graphics).",".$this->db->escape($little_info).",".$this->db->escape($full_info).",".$this->db->escape($posters).",".$this->db->escape($screenshots).",".$this->db->escape($trailers).",".$this->db->escape($gameplays).",".$this->db->escape($date).",".$this->db->escape($author).")";
        $this->db->query($sql);
        return $this->db->affected_rows();
    }
}

postnews.php view

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Post News</title>
</head>
<body style="background-color:black;">
    <div style="margin:auto auto auto auto; width:800px; background-color:white; padding-top:20px; padding-bottom:20px; text-align:center;">
        <form action="http://www.gameslib.net/admin/postnews" method="post"><br />
            <input type="text" placeholder="title" name="title" style="width:300px;" /><br />
            <input type="text" placeholder="main poster" name="main-poster" style="width:300px;" /><br />
            <input type="text" placeholder="type" name="type" style="width:300px;" /><br />
            <input type="text" placeholder="year" name="year"/><br />
            <input type="text" placeholder="language" name="language" style="width:300px;" /><br />
            <input type="text" placeholder="platform" name="platform" style="width:300px;" /><br />
            <input type="text" placeholder="publisher" name="publisher" style="width:300px;" /><br />
            <input type="text" placeholder="size" name="size"/><br />
            <input type="text" placeholder="graphics" name="graphics" style="width:300px;" /><br />
            <textarea name="little-info" placeholder="little-info" style="width:600px; height:100px;" ></textarea><br />
            <textarea name="full-info" placeholder="full-info" style="width:600px; height:200px;" ></textarea><br />
            <textarea name="posters" placeholder="posters" style="width:600px; height:50px;" ></textarea><br />
            <textarea name="screenshots" placeholder="screenshots" style="width:600px; height:50px;" ></textarea><br />
            <textarea name="trailes" placeholder="trailes" style="width:600px; height:50px;" ></textarea><br />
            <textarea name="gameplays" placeholder="gameplays" style="width:600px; height:50px;" ></textarea><br />
            <input type="text" placeholder="author" name="author" /><br />
            <input type="submit" value="P O S T"/><br />
            <input type="reset" value="reset"/><br />
        </form>
    </div>
</body>
</html>

请帮助我,我复制了所有的东西,以保证我不会忽视某些事情,

1 个答案:

答案 0 :(得分:2)

好的,我们先来清理代码吧。您无需在if ($_SESSION['admin'] == 'loged')方法中创建每个自变量,而是可以使用函数extract();extract()方法为提供的数组中的每个键创建一个变量。假设您在数组name中有密钥$_POST,则extract方法将为您创建名为name的变量。要检索该值,您只需访问变量$name

if ($_SESSION['admin'] == 'loged'){

    extract($_POST);

}

其次,如果要在if语句中检查多个内容,则不要使用单词and,而是使用以下操作数'&amp;&amp;'。

if (isset($_POST['title']) && isset($_POST['main-poster']) && isset($_POST['type']) && isset($_POST['year']) && isset($_POST['language']) && isset($_POST['platform']) && isset($_POST['publisher']) && isset($_POST['size']) && isset($_POST['graphics']) && isset($_POST['little-info']) && isset($_POST['full-info']) && isset($_POST['posters']) && isset($_POST['screenshots']) && isset($_POST['trailers']) && isset($_POST['gameplays']) && isset($_POST['author']))

不是手动检查是否已在$_POST数组中设置每个对象,而是可以遍历$_POST

创建需要设置的变量数组:

$req_fields = array(

    'title', 
    'main-poster', 
    'type', 
    'year', 
    'language', 
    'platform', 
    'publisher', 
    'size', 
    'graphics', 
    'little-info', 
    'full-info', 
    'posters', 
    'screenshots', 
    'trailers', 
    'gameplays', 
    'author'

);

然后为尚未设置的元素创建一个数组:

$notset = array();

最后,迭代$_POST检查是否设置了每个值。如果没有,请将其添加到数组中。

foreach ($req_fields as $key) {

    if (!isset($_POST[$key]) {

        $notset[] = $key;

    }
}

然后检查是否有任何值未设置并重定向用户,否则,加载模型并回显帖子:

if (count($notset) > 0) {

    $this->load->view('postnews');

}
else {

    $this->load->model('Gamesmodel');
    echo $this->Gamesmodel->PostArticle($title, $main_poster, $type, $year, $language, $platform, $publisher, $size, $graphics, $little_info, $full_info, $posters, $screenshots, $trailers, $gameplays, $autor, $date);

}

推测插入不起作用的实际原因是因为它实际上并未被调用。这背后的原因是某些键实际上没有设置。

遍历$notset数组以查看是否是这种情况:

foreach ($notset as $unsetField) {

    echo "Field {$unsetField} is not set. <br />";

}