2013-03-13 91 views
0

一個調用Job,另一個調用Attributes。屬性表依賴於作業表,因爲一個表可以有很多屬性。該工作表中包含多個字段,即一次插入2個表格到數組中的mysql數據庫

作業ID(自動增加,主鍵),作業名,Jobdescription

屬性表包含以下字段

ID(自動遞增, PRIMARY KEY,AttribName,Score,Jobid(來自作業表的外鍵)

需要的屬性數每個作業輸入的內容會有所不同,因此一個作業可以有10個屬性,而另一個作業可能有2,3,4等屬性。

以下是我在卡住前創建的代碼。

Insert_job.php

<?php 

include 'html/head.php';//connect to connect to the database and initialize all functions 
include 'scripts/functions/init.php'; 
include 'html/page_title.php'; 
include 'html/top_menu.php'; 
include 'titles/Job_insert.php'; 

if(empty($_POST)=== false) 
    { 
     $R_fields = array('JobName','JobDesc','JobDuties','RecruitmentProcess','ContractType','SPackage'); 
     foreach($_POST as $key=>$value) 
     { 
      if (empty($value) && in_array($key,$R_fields)=== true) 
       { 
        $errors[] = 'fields marked with (*) are required'; 
        break 1; 
       } 


     } 

     if(empty($errors)=== true) 
      { 

       if($_POST['DirectorateName'] == '------ select ------') 
        { 
         $errors[] ='Please select Directorate Name'; 
        } 
       if($_POST['Attributes'] == '-select-') 
        { 
         $errors[] ='Please Select the Number of Attributes'; 
        } 

      } 
    } 

    include 'html/job_insert.php'; 

    //Check if the form is not empty then submit details 
    if(empty($_POST) === false && empty($errors)=== true) 
     { 
      //store input into the session variables 
      $_SESSION['JobName'] = $_POST['JobName']; 
      $_SESSION['JobDesc'] = $_POST['JobDesc']; 

      //Store the number of attributes to get captured 
      $_SESSION['Attributes'] = $_POST['Attributes']; 

      //redirect 
      header('Location: Rank.php'); 
      exit();                                
     } 
    else if(empty($errors) === false) 
     { 
      //output errors if the errors array is not empty 
      echo output($errors); 

     } 
?> 

         </div> <!-- div class entry ends here --> 
        </div> <!-- div post ends here --> 
       </div> <!-- div idcontents ends here --> 
       <!-- end #content --> 
       <?php 
        include 'html/top_side.php'; 
        include 'html/side_other.php'; 
        include 'html/side_bottom.php'; 
        include 'html/footer.php'; 
       ?> 

你會發現,我所有的輸入存儲到會話變量,並將其攜帶到下一個頁面,在這裏,我會一次插入一切都變成了兩個表。

以下代碼是用於Rank.php

​​

//輸出的形式來在屏幕 包括 '的HTML/Rank.php';

以下是如何我的用戶輸入存儲到一個數組

<form action = "" method ="POST" enctype="multipart/form-data"> 
         <fieldset> 
          <?php 

            if($Number == 10) 
             { 
              echo '<table border="0">'; 
              echo '<th>'.'Attribute'.'</th>'; 
              echo '<th>'.'Score'.'</th>'; 
              //Print the first row of the result set 
              echo '<tr>'; 
              //First Form 
               echo '<td>'.'<input type="text" size="35" name="attributes[]">'.'</td>'; 
               echo '<td>'.'<select id="select1" name ="Score[]"> 
                   <option>-select-</option> 
                   <option>10</option> 
                   <option>9</option> 
                   <option>8</option> 
                   <option>7</option> 
                   <option>6</option> 
                   <option>5</option> 
                   <option>4</option> 
                   <option>3</option> 
                   <option>2</option> 
                   <option>1</option> 
                  </select>'.'</td>';    
               echo '</tr>'; 

               //Second Form 
               echo '<tr>'; 
               echo '<td>'.'<input type="text" size="35" name="attributes[]">'.'</td>'; 
               echo '<td>'.'<select id="select2" name = "score[]""> 
                   <option>-select-</option> 
                   <option>10</option> 
                   <option>9</option> 
                   <option>8</option> 
                   <option>7</option> 
                   <option>6</option> 
                   <option>5</option> 
                   <option>4</option> 
                   <option>3</option> 
                   <option>2</option> 
                   <option>1</option> 
                  </select>'.'</td>';    
                echo '</tr>'; 

基本上我存儲用戶輸入到陣列稱爲屬性和另一個陣列稱爲分數的示例。將作業詳細信息插入作業表中工作正常,但現在我需要將Attributes和Score數組中包含的信息插入到mysql數據庫中。請協助

+0

這段代碼有太多問題,很難提供幫助。首先:在將數據插入數據庫之前對輸入進行清理。不要在腳本中包含代碼。準備你的圖書館並加入他們。嘗試從php邏輯中分離html輸出。 – Ghigo 2013-03-13 11:44:13

+0

我有一個函數用來清理輸入,但我用不同的腳本來完成,然後調用這個腳本的函數。 – user1783675 2013-03-13 11:46:24

回答

0

不是真的知道你的意思由

//This is where I am getting stuck 

但你基本上要插入的屬性爲已創建的作業。所以你可以遍歷屬性列表併爲其構建查詢。

$query = 'INSERT INTO attributes 
    (name, score, job_id) 
VALUES'; 

foreach ($_SESSION['Attributes'] as $attribute) { 
    $query .= "({$attributep['name']}, {$attribute['score']}, $job_id),"; 
} 

執行該查詢就像在job_insert中執行那樣。

+0

是的,這更像是我想要做的事情。有可能你可以解釋foreach循環中的代碼。這會自動插入屬性和分數數組中包含的所有值嗎? – user1783675 2013-03-13 11:53:46

+0

其實這個工程,我只是試了一下 – user1783675 2013-03-13 12:40:32

+0

它應該從可用的所有數組元素構建查詢。很高興聽到它的作品! – tlenss 2013-03-13 13:08:48