从共享一个ID的两个表中提取数据

时间:2014-12-28 03:24:13

标签: php database mysqli

我有两张桌子

project_titles
    projectTitleID
    projectID
    projectTitle

project_steps
    projectStepID
    projectID
    projectStepDesc

如果我必须将其插入数据库:

Project Title: This is the Step 1 Title
Project Step: This is the Step 1 Description

它会像这样进入数据库:

project_titles表中的

projectTitleID=1
projectID=4(this number is from the mysqli_insert_id)
projectTitle=This is the Step 1 Title
project_steps表中的

projectStepID=1
projectID=4 (this number is from the mysqli_insert_id)
projectStepDesc=This is the Step 1 Description

一切都进入数据库很好,所有数据都保存在各自的表中....但是当我尝试从数据库中提取数据并将projectTitle与正确的projectStepDesc匹配时,所有内容都是重复的(如果我有3个)标题和3个步骤,我结束了从数据库中提取的9个项目每个步骤3个标题)...这是我用来从数据库中重新保存数据的mysqli命令:

$sql = "SELECT project_titles.projecttitleID, project_titles.projectID, project_titles.projectTitle, project_steps.projectID, project_steps.projectStepDesc from project_titles INNER JOIN project_steps on project_titles.projectID=project_steps.projectID where project_titles.projectID=$ID ";

ID是主项目页面的$ _GET变量....

我需要它输出如下:

Step 1 Title
    Step 1 Description
Step 2 Title
    Step 2 Description

但是我用上面的sql语句得到了这个......

Step 1 Title
    Step 1 Description
Step 2 Title
    Step 1 Description
Step 3 Title
    Step 1 Description

我在这里做错了什么....我知道这是我的$ sql声明......但不知道如何纠正它...你能帮忙吗?

以下是我用于获取数据的代码:

//get the project steps          
      $projSteps = "SELECT project_titles.projecttitleID, project_titles.projectID, project_titles.projectTitle, project_steps.projectID, project_steps.projectStepDesc from project_titles INNER JOIN project_steps on project_titles.projectID=project_steps.projectID where project_titles.projectID=$ID";  
    $results1 = mysqli_query($con,$projSteps);  
    if (!$results1) {
printf("Error: %s\n", mysqli_error($con));
exit();
}   
       if (mysqli_num_rows($results1) > 0){
        echo '<div class="well well-sm">';
        echo '<h2>Project Steps</h2>';  
        echo '</div>';         
        $a=1;
 while ($rows = mysqli_fetch_assoc($results1))
    {         
      echo "<p><strong>Step ". $a++ .":&nbsp;&nbsp; ".$rows['projectTitle']."</strong></p>";          
      $psteps=$rows['projectStepDesc'];
      echo '<div id="steps">';
      $purifier = new HTMLPurifier();
      $clean_html1 = $purifier->purify($psteps);
      echo "$clean_html1";
      echo '</div>';
      }}//end project steps

0 个答案:

没有答案