PHP求助table获取错误
PHP help table fetch mistake
可以帮我解决这个问题吗?
我只想替换单行的值。我的意思是,我有两个 tables:Subjects 和 Careers ->
"Subjects"包括(id,careers_id(是table职业的"id"列的外键) 主题、描述、时间)
"Careers" 包括 (id,name,description)
我只想替换外键的值,它将为我们提供职业名称
最后我需要这样的东西:
型号
这是我的代码:
<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>
而且,我放了一个按钮,可以让我添加一个新主题。因此,当我单击它时,另一个页面 open.I 需要添加一个 slider/select,它向我显示 table 职业中可用的职业。看一看,我需要这样的东西:
SELECT/SLIDER
这是我添加新主题的代码(但我不知道如何制作 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>
希望你能帮助我:/
谢谢!
您需要在查询中加入职业table:
SELECT s.*, c.name AS career FROM subjects s LEFT JOIN careers c ON s.careers_id = c.id.
之后,您可以使用$row['career']
访问职业名称。
(抱歉英语不好)
<?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
可以帮我解决这个问题吗? 我只想替换单行的值。我的意思是,我有两个 tables:Subjects 和 Careers ->
"Subjects"包括(id,careers_id(是table职业的"id"列的外键) 主题、描述、时间)
"Careers" 包括 (id,name,description)
我只想替换外键的值,它将为我们提供职业名称
最后我需要这样的东西:
型号
<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>
而且,我放了一个按钮,可以让我添加一个新主题。因此,当我单击它时,另一个页面 open.I 需要添加一个 slider/select,它向我显示 table 职业中可用的职业。看一看,我需要这样的东西:
SELECT/SLIDER
这是我添加新主题的代码(但我不知道如何制作 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>
希望你能帮助我:/
谢谢!
您需要在查询中加入职业table:
SELECT s.*, c.name AS career FROM subjects s LEFT JOIN careers c ON s.careers_id = c.id.
之后,您可以使用$row['career']
访问职业名称。
(抱歉英语不好)
<?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