简体   繁体   English

PHP MySQL根据选择的选项和onclick显示数据库

[英]PHP MySQL display database based on option chosen and onclick

I need to select an option(book category) and then search the database to find all the books with that category, and display the database as a table. 我需要选择一个选项(书籍类别),然后搜索数据库以查找具有该类别的所有书籍,并将数据库显示为表格。

I can use HTML, JavaScript, PHP and MySQL in this code. 我可以在此代码中使用HTML,JavaScript,PHP和MySQL。

This is the HTML code: 这是HTML代码:

<p>
            <select name="genre" size="1">
                <option value='0' id='AA'>Art & Architecture</option>
                <option value='1' id='BG'>Biography</option>
                <option value='2' id='CH'>Children</option>
                <option value='3' id='DR'>Drama</option>
                <option value='4' id='ER'>Erotica</option>
                <option value='5' id='HS'>History</option>
                <option value='6' id='ML'>Military</option>
                <option value='7' id='MU'>Music</option>
                <option value='8' id='NE'>Non-English</option>
                <option value='9' id='NV'>Novels</option>
                <option value='10' id='OC'>Occult</option>
                <option value='11' id='PS'>Philosophy</option>
                <option value='12' id='PG'>Photography</option>
                <option value='13' id='PT'>Poetry</option>
                <option value='14' id='PE'>Politics & Economics</option>
                <option value='15' id='RG'>Religion</option>
                <option value='16' id='SE'>Science & Engineering</option>
                <option value='17' id='SP'>Sport</option>
                <option value='18' id='TE'>Travel & Exploration</option>
            </select>
        <input type="submit" value="Search" onClick="">
    </p> 

This is the PHP and MySQL code: 这是PHP和MySQL代码:

<?php 
        $mysqli = new mysqli('localhost','root','','bookstore');
        if(mysqli_connect_errno())
        {
            $problem = mysqli_connect_error();
            echo "Error opening database";
            die ($problem);
        }
        $genre = getElementByName('genre');
        $query = "SELECT category(*) FROM book_list WHERE category=$genre"; 

        echo "<h2>These are the books available</h2>";
        // Execute Query
        $result = $mysqli->query($query);
        // echo "<table border="1">";
            while($row = $result->fetch_array(mysql_query))
            {
                $Title = $row["Title"];
                $Author = $row["Author"];
                $Price = $row["Price ($)"];

                echo "<tr><td>$Title</td><td>$Author</td><td>$Price</td></tr>";
                echo "</table>";
            }
        $result->close();
    ?>

Any ideas where it is going wrong? 任何想法哪里出了问题?

PS: super new to PHP so please use simple language and explanation. PS:PHP的新手,请使用简单的语言和说明。 Thank you! 谢谢!

It seems like you have mixed up the things between php and javascript (jquery) here .. I have come up with below conclusion for your query though .. 似乎您在这里混合了php和javascript(jquery)之间的内容。..不过,我为您的查询提出了以下结论。

Created a test.php file 创建了一个test.php文件

<script   src="https://code.jquery.com/jquery-3.1.0.min.js"   integrity="sha256-cCueBR6CsyA4/9szpPfrX3s49M9vUU5BgtiJj06wt/s="   crossorigin="anonymous"></script>

<script>

$(document).ready(function(){
    $("#genre").on('change', function postinput(){
        var genre = $(this).val(); // this.value
        $.ajax({
            url: 'test2.php',
            data: { genre: genre },
            type: 'post'
        }).done(function(responseData) {
            console.log('Done: ', responseData);
            $("#your_html_response").html(responseData);
        }).fail(function() {
            console.log('Failed');
        });
    });
});

</script>

<p>
            <select name="genre" id="genre" size="1">
                <option value='0' id='AA'>Art & Architecture</option>
                <option value='1' id='BG'>Biography</option>
                <option value='2' id='CH'>Children</option>
                <option value='3' id='DR'>Drama</option>
                <option value='4' id='ER'>Erotica</option>
                <option value='5' id='HS'>History</option>
                <option value='6' id='ML'>Military</option>
                <option value='7' id='MU'>Music</option>
                <option value='8' id='NE'>Non-English</option>
                <option value='9' id='NV'>Novels</option>
                <option value='10' id='OC'>Occult</option>
                <option value='11' id='PS'>Philosophy</option>
                <option value='12' id='PG'>Photography</option>
                <option value='13' id='PT'>Poetry</option>
                <option value='14' id='PE'>Politics & Economics</option>
                <option value='15' id='RG'>Religion</option>
                <option value='16' id='SE'>Science & Engineering</option>
                <option value='17' id='SP'>Sport</option>
                <option value='18' id='TE'>Travel & Exploration</option>
            </select>
    </p>

    <div id="your_html_response">

    </div>

Here above, i have included a jquery library and using dropdown's onchange function to post a value to test2.php file.. 在上面,我已经包括一个jQuery库,并使用下拉列表的onchange函数将值发布到test2.php文件。

also you can see blank <div id="your_html_response"></div> which will add data from the response which will get from test2.php file 您还可以看到空白的<div id="your_html_response"></div> ,它将添加来自响应的数据,该响应将从test2.php文件获取

Also removed <input type="submit" value="Search" onClick=""> as its not required as you have not created a form in your code. 还删除了<input type="submit" value="Search" onClick=""> ,因为您尚未在代码中创建表单,因此不需要此内容。

Created test2.php file 创建的test2.php文件

<?php


        $mysqli = new mysqli('localhost','root','','bookstore');
        if(mysqli_connect_errno())
        {
            $problem = mysqli_connect_error();
            echo "Error opening database";
            die ($problem);
        }
        $genre = $_POST['genre'];
        $query = "SELECT category(*) FROM book_list WHERE category=$genre";

        $html_response = "<h2>These are the books available</h2>";
        // Execute Query
        $result = $mysqli->query($query);
        $html_response .= "<table border='1'>";
            while($row = $result->fetch_array(mysql_query))
            {
                $Title = $row["Title"];
                $Author = $row["Author"];
                $Price = $row["Price ($)"];

                $html_response.= "<tr><td>$Title</td><td>$Author</td><td>$Price</td></tr>";

            }
            $html_response .= "</table>";


        $result->close();
        echo $html_response;
    ?>

Here above i have got the posted value from dropdown and connected a database and returning a records (if any) using mysqli query .. i have returned response in html here .. 在上面,我从下拉列表中获取了发布的值,并连接了数据库,并使用mysqli查询返回了记录(如果有)..我在html中返回了响应..

if you see, in test.php you can find this code 如果看到的话,在test.php中可以找到此代码

done(function(responseData) {
            console.log('Done: ', responseData);
            $("#your_html_response").html(responseData);
    }

in which i have used $("#your_html_response").html(responseData); 其中我使用了$("#your_html_response").html(responseData); which is adding a data from the response we got from test2.php file 这是从我们从test2.php文件获得的响应中添加数据

Hope this sums your confusion. 希望这能使您感到困惑。

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

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