您好我创建了一个网站,允许您下载视频,您可以使用下拉框指定所需的视频。有五个下拉框主题部分和原则,然后seperatley有工作角色和职业。选择框相互连接,例如,如果您在主题框中选择英语,它只显示与英语主题相关的部分,并且原则相同,它只显示与该部分相关的原则。我现在的问题是当选择工作角色和职业时,哪些值应该是(1,15)实际返回结果(1,1),表示没有与之相关的结果。我的完整SQL查询代码是:
`
<?php
class SelectList
{
protected $conn;
public function __construct()
{
$this->DbConnect();
}
protected function DbConnect()
{
include "db_config.php";
$this->conn = mysql_connect($host,$user,$password) OR die("Unable to connect to the database");
mysql_select_db($db,$this->conn) OR die("can not select the database $db");
return TRUE;
}
public function ShowCategory()
{
$sql = "SELECT subject_id,description FROM subject";
$res = mysql_query($sql,$this->conn);
$category = '<option value="%">Subject</option>';
while($row = mysql_fetch_array($res))
{
$category .= '<option value="' . $row['subject_id'] . '">' . $row['description'] . '</option>';
}
return $category;
}
public function ShowType()
{
$sql = "SELECT section_id,description FROM section WHERE subject_id=$_POST[id]";
$res = mysql_query($sql,$this->conn);
$type = '<option value="%">choose...</option>';
while($row = mysql_fetch_array($res))
{
$type .= '<option value="' . $row['section_id'] . '">' . $row['description'] . '</option>';
}
return $type;
}
public function ShowPrinciple()
{
$sql = "SELECT principle_id,description FROM principle WHERE section_id=$_POST[id]";
$res = mysql_query($sql,$this->conn);
$principle = '<option value="%">choose...</option>';
while($row = mysql_fetch_array($res))
{
$principle .= '<option value="' . $row['principle_id'] . '">' . $row['description'] . '</option>';
}
return $principle;
}
public function GetResults()
{
$vars = $_POST["comboboxselections"]; // this gets comboboxselections from index.php
$box = ""; // placeholder for the html results to be returned to
$sql = "SELECT title,blurb FROM media WHERE subject_id = $vars[0] AND section_id= $vars[1] AND principle_id= $vars[2] "; // sql query
$box = $sql;
$hasprintedrecords = false; // capture records if there are no records printed to screen
//$box .= $sql;
$res = mysql_query($sql,$this->conn); // connection to database and also getting the results from query
while($row = mysql_fetch_array($res)) // the loop while there are results within the array run the function
{
$hasprintedrecords = true;// if there are records this equals true if not equals false
$box .= '<div style=";height:120px;"id=\"Video\"><br/><div style="border-style:solid;border-width:1px;border-color:#00000;width:504px;float:right;"id=\"Title\">'.$row['title'].'</div> <div style="width:220px;height:100px;float:left;border-style:solid;border-width:1px;border-color:#000000;" id=\"VideoImage\">hii</div> <div style="height:50px;width:504px;float:right;padding:2px;margin-bottom:5px;" id=\"Blurb\">'.$row['blurb'].'</div><div style="height:21px;width:152px;background:url(images/bg_top_img2-09.jpg) repeat-x;color:#ffffff;padding-left:2px;float:left;border:1px solid #000000;position:relative;border-radius:15px;text-align:center;" id=\"Downloads\"> Download </div></div>'; // if results are available then display to screen
}
if ( $hasprintedrecords == false) // if there are no results then function below is run
{
$box .='<div id=\"video\" style=\" border-style:solid; border-color:#000000; border-width:1px;\"> <div style="border-style:dashed; border-color:#f90;" id="text"> no media found for this selection.</div></div>'; // returns html message to screen
}
return $box; // returns results to the screen
}
public function ShowJobRole() {
$sql = "SELECT jobrole_id,description FROM jobrole";
$res = mysql_query($sql,$this->conn);
$jobrole = '<option value="%">Job role</option>';
while($row = mysql_fetch_array($res))
{
$jobrole .= '<option value="' . $row['jobrole_id'] . '">' . $row['description'] . '</option>';
}
return $jobrole;
}
public function ShowCareer() {
$sql = "SELECT career_id,description FROM career WHERE jobrole_id=$_POST[id]";
$res = mysql_query($sql,$this->conn);
$career = '<option value="%">Career</option>';
while($row = mysql_fetch_array($res))
{
$career .= '<option value="' . $row['career_id'] . '">' . $row['description'] . '</option>';
}
return $career;
}
public function get_job () {
$vars1 = $_POST["comboboxselections1"]; // this gets comboboxselections from index.php
$res = mysql_query($sql,$this->conn);
$box = ""; // placeholder for the html results to be returned to
$sql = "SELECT title,blurb FROM media_career_crossref INNER JOIN media ON media.media_id = media_career_crossref.media_id WHERE media_career_crossref.jobrole_id = $vars1[0] AND media_career_crossref.career_id = $vars1[1] ";
$box = $sql;
$hasprintedrecords = false; // capture records if there are no records printed to screen
//$box = $sql;
$res = mysql_query($sql,$this->conn); // connection to database and also getting the results from query
while($row = mysql_fetch_array($res)) // the loop while there are results within the array run the function
{
$hasprintedrecords = true;// if there are records this equals true if not equals false
$box .= '<div style=";height:120px;"id=\"Video\"><br/><div style="border-style:solid;border-width:1px;border-color:#00000;width:504px;float:right;"id=\"Title\">'.$row['title'].'</div> <div style="width:220px;height:100px;float:left;border-style:solid;border-width:1px;border-color:#000000;" id=\"VideoImage\">hii</div> <div style="height:50px;width:504px;float:right;padding:2px;margin-bottom:5px;" id=\"Blurb\">'.$row['blurb'].'</div><div style="height:21px;width:152px;background:url(images/bg_top_img2-09.jpg) repeat-x;color:#ffffff;padding-left:2px;float:left;border:1px solid #000000;position:relative;border-radius:15px;text-align:center;" id=\"Downloads\"> Download </div></div>'; // if results are available then display to screen
}
if ( $hasprintedrecords == false) // if there are no results then function below is run
{
$box .='<div id=\"video\" style=\" border-style:solid; border-color:#000000; border-width:1px;\"> <div style="border-style:dashed; border-color:#f90;" id="text"> no media found for this selection.</div></div>'; // returns html message to screen
}
return $box; // returns results to the screen
}
}
$opt = new SelectList();
?>
以及下拉框中的javascript和html是:
<html>
<head>
<!--[if !IE]><!-->
<link rel="stylesheet" type="text/css" href="Content.css">
<!--<![endif]-->
<!--[if IE 7]>
<link rel="stylesheet" type="text/css" href="IE7Content.css" />
<![endif]-->
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
</head>
<script type="text/javascript">
$(document).ready(function(){
$("select#type").attr("disabled","disabled");
$("select#category").change(function(){
$("select#type").attr("disabled","disabled");
$("select#type").html("<option>wait...</option>");
var id = $("select#category option:selected").attr('value');
$.post("select_type.php", {id:id}, function(data){
$("select#type").removeAttr("disabled");
$("select#type").html(data);
});
});
$("select#principle").attr("disabled","disabled");
$("select#type").change(function(){
$("select#principle").attr("disabled","disabled");
$("select#principle").html("<option>wait...</option>");
var id = $("select#type option:selected").attr('value');
$.post("select_principle.php", {id:id}, function(data){
$("select#principle").removeAttr("disabled");
$("select#principle").html(data);
});
});
$("select#career").attr("disabled","disabled");
$("select#jobrole").change(function(){
$("select#career").attr("disabled","disabled");
$("select#career").html("<option>wait...</option>");
var id = $("select#jobrole option:selected").attr('value');
$.post("select_career.php", {id:id}, function(data){
$("select#career").removeAttr("disabled");
$("select#career").html(data);
});
});
$("form#select_form").submit(function()
{
var cat = $("select#category option:selected").attr('value');
var type = $("select#type option:selected").attr('value');
var princ = $("select#principle option:selected").attr('value');
$txt = cat + type + princ;
var id = $("select#type option:selected").attr('value');
$.post("get_results.php", {comboboxselections:$txt}, function(data){
$("#Results").html(''+data);
});
if(cat>0 && type>0 && princ>0)
{
}
else
{
$("#result").html("you must choose three options!");
}
return false;
});
$("form#select_job").submit(function(){
var job = $("select#jobrole option:selected").attr('value');
var car = $("select#career option:selected").attr('value');
$txt1 = job + car;
var id = $("select#career option:selected").attr('value');
$.post("get_jobresults.php", {comboboxselections1:$txt1}, function(data){
$("#Results").html(''+data);
});
if(job>0 && car>0)
{
}
else
{
$("#result").html("you must choose two options!");
}
return false;
});
});
</script>
<body>
<?php
$host=""; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name
$tbl_name=""; // Table name
session_start();
$_SESSION["username"];
$_SESSION["password"];
$_SESSION["access"];
?>
<div id="Container">
<div id="Header">
<div id="SignIn"> <!-- This is the SignIn -->
<a href="logout.php">Logout</a> <!-- This is the Logout button -->
<a href="login.php">Login</a> <!-- This is the Login button -->
<a href="index.php">Home</a> <!-- This is the Home button -->
</div><!-- This is the end of SignIn -->
<div id="MainNavigation">
<a href="content.php"><img id="button2" src="images/banner2new.jpg"></a></li>
<a href="contributors.php"><img id="button1" src="images/banner3new.jpg"></a>
</div><!-- End of the MainNavigation -->
</div> <!-- End of the Header -->
<div id="Content">
<?php
$Public = "Please Login in or register an account to be able to see the content";
if($_SESSION["username"] == "")
{
header("Location: http://thor/classroom/login.php");
}
if($_SESSION["access"] == "Consumer")
{
echo "<div id=\"Options\">";
include "select.class.php";
echo "<form id=\"select_form\">";
echo "<div class=\"selecttitle\">";
echo "Choose a subject:";
echo "</div>";
echo "<select style=\"width:200px; margin-left:10px;\" id=\"category\" name=\"cat\" >";
echo $opt->ShowCategory();
echo "</select>";
echo "<br />";
echo "<br />";
echo "<div class=\"selecttitle\">";
echo "Choose a section:";
echo "</div>";
echo "<select style=\"width:200px; margin-left:10px;\"id=\"type\">";
echo "<option value=\"%\">";
echo "Section";
echo "</option>";
echo "</select>";
echo "<br />";
echo "<br />";
echo "<div class=\"selecttitle\">";
echo "Choose a principle:";
echo "</div>";
echo "<select style=\"width:200px; margin-left:10px;\"id=\"principle\">";
echo "<option value=\"%\">";
echo "Principle";
echo "</option>";
echo "</select>";
echo "<br />";
echo "<br />";
echo "<input style=\"margin-left:10px;\" type=\"submit\" value=\"Search\"/>";
echo "</form>";
echo " ------ Or ------";
echo "<form id=\"select_job\">";
echo "<div class=\"selecttitle\">";
echo "Choose a Job Role:";
echo "</div>";
echo "<select style=\"width:200px; margin-left:10px;\"id=\"jobrole\">";
echo $opt ->ShowJobRole();
echo "</select>";
echo "<br />";
echo "<br />";
echo "<div class=\"selecttitle\">";
echo "Choose a career:";
echo "</div>";
echo "<select style=\"width:200px; margin-left:10px;\"id=\"career\">";
echo "<option value=\"%\">";
echo "career";
echo "</option>";
echo "</select>";
echo "<br />";
echo "<br />";
echo "<input style=\"margin-left:10px;\" type=\"submit\" value=\"Search\"/>";
echo "</form>";
echo "</div>"; // end of the options tag any new content that needs to be on left side put inside this tag
echo "<div id=\"Results\">";
echo "</div>"; // end of the results results div needs to be out of the options tag
}
if($_SESSION["access"] == "Contributor")
{
echo "<div id=\"Options\">";
include "select.class.php";
echo "<form id=\"select_form\">";
echo "<div class=\"selecttitle\">";
echo "Choose a subject:";
echo "</div>";
echo "<select style=\"width:200px; margin-left:10px;\" id=\"category\" name=\"cat\" >";
echo $opt->ShowCategory();
echo "</select>";
echo "<br />";
echo "<br />";
echo "<div class=\"selecttitle\">";
echo "Choose a section:";
echo "</div>";
echo "<select style=\"width:200px; margin-left:10px;\"id=\"type\">";
echo "<option value=\"%\">";
echo "Section";
echo "</option>";
echo "</select>";
echo "<br />";
echo "<br />";
echo "<div class=\"selecttitle\">";
echo "Choose a principle:";
echo "</div>";
echo "<select style=\"width:200px; margin-left:10px;\"id=\"principle\">";
echo "<option value=\"%\">";
echo "Principle";
echo "</option>";
echo "</select>";
echo "<br />";
echo "<br />";
echo "<input style=\"margin-left:10px;\" type=\"submit\" value=\"confirm\"/>";
echo "</form>";
echo "<form id=\"select_job\">";
echo "<div class=\"selecttitle\">";
echo "Choose a Job Role:";
echo "</div>";
echo "<select style=\"width:200px; margin-left:10px;\"id=\"jobrole\">";
echo $opt ->ShowJobRole();
echo "</select>";
echo "<br />";
echo "<br />";
echo "<div class=\"selecttitle\">";
echo "Choose a career:";
echo "</div>";
echo "<select style=\"width:200px; margin-left:10px;\"id=\"career\">";
echo "<option value=\"%\">";
echo "career";
echo "</option>";
echo "</select>";
echo "<br />";
echo "<br />";
echo "<input style=\"margin-left:10px;\" type=\"submit\" value=\"confirm\"/>";
echo "</form>";
echo "</div>"; // end of the otions tag any new content that needs to be on left side put inside this tag
echo "<div id=\"Results\">";
echo "</div>"; // end of the results results div needs to be out of the otions tag
}
if($_SESSION["access"] == "Administrator")
{
echo "<div id=\"Options\">";
include "select.class.php";
echo "<form id=\"select_form\">";
echo "<div class=\"selecttitle\">";
echo "Choose a subject:";
echo "</div>";
echo "<select style=\"width:200px; margin-left:10px;\" id=\"category\" name=\"cat\" >";
echo $opt->ShowCategory();
echo "</select>";
echo "<br />";
echo "<br />";
echo "<div class=\"selecttitle\">";
echo "Choose a section:";
echo "</div>";
echo "<select style=\"width:200px; margin-left:10px;\"id=\"type\">";
echo "<option value=\"%\">";
echo "Section";
echo "</option>";
echo "</select>";
echo "<br />";
echo "<br />";
echo "<div class=\"selecttitle\">";
echo "Choose a principle:";
echo "</div>";
echo "<select style=\"width:200px; margin-left:10px;\"id=\"principle\">";
echo "<option value=\"%\">";
echo "Principle";
echo "</option>";
echo "</select>";
echo "<br />";
echo "<br />";
echo "<input style=\"margin-left:10px;\" type=\"submit\" value=\"confirm\"/>";
echo "</form>";
echo "<form id=\"select_job\">";
echo "<div class=\"selecttitle\">";
echo "Choose a Job Role:";
echo "</div>";
echo "<select style=\"width:200px; margin-left:10px;\"id=\"jobrole\">";
echo $opt ->ShowJobRole();
echo "</select>";
echo "<br />";
echo "<br />";
echo "<div class=\"selecttitle\">";
echo "Choose a career:";
echo "</div>";
echo "<select style=\"width:200px; margin-left:10px;\"id=\"career\">";
echo "<option value=\"%\">";
echo "career";
echo "</option>";
echo "</select>";
echo "<br />";
echo "<br />";
echo "<input style=\"margin-left:10px;\" type=\"submit\" value=\"confirm\"/>";
echo "</form>";
echo "</div>"; // end of the otions tag any new content that needs to be on left side put inside this tag
echo "<div id=\"Results\">";
echo "</div>"; // end of the results results div needs to be out of the otions tag
}
?>
</div> <!-- End of the Content -->
<!-- <div id="SignIn">
<a href="logout.php">Logout</a>
<a href="login.php">Login</a>
<a href="index.php">Home</a>
</div> -->
</div> <!-- End of the Container -->
</body> <!-- End of the Body -->
</html> <!-- End of the Html -->`
答案 0 :(得分:1)
var_dump($vars);
在这种情况下,var_dump或类似的print_r是你的朋友,以确定数组是否具有你想要的结构并包含适当的数据。
如果$vars1
只包含单个数组,则应使用fetchall来推送查询收集的数据
$sql = "SELECT career_id,description FROM career WHERE jobrole_id={$_POST[id]}";
$res = mysql_query($sql,$this->conn);
while($row = mysql_fetch_array($res))
{
$all[] = $row;
}
return $all;
}
你得到了
jobrole_id = $_POST['id']
和
carear_id = $all[0]['carear_id']
注意:我强烈建议您使用PDO,因为您的代码对于通过post [id]进行sql注入非常开放;
进行编辑;
public function ShowCareer() {
$sql = "SELECT career_id,description FROM career WHERE jobrole_id=$_POST[id]";
$res = mysql_query($sql,$this->conn);
$career = '<option value="%">Career</option>';
while($row = mysql_fetch_array($res))
{
$career .= '<option value="' . $row['career_id'] . '">' . $row['description'] . '</option>';
}
return $career;
}
所以问题出在这个代码上?我对么?以下是预计的结果,最高可达15?
<option value="1">Career 1</option>
<option value="2">Career 2</option>
<option value="3">Career 3</option>
等等?
顺便说一下
尝试将$_POST[id]
修改为{$_POST['id']}
如果数据库结构类似
<强> Jobrole:(父)强>
*jobrole_id* | description
职业(儿童)
career_id | *job_id* | description
所以你想在职业 * job_id *中使用 Jobrole 表的 id 我假设您的{$ _POST ['id']}持有Jobrole_id,然后像这样更改您的代码
public function ShowCareer() {
$sql = "SELECT career_id,description FROM career WHERE job_id={$_POST['id']}";
$res = mysql_query($sql,$this->conn);
$career = '<option value="%">Career</option>';
while($row = mysql_fetch_array($res))
{
$career .= '<option value="' . $row['career_id'] . '">' . $row['description'] . '</option>';
}
return $career;
}
答案 1 :(得分:0)
我弄清楚我做错了这不是sql的问题它是顶部的javascript问题因为在顶部数据库中的所有数据放在一起我只是完全忘记了我不得不拆分它们以形成正确的答案所以在数据库中它将是2,10,11当它被放在一起它看起来像21011然后当下拉框读取它时它读取2然后1而不是10
感谢您的建议。