一次插入2个表格到数组中的mysql数据库

问题描述:

一个调用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

不是真的知道你的意思由

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