duanfen2349 2014-02-24 13:48
浏览 121
已采纳

sql查询只读取ID的第一个数字

Hi I have created a website that allows you to download videos you can specify what videos you want using drop down boxes. there are five drop down boxes subject section and principle and then seperatley there are job role and career. the selection boxes are connected to each other for example if you select english in the subject box it only shows the sections connected to english subject and the same with principle it will only show the principles related to the section. my problem at the moment is when choose the job role and careers which values should be (1, 15) actually return the results (1,1) which says there are no results related to them. my full sql query code is:

`

<?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();
?>

and the javascript and html that are the drop down boxes are:

<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 -->`
  • 写回答

2条回答 默认 最新

  • douchensou6969 2014-02-24 13:58
    关注

    var_dump($vars);
    var_dump or similar print_r is your friend in situations like this to determine whethere the array has the structure you want and contain the proper data.

    if $vars1 containing only single array then you should use fetchall to push the datas gathered by your query

    $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;
        }
    

    you got jobrole_id = $_POST['id'] and

    carear_id = $all[0]['carear_id']
    

    Note: I highly suggest you to to use PDO, because your code is very open to sql injections thru post[id];

    Furter editing;

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

    so the problem is with this code ? am i correct? below is the expected result up to 15?

    <option value="1">Career 1</option>
    <option value="2">Career 2</option>
    <option value="3">Career 3</option>
    

    an so on? and by the way try fixing $_POST[id] to {$_POST['id']}

    if you database structure is like

    Jobrole:(parent)

    *jobrole_id* | description
    

    Career (child)

    career_id | *job_id* | description
    

    So you want to use the Jobrole table's id inside the career *job_id* i assume your {$_POST['id']} is holding Jobrole_id then change your code above like this

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

报告相同问题?

悬赏问题

  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误