Output sql query into html table

前端 未结 5 573
旧时难觅i
旧时难觅i 2021-01-16 04:52

I\'m trying to place the output of this PHP SQL query into a database table, but it is outputting all of the row data into one column.

if(isset($_POST[\'subm         


        
5条回答
  •  死守一世寂寞
    2021-01-16 05:19

    The problem is that you're outputting a tag for every row and column. You need to move the outside the inner loop.

    Technically, you don't need to concatenate "{$value}" with the other two strings, but you really should pass $value through htmlspecialchars() to avoid producing incorrect HTML if the value contains any < or & characters. Eg:

    while ($row = mysql_fetch_row($result)) {
        print '';
        foreach ($row as $value) {
            $v = htmlspecialchars ($value);
            print "$v";
        }
        echo "\n";
    }
    

    Also, you're not supposed to have a
    element between table rows, which is why I replaced it with a newline above. Personally, I would skip the closing and tags since they are optional in HTML.

    Note also, that the MySQL extension is deprecated and no longer maintained. You should be using MySQLi or PDO these days.

提交回复
热议问题