2011-11-20 65 views
2

我在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视图

<!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> 

请帮助我,我把所有的东西都复制到了我不会忽略的东西上,

+0

而不是设置每个'$ _POST'关键各自变量,你应该使用'提取物($ _ POST);' –

+0

我不知道你的意思,我写到我是小白,你能给我一个例子吗? – Irakli

+0

我做过了吗?重新阅读我的评论。另外,将所有'和'改为'&&' –

回答

2

好的,让我们先清理你的代码。您可以使用功能extract();,而不必在您的if ($_SESSION['admin'] == 'loged')方法中创建每个自变量。 extract()方法为提供的数组中的每个键创建一个变量。假设您在数组$_POST中有关键字name,则抽取方法将为您创建一个名为name的变量。要检索该值,您只需访问变量$name

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

    extract($_POST); 

} 

其次,如果你想检查多件事情在if语句中不使用这个词and,您使用以下操作数“& &”。

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 />"; 

} 
+0

,请在代码中保留AND,谢谢,非常感谢您再次感谢您检查我的代码。我相信其他具有相同问题的“noobs”会很高兴看到答案。 – Irakli