可以帮我解决这个问题吗? 我只想替换单行的值。我的意思是,我有两张桌子:主题和职业 - >
"主体"包括(id, careers_id(是列的外键" id"表格职业)主题,描述,小时)
"职业"包括(身份证,姓名,说明)
我只想更换外键的价值,这将给我们提供职业的名称
最后我需要这样的东西:
模型
<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>
而且,我按了一个按钮,允许我添加一个新主题。所以,当我点击它打开另一页时。我需要添加一个滑块/选择,向我展示表职业中可用的职业。看一看,我需要这样的东西:
选择/ SLIDER
这是我添加新主题的代码(但我不知道如何制作滑块/选择:/)
<?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>
希望你能帮助我:/
谢谢!
答案 0 :(得分:0)
您需要加入查询中的职业表:
SELECT s.*, c.name AS career FROM subjects s LEFT JOIN careers c ON s.careers_id = c.id.
之后,您可以使用$row['career']
访问职业名称。
答案 1 :(得分:0)
(抱歉英语不好)
<?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>
注意:我建议使用mysqli(面向过程或面向对象)或Pdo而不是mysql,因为不推荐使用mysql_ *函数。
EXTRA:我认为 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;
}
使用 mysqli 对象将是:
<强> 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
你可以这样查询:
<?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