通过输入mysql值创建带有javascript的html表。

时间:2022-09-26 10:09:28

The question might not really be clear because I could not think of something that could suit my problem, anyway, I am importing some mysql values using php and creating a html table using javascript, however I am having problems with the PHP as well as Javascript part, Also if anyone could tell me how I could just import values from MYSQL database and make a html table with it (That's basically what I am trying to do)

这个问题可能不是很清楚,因为我不能想到的东西可能适合我的问题,无论如何,我导入一些使用php和mysql值使用javascript创建一个html表,但是我有问题与php和javascript部分,如果任何人都可以告诉我怎样我可以从mysql数据库导入的值并做一个html表(这基本上就是我想做的)

1.) PHP is not importing values from MYSQL correctly

1)。PHP没有正确地从MYSQL导入值。

2.) On console it says: "Uncaught SyntaxError: Unexpected token ILLEGAL"

2)。在控制台,它说:“未捕获的SyntaxError:意外的标记非法”

The following is my code,

下面是我的代码,

<?php
$con = mysqli_connect("localhost","root","","human_information");
$result = mysqli_query($con,"SELECT * FROM basic_human_info");
$rows = mysqli_num_rows($result);
$columns = mysqli_num_fields($result);
 ?>

<body>
    <script type="text/javascript">
        function createTable(){
            var tBody = document.getElementsByTagName("body")[0];
            var table = document.createElement("table");
            table.style.border=1;
            table.style.width='50%';
            table.setAttribute('border',1);
            var rows = "<?php echo $rows; ?>";
            var columns = "<?php echo $columns ?>";
            for(var i=0;i<columns;i++){
                var tr = document.createElement("tr");
                for(var j=0;j<rows;j++){
                    var td = document.createElement("td");
                    td.appendChild(document.createTextNode("
                    <?php
                    $sql = mysqli_query($con,"SELECT 'First Name' FROM basic_human_info");
                    echo mysqli_fetch_assoc($sql);
                    ?>
                    "));
                    tr.appendChild(td);
                }
                table.appendChild(tr);
            }
            tBody.appendChild(table);
        }
        createTable();
    </script>
</body>

2 个解决方案

#1


1  

You are outputting an array with echo (echo mysqli_fetch_assoc($sql);) which will just print Array in the middle of the Javascript. That leads to the Uncaught SyntaxError.

您将输出一个带有echo的数组(echo mysqli_fetch_assoc($sql)),它将在Javascript的中间打印数组。这导致了未捕获的SyntaxError。

But the question rather is: Why use Javascript? Try it with HTML first until you get the PHP right, then do what ever you are planning to do with Javascript.

但问题是:为什么要使用Javascript?先尝试HTML,直到你得到了PHP,然后再做你打算用Javascript做的事情。

#2


1  

Like this you can try in php directly

像这样,您可以直接在php中尝试。

while ($row = mysqli_fetch_array($stmt)) {
    echo "<tr>";
    echo "<td>" . $row["aid"] . "</td>";
    echo "<td>" . $row["aname"] . "</td>";
    echo "</tr>";
}

#1


1  

You are outputting an array with echo (echo mysqli_fetch_assoc($sql);) which will just print Array in the middle of the Javascript. That leads to the Uncaught SyntaxError.

您将输出一个带有echo的数组(echo mysqli_fetch_assoc($sql)),它将在Javascript的中间打印数组。这导致了未捕获的SyntaxError。

But the question rather is: Why use Javascript? Try it with HTML first until you get the PHP right, then do what ever you are planning to do with Javascript.

但问题是:为什么要使用Javascript?先尝试HTML,直到你得到了PHP,然后再做你打算用Javascript做的事情。

#2


1  

Like this you can try in php directly

像这样,您可以直接在php中尝试。

while ($row = mysqli_fetch_array($stmt)) {
    echo "<tr>";
    echo "<td>" . $row["aid"] . "</td>";
    echo "<td>" . $row["aname"] . "</td>";
    echo "</tr>";
}