php - 在 php 中获取 2 个表不起作用

标签 php mysql mysqli fetch

我必须获取外键的所有值。事情是这样的: 我有两个表:主题和职业 ->“主题”包括(id,careers_id(是表职业的“id”列的外键)名称,描述,小时)“职业”包括(id,名称,描述)

首先,我必须显示一个包含以下内容的表格: Final_table 但是,在该表中,我自己写了科目(微积分、物理、生物学),这是错误的。我应该从两个表(主题和职业)中获取该信息。我还需要添加新主题 - 我的意思是,当我单击“添加新主题”时,它会转到另一个页面,并且从“主题”选项中必须有一个 slider /选择表中的所有职业。 所以: 1)我需要从外键中获取信息 2)当我想添加新科目时,我需要选择职业

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

</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");

您需要在查询中添加联接

新主题

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

关于php - 在 php 中获取 2 个表不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42878245/

相关文章:

php - 编译错误 : Cannot declare class because the name is already in use

php - 在类里面执行 SELECT 时无效的语句/准备

PHP MySQLi - WHERE "calculated value is smaller than"不工作

php - mysqli_fetch_array while 循环列

php - 一键更新 mySQL 数据库

php - 如何在 php 中将公历日期转换为伊斯兰日期并将其存储在数据库中?

javascript - Highlight.js 不适用于 PHP 的回显文本

php - 正则表达式 int 或 float

python - Django自定义注册: how to set a default password and use first name + last name as username

php - PDO 和 SQL 创建临时表的一般错误