简体   繁体   English

PHP帮助表获取错误

[英]PHP help table fetch mistake

Can help me to solve this? 能帮我解决这个问题吗? I just want to replace the values of a single row. 我只想替换单行的值。 I mean, i got two tables:Subjects and Careers -> 我的意思是,我有两个表格:主题和职业->

"Subjects" includes (id, careers_id (is the foreign key to the column "id" of the table Careers) subject, description, hours) “主题”包括(id, careers_id(是“职业”表的“ id”列的外键)主题,描述,工作时间)

"Careers" includes (id,name,description) “职业”包括(ID,名称,说明)

I JUST WANT TO REPLACE THE VALUE OF THE FOREIGN KEY which will give us the name of the career 我只想替换外键的值,这将为我们提供职业名称

I need something like this at the end: 我最后需要这样的东西:

model 模型

在此处输入图片说明 Here is my code: 这是我的代码:

<html>
<head>





</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>Subject</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['subject']."</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>

And,i put a button which allows me to add a new subject. 而且,我放了一个按钮,使我可以添加一个新主题。 So when i click on it another page open.I need to add a slider/select which shows me the careers available in the table career. 因此,当我单击它时,另一页打开。我需要添加一个滑块/选择,向我显示桌面职业中的可用职业。 TAKE A LOOK,i need something like this: 看一下,我需要这样的东西:

SELECT/SLIDER SELECT /滑

在此处输入图片说明

Here is my code to add a new subject (but i dont know how to make the slider/select :/) 这是我的代码以添加新主题(但我不知道如何制作滑块/选择:/)

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

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


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

        $a_sql=mysql_query("INSERT INTO subjects VALUES('','$carreras_id','$subject','$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>Subject:</td>
                    <td><input type='text' name='txtsubject'/></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>

Hope you can help me :/ 希望你能帮我 :/

Thanks! 谢谢!

You'll need to join the careers table in the query: 您需要在查询中加入职业表:

SELECT s.*, c.name AS career FROM subjects s LEFT JOIN careers c ON s.careers_id = c.id.

Afterwards, you can access the career name using $row['career'] . 之后,您可以使用$row['career']访问职业名称。

(sorry for bad english) (对不起,英语不好)

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

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

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

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

            header("location:index.php");

        }


    }else{

        echo "Actualizar";
    }

}
//NEW CODE for get careers list START HERE!
//do this outside your 'post' check because
//you need $careers array available for populate your form's select field
$careers = array();


$c_sql=mysql_query("SELECT * FROM careers"); //get careers data

while($row = mysql_fetch_assoc($c_sql))
{
    $careers[] = $row; // store each career in $careers variable
}
//NEW CODE for get careers list END HERE!
?>



<h2 align="center">ADD NEW SUBJECT</h2>

<form method="post"> /*post in lowercase*/
    <table align="center">
        <tr>    
            <td>Career:</td>
            <td>
              /*NEW CODE for show careers list START HERE!*/
              <select name='txtcarreras_id'>
              <?php
              foreach($careers as $career)
              {
                  echo "<option value='{$career['id']}'>{$career['name']}</option>";
              }
              ?>
              </select>
              /*NEW CODE for show careers list END HERE!*/
              <input type="hidden" name="txtid" value="0" />
            </td>
        </tr>
        <tr>    
            <td>Subject:</td>
            <td><input type='text' name='txtsubject'/></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>

Note: i would recommend use mysqli (procedural or object oriented) or Pdo instead of mysql, because mysql_* functions will be deprecated. 注意:我建议使用mysqli(面向过程或面向对象)或Pdo代替mysql,因为mysql_ *函数将被弃用。


EXTRA: I asume that connect.php looks similar to: 额外:我假设connect.php看起来类似于:

connect.php: connect.php:

<?php

$conn = mysql_connect($server, $user, $pass);

if (!$conn) {
    echo "Can't connect to mysql:".mysql_error();
    exit;
}

if (!mysql_select_db($dbname)) {
    echo "Can't select Database {$dbname}: " . mysql_error();
    exit;
}

using mysqli object would be: 使用mysqli对象将是:

connect.php: connect.php:

<?php

function getDB()
{
    $conn = new mysqli($server, $user, $pass);

    if ($conn->connect_errno) {
        echo "Can't connect to mysql:".$conn->connect_error;
        exit;
    }

    $conn->select_db($dbname);

    if ($conn->connect_errno) {
        echo "Can't select Database {$dbname}: " . $conn->connect_error;
        exit;
    }
    return $conn;
}
//getDB() will return your connection object

and you can make your querys like this: 您可以像这样进行查询:

<?php
include('connect.php'); 
//$sql = "SELECT * FROM sometable";
//$resource = getDB()->query($sql);
//now resource will have the result of your query

//for select your careers
$c_sql = getDB()->query("SELECT * FROM careers"); //get careers data

//while($row = mysql_fetch_assoc($c_sql))
//{
//    $careers[] = $row; // store each career in $careers variable
//}

//LESS CODE!
$careers = $c_sql->fetch_all(MYSQLI_ASSOC) // MYSQLI_ASSOC for associative array

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM