在 php 中获取 2 个表不工作

Fetch 2 tables in php not working

我必须获取外键的所有值。它是这样的: 我有两个 tables:Subjects 和 Careers -> "Subjects" includes (id, careers_id (是 table Careers 的 "id" 列的外键)名称,描述、小时数)"Careers" 包括(id、名称、描述)

首先,我要展示一个 table,其中包含以下内容: Final_table 但是,在那 table 中,我自己写了科目(微积分、物理学、生物学),这是错误的。我应该从 table(学科和职业)中获取该信息。而且我还需要添加新主题 - 我的意思是当我点击 "Add new subject" 它会转到另一个页面并且从选项 "Subjects" 必须有一个 slider/select 与来自 table。 所以: 1)我需要从外键中获取信息 2)当我想添加一个新主题时,我需要 select 职业

这是我的代码(这个代码创建了一个 table,但它没有从 table 职业中获取信息):

</head>
<body>
<a href="estudiante.php">ADD NEW SUBJECT</a><br /><br />
    <h2 align="center">TABLE:SUBJECTS</h2>
        <table align="center" border="1" cellspacing="0" cellpadding="0" width="700">
            <thead>
                <th>id</th>
                <th>Career</th>
                <th>Name</th>
                <th>Description</th>
                <th>Hours</th>
                <th>Action</th>

            </thead>

            <?php
            $sql=mysql_query("SELECT * FROM subjects");
            $i=1;
                while($row=mysql_fetch_array($sql)){
                    echo "<tr>
                            <td>".$i."</td>
                            <td>".$row['careers_id']."</td>
                            <td>".$row['name']."</td>
                            <td>".$row['description']."</td>
                            <td>".$row['hours']."</td>
                            <td align='center'>
                                <a href='editar.php?editar=1&iden=".$row['id']."'>UPDATE</a> |
                                <a href='borrar.php?borrar=1&iden=".$row['id']."'>DELETE</a>
                            </td>
                    </tr>";
                    $i++;

                }
            ?>

        </table>    

</body>

而这段代码是添加一个新主题:

<?php include('connect.php'); 
$error="";

if(isset($_POST['btnsave']))
{
    $carreras_id=$_POST['txtcarreras_id'];
    $nombre=$_POST['txtnombre'];
    $descripcion=$_POST['txtdescripcion'];
    $carga_horaria=$_POST['txtcarga_horaria'];


    if($_POST['txtid']=="0")
    {

        $a_sql=mysql_query("INSERT INTO subjects VALUES('','$carreras_id','$nombre','$descripcion','$carga_horaria')");
        if($a_sql)
        {

            header("location:index.php");

        }


    }else{

        echo "Actualizar";
    }

}

?>

        <h2 align="center">ADD NEW SUBJECT</h2>
        <form method="Post">
            <table align="center">
                <tr>    
                    <td>Career:</td>
                    <td><input type='text' name='txtcarreras_id'/><input type="hidden" name="txtid" value="0" /></td>

                </tr>
                <tr>    
                    <td>Name:</td>
                    <td><input type='text' name='txtnombre'/></td>

                </tr>
                <tr>    
                    <td>Description:</td>
                    <td><input type='text' name='txtdescripcion'/></td>

                </tr>
                <tr>    
                    <td>Hours:</td>
                    <td><input type='text' name='txtcarga_horaria'/></td>

                </tr>
                <tr>    
                    <td></td>
                    <td><input type='submit' value=save name='btnsave'/></td>

                </tr>
            </table>


        </form>

拜托,我真的不知道该怎么办:/

改变这个

 $sql=mysql_query("SELECT * FROM subjects");

到这个

$sql=mysql_query("SELECT * FROM subjects s INNER JOIN careers c ON s.careers_id = c.id");

您需要在查询

中添加一个join

新主题

<form method="Post">
            <table align="center">
                <tr>    
                    <td>Career:</td>
                    <td><input type='text' name='txtcarreras_id'/><input type="hidden" name="txtid" value="0" /></td>

                </tr>
                <tr>    
                    <td>Name:</td>
                    <td><input type='text' name='txtnombre'/></td>

                </tr>
                <tr>    
                    <td>Description:</td>
                    <td><input type='text' name='txtdescripcion'/></td>

                </tr>
                <tr>    
                    <td>Hours:</td>
                    <td><input type='text' name='txtcarga_horaria'/></td>

                </tr>
                <tr>    
                    <td></td>
                    <td><input type='submit' value=save name='btnsave'/></td>

             </tr>
           <tr>
               <select name="new_sub">
                   <?php foreach(){?> <option value="<?php echo $row['careers_id'] ?>"><?php echo $row['name']; ?></option> <?php } ?>
               </select>
           </tr>
       </table>


    </form>

要从 2 个表中获取值,您可能需要使用 SQL JOIN clause

改变这个:

SELECT * FROM subjects

以下:

SELECT subjects.name,subjects.hours,careers.description...(choose the columns you want) FROM subjects INNER JOIN careers ON subjects.careers_id=careers.id;

如果你想制作一个包含所有职业的下拉菜单:

all_careers = mysql_query(get all the careers and corresponding career_id from the table);

然后在你的表单中你可以在 'dropdown'-html 标签之间有一些 php,像这样:

    <select>

    <?php
    here go through all_careers(You can use foreach -construct) and on each cycle print out new career option:
    echo "<option value=\"".[career_id here]."\">".[career name here]."</option>";
    ?>
    </select>