简体   繁体   中英

Using php to populate html table

I need to populate a table with data regarding 10 countries using a .php and .html file. I have downloaded XAMPP (7.1.8) I am working on a MacBook Pro 2012 running Sierra (can not get XAMPP-VM to run - get a signal killed error message on start up).

I have created the database testdb in MySql and coded the database.html file and the getuser.php file as below and saved them to the htdocs folder on the server - but when it renders, it isn't picking up the xml file or the data within.

I believe the problem is either on line 46 in the php or line 36 in the html, but I don't know enough about php to to know how to change it to make it work. Any help would be much appreciated,

Thanks

Suze

database.html

<head>

    <script>

        function showUser (str) {
            if (str=="") {
                document.getElementById("txtHint").innerHTML="";

                return;
            }

            if (window.XMLHttpRequest)  {

                // code for IE7+, Firefox, Chrome, Opera, Safari

                xmlhttp=new XMLHttpRequest ();

            } else { // code for IE6, IE5

                xmlhttp=new ActiveXObject ("Microsoft.XMLHTTP");

            }

            xmlhttp.onreadystatechange=function () {

                if (this.readyState==4 && this.status==200) {

                    document.getElementById("hint").innerHTML=this.responseText;
                }
            }

            xmlhttp.open("GET","getuser.php?q="+str,true);
            xmlhttp.send();
        }

    </script>

</head>

<body>

    <form>

    <select name="users" onchange="showUser(this.value)">

        <option value ="">Select a country:</option>

        <option value ="1">United Kingdom</option>

        <option value="2">France</option>

        <option value="3">Germany</option>

        <option value="4">India</option>

        <option value="5">Hungary</option>

        <option value="6">Ireland</option>

        <option value="7">Greece</option>

        <option value="8">USA</option>

        <option value="9">Japan</option>

        <option value="10">Spain</option>

        </select>

    </form>

<br>

    <div id="hint"><b>Country info will be highlighted below</b></div>

</body>

getuser.php

<style>

    table {

        width: 100%;

        border-collapse: collapse;
    }

    table, td, th {

        border: 1px solid black;

        padding 5px;

    }

    th {text-align: left;}

</style>

</head>

<?php

$q = intval ($_GET['q']);


$con = mysqli_connect('localhost', 'root', '');

if (!$con) {

    die('Could not connect: ' . mysqli_error($con));
}

mysqli_select_db($con,"testdb");

$sql = "SELECT * FROM records WHERE id = '".$q."'";

$result = mysqli_query($con,$sql);


echo "<table

<tr>

<th>Country</th>

<th>Capital City</th>

<th>Currency</th>

</tr>";

error_reporting(E_ERROR | E_PARSE);

while ($row = mysqli_fetch_array($result)) {

    echo "<tr>";

    echo "<td>" . $row['Country'] . "</td>";

    echo "<td>" . $row['Capital City'] . "</td>";

    echo "<td>" . $row['Currency'] . "</td>";

    echo "</tr>";
}

echo "</table>";

mysqli_close($con);

?>

</body>

The Problem is that you fetch an Array instead of an Assoziation. Use mysqli_fetch_assoc(). Then you can access the Array like you do.

while ($row = mysqli_fetch_assoc($result))

and close the first table tag.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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