[英]Using php to populate html table
I need to populate a table with data regarding 10 countries using a .php and .html file. 我需要使用.php和.html文件填充有关10个国家/地区的数据的表格。 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). 我已经下载了XAMPP(7.1.8),我正在运行Sierra的MacBook Pro 2012上运行(无法运行XAMPP-VM-启动时收到信号消失的错误消息)。
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. 我已经在MySql中创建了数据库testdb,并按如下所示对database.html文件和getuser.php文件进行了编码,并将它们保存到服务器上的htdocs文件夹中-但是在呈现时,它并没有选择xml文件或内的数据。
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. 我认为问题出在php的第46行或html的第36行,但我对php的了解还不足以知道如何更改它以使其正常工作。 Any help would be much appreciated, 任何帮助将非常感激,
Thanks 谢谢
Suze 叙
database.html 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 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(). 使用mysqli_fetch_assoc()。 Then you can access the Array like you do. 然后,您可以像访问阵列一样访问该数组。
while ($row = mysqli_fetch_assoc($result)) 而($ row = mysqli_fetch_assoc($ result))
and close the first table tag. 并关闭第一个表格标签。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.