我正在尝试从我的MySQL数据库获得所有具有相同名称的行到我的HTML表。 所以基本上,我的php脚本getRequestToShow.php将值回显到。js文件(该文件通过使用XMLHttpRequest向php脚本请求值)。 然后这个。js文件在HTML表中添加行。
我从php中回显MySQL行,如下所示:$conn = new mysqli($servername, $username, $password, $db_name);
if ($conn->connect_error){
die("Connection failed: " . $conn->connect_error);
}
$id = $_GET['id'];
$id = mysqli_real_escape_string($conn,$id);
$query = "SELECT * FROM `requests` WHERE `name`='$id'";
$result = mysqli_query($conn,$query);
while($row = mysqli_fetch_array($result)) {
echo json_encode(array('first'=>$row['name'],'second'=>$row['amount'],'third'=>$row['date'],'fourth'=>$row['state']));
}
但是我只能从数据库到HTML表中获取值。 如果有多个行具有相同的名称,它将不在HTML表中显示任何内容。function showUser45(str) {
var tableRef = document.getElementById('customers').getElementsByTagName('tbody')[0];
var newRow = tableRef.insertRow(tableRef.rows.length);
var xmlhttp22 = new XMLHttpRequest();
xmlhttp22.onreadystatechange = function() {
if (xmlhttp22.readyState == 4 && xmlhttp22.status == 200) {
if(xmlhttp22.responseText === "") {
var newCell = newRow.insertCell(0);
var newText = document.createTextNode("No earlier withdraws");
newCell.appendChild(newText);
} else {
var h2 = JSON.parse(xmlhttp22.responseText);
var newCell1 = newRow.insertCell(0);
var newText1 = document.createTextNode(h2.first);
var newCell2 = newRow.insertCell(1);
var newText2 = document.createTextNode(h2.second);
var newCell3 = newRow.insertCell(2);
var newText3 = document.createTextNode(h2.third);
newCell1.appendChild(newText1);
newCell2.appendChild(newText2);
newCell3.appendChild(newText3);
if (h2.fourth == 1) {
var newCell4 = newRow.insertCell(3);
var newText4 = document.createTextNode("Completed");
newCell4.appendChild(newText4);
} else {
var newCell5 = newRow.insertCell(3);
var newText5 = document.createTextNode("Pending");
newCell5.appendChild(newText5);
}
}
}
};
xmlhttp22.open("GET","getRequestToShow.php/?id="+str,true);
xmlhttp22.send();
}
这是HTML表的HTML。
UsernameAmountDateState
提前谢谢!