简体   繁体   English

使用PHP在HTML表中显示MySQL行内容

[英]Showing mysql rows content in html table using php

I'm new here and I have a question, I looked for the solution everywhere and I still cant manage to solve this. 我是新来的,我有一个问题,我到处都在寻找解决方案,但我仍然无法解决这个问题。

I want to show the results of the SELECT statement (in php) in a table (html) using JS. 我想在使用JS的表(html)中显示SELECT语句(在php中)的结果。 Here is the code of these 3 files: 这是这3个文件的代码:

main.HTML file main.HTML文件

<html>
<head>
    <meta charset="UTF-8"/>
    <script type = "text/javascript" src="jquery.js" ></script> 
    <script src="select.js" type="text/javascript"></script>
</head>
<body>
<button id="button"> Mostrar </button>
<br>
<input type="text" id="id" />
<div id="content"></div>
</body>
</html>

select.PHP file select.PHP文件

<?php
$link=mysqli_connect("xxxxxxx", "user_tienda","%%%%%%%%","pool_tiendas");

if (mysqli_connect_errno() )
    echo "Fallo en la conexion con mysql" .mysqli_connect_error();

$action=$_POST["action"];
if ($action=="showroom") {
    $query = "SELECT cod, nmbre, drccn from tienda";
    $show = mysqli_query($link, $query) or die ("error");
    echo "<table border='2px'><tr><td>cod</td><td>nmbre</td><td>drccn</td</tr>";
    while ($row = mysqli_fetch_array($show)) {

        echo "<tr><td>" .$row['cod']."</td><td>".$row['nmbre']."</td><td>".$row['drccn']."</td></tr>";
    }
    echo "</table>";
}
?>

select.JS file select.js文件

$(document).ready(function(){
    $("#button").click(function () {

        function show_all() {
            $.ajax({
                type: "POST",
                url: "select.php",
                data:{action:"showroom"},
                success: function (data) {
                    $("#id").hide();
                    $("#content").html(data);
                }
            });
        }

        show_all();
    });
});

The problem is when I click the button to show the content nothing happens. 问题是当我单击按钮以显示内容时,什么也没发生。

The Select statemnt is correct, in Mysql font I can see the results of the SELECT statement. Select状态是正确的,在Mysql字体中,我可以看到SELECT语句的结果。

This is what I'm getting if I directly run select.php: codnmbredrccn"; while ($row = mysqli_fetch_array($show)) { echo "" .$row['cod']."".$row['nmbre']."".$row['drccn'].""; } echo ""; } ?> 如果我直接运行select.php:codnmbredrccn“;而($ row = mysqli_fetch_array($ show)){echo”“。$ row ['cod']。”“。$ row ['nmbre ']。“”。$ row ['drccn']。“”;}回声“”;}?>

Now in mozilla console I can see 2 errors: no se encuentra elemento select.php:18 and no se encuentra elemento main.html:18 (no se encuentra elemento -> can't find element 现在,在mozilla控制台中,我可以看到2个错误:没有se encuentra elemento select.php:18和no se encuentra elemento main.html:18(没有se encuentra elemento->找不到元素

The button is supposed to hide when clicked, but nothing happens. 该按钮应该在单击时隐藏,但是什么也没有发生。 Seems like it never executes the js file. 似乎它从不执行js文件。

In this line 在这条线

echo "<table border='2px'><tr><td>cod</td><td>nmbre</td><td>drccn</td</tr>";

The final </td> is missing a > 最后的</td>缺少>

Everything is perfect in your script if you are sure about your mysql details. 如果您确定自己的mysql详细信息,那么一切在您的脚本中都是完美的。 Please check your js files getting included in script. 请检查您的js文件是否包含在脚本中。

<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>

I removed jquery.js and inserted another script at head. 我删除了jquery.js并在头插入了另一个脚本。 It worked for me. 它为我工作。 I connected it to localhost. 我将其连接到本地主机。 check it. 核实。

main.html main.html

<html>
    <head>
        <meta charset="UTF-8"/>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
        <script src="select.js" type="text/javascript"></script>
    </head>
    <body>
    <button id="button"> Mostrar </button>
    <br>
    <input type="text" id="id" />
    <div id="content"></div>
    </body>
</html>

select.js select.js

 $(document).ready(function(){
        $("#button").click(function () {

            function show_all() {
                $.ajax({
                    type: "POST",
                    url: "select.php",
                    data:{action:"showroom"},
                    success: function (data) {
                        $("#id").hide();
                        $("#content").html(data);
                    }
                });
            }

            show_all();
        });
    });

select.php select.php

<?php
$link=mysqli_connect("localhost", "root","","manishYii");

if (mysqli_connect_errno() )
    echo "Fallo en la conexion con mysql" .mysqli_connect_error();

$action=$_POST["action"];
if ($action=="showroom") {
    $query = "SELECT FirstName, LastName, EmailID from members";
    $show = mysqli_query($link, $query) or die ("error");
    echo "<table border='2px'><tr><td>cod</td><td>nmbre</td><td>drccn</td</tr>";
    while ($row = mysqli_fetch_array($show)) {

        echo "<tr><td>" .$row['FirstName']."</td><td>".$row['LastName']."</td><td>".$row['EmailID']."</td></tr>";
    }
    echo "</table>";
}
?>

Output 输出量 在此处输入图片说明

try this 尝试这个

while ($row = mysqli_fetch_assoc($show)) {

    echo "<tr><td>" .$row['cod']."</td><td>".$row['nmbre']."</td><td>".$row['drccn']."</td></tr>";
}

or 要么

while ($row = mysqli_fetch_assoc($show)) {

    echo "<tr><td>" .$row['0']."</td><td>".$row['1']."</td><td>".$row['2']."</td></tr>";
}

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

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