2013-12-13 32 views
0

我有一個論壇,用戶可以輸入他們正在尋找的工作,這將提交到數據庫,然後顯示在下一頁。只有我無法獲取任何數據才能上傳,我不知道爲什麼。作業資料不提交到數據庫表

我也在努力尋找錯誤檢查的方法。有任何想法嗎?

// Check for job submission 
     if(isset($_POST['submit'])) 

      //empty error array 
      $error = array(); 

     // check for a things in the field 

     if(empty($_POST['job'])) 
     { 
      $error[] = 'Please fill in all required fields'; 

     } 

     // iff there are no errors, insert the job listing into the database. 
     // otherwies, display error. 
     if(sizeof($error) == 0) 
     { 
      // insert job listing 
      $query = "INSERT INTO job (
           job_id, 
           user_id, 
           jobtitle, 
           company, 
           location, 
           summary, 
           responsibilities, 
           skills 
           ) VALUES (
            null, 
            '{$_SESSION['user_id']}', 
            '{$_POST['jobtitle']}', 
            '{$_POST['company']}', 
            '{$_POST['location']}', 
            '{$_POST['summary']}', 
            '{$_POST['responsibilities']}', 
            '{$_POST['skills']}', 
            NOW() 
            )"; 

      $result = mysqli_query($dbc, $query) or die('Query failed: ' . mysqli_error($dbc)); 

      // display a confirmation 
      echo "<div class=\"alert alert success\">Your job listing has been added</div>"; 

     } else { 

      // display error message 
      foreach($error as $value) 
      { 
       echo "<div class=\"alert alert-danger\"{$value}</div>"; 
      } 
     } 

     ?> 

     <!-- Job listing Form --> 
     <form method="post" action="listings.php"> 
      <div class="form-group"> 
       <label> Job Title </label> 
       <input name ="jobtitle" type="text" class="jobform"/> 

       <label>Company/Organization</label> 
       <input name="company" type="text" class="jobform"/> 

       <label> Job Location </label> 
       <input name ="location" type="text" class="jobform"/> 

       <label> Job Responsibilities </label> 
       <textarea name="summary" rows="8" cols="20" class="jobfourm"></textarea> 

       <label> Job Responsibilities </label> 
       <textarea name="responsibilities" rows="8" cols="20" class="jobfourm"></textarea> 

       <label> Job Skills </label> 
       <textarea name="skills" rows="8" cols="20" class="jobforum"></textarea> 

      </div> 

     <div class="form-group"> 
        <input name="submit" type="submit" value="Submit" class="btn btn-large btn-primary" /> 
       </div> 

     </form> 

    </div> 
+2

*旁註:*您的代碼受到SQL注入攻擊,因爲您直接允許將POST值插入到您的查詢中。 – Raptor

+0

@ Raptor我知道,我有點還在學習它,所以爲了這個練習,我更加努力地在輸入代碼以防止注入之前弄清基本工作代碼 –

+0

@ChristineAustin這是一個明智的方法,但它永遠不會早期提供良好的安全:) – Zarathuztra

回答

1

我的賭注是你的查詢:

(
           job_id, 
           user_id, 
           jobtitle, 
           company, 
           location, 
           summary, 
           responsibilities, 
           skills 
           ) VALUES (
            null, 
            '{$_SESSION['user_id']}', 
            '{$_POST['jobtitle']}', 
            '{$_POST['company']}', 
            '{$_POST['location']}', 
            '{$_POST['summary']}', 
            '{$_POST['responsibilities']}', 
            '{$_POST['skills']}', 
            NOW() 

什麼應該JOB_ID,你傳遞無效。現在,我假設所有的工作都必須有工作編號,對嗎?你需要實際傳遞一個有效的ID,因爲我打賭錢(或代表),這是表中的一個不可空字段。另外,您已經在您的列名稱參數中未聲明的值中添加了一列。

+0

你說得對,我沒有通過'NOW(')'這個與'NOW()'相關的事情,我想這打破了一切。非常感謝! :] –

+0

沒問題。這總是我們錯過的那些微小的小事;) – Zarathuztra