2011-11-20 56 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