我正在为类构建一个数据库表,我必须将数据从MySQL数据库呈现到表中。
我遇到的问题是,在100个ID之后,用户名和所有内容都会重复。屏幕截图:http://imgur.com/a/vKfzA
我是一个巨大的编码菜鸟,尝试一切,似乎无法解决它
这是我的代码
<?php
//connection to DB
require "connectionPages/remoteConnect.php";
$userSQL = "SELECT * FROM USER, CUSTOMER";
$result = $mysqli->query($userSQL);
$pTable = "<div class='table'>
<div class='heading'>
<div class='cell'></div>
<div class='cell'>ID</div>
<div class='cell'>userName</div>
<div class='cell'>custEmail</div>
<div class='cell'>Last Name</div>
</div>";
while($row = $result->fetch_array())
{
$ID = $row['userID'];
$userName = $row['userName'];
$custEmail = $row['custEmail'];
$userLname = $row['custLName'];
$pTable .= "<div class='row'>
<div class='cell'>
<a class='delete'>Delete</a>
<a class='edit'>Edit</a>
<a class='cancel'>Cancel</a>
</div>
<div class='cell'><input disabled size='2' type='text' name='txtID' value='$ID'></div>
<div class='cell'><input disabled size='25' type='text' name='txtFirst' value='$userName'></div>
<div class='cell'><input disabled size='25' type='text' name='txtEmail' value='$custEmail'></div>
<div class='cell'><input disabled size='25' type='text' name='txtTeam' value='$userLname'></div>
</div>";
}
//close out table tag
$pTable .= "</div><p><a href='outputPages/enterUser.php'>Add A User</a></p>";
//close connection to DB
$mysqli->close();
$doc = "<!DOCTYPE html>
<html>
<head>
<script src='clientCode/jquery-1.12.2.min.js'></script>
<script src='clientCode/AJAX.js'></script>
<link rel='stylesheet' type='text/css' href='css/tableStyle.css'>
</head>
<body>
$pTable
</body>
</html>";
echo $doc;
?>
答案 0 :(得分:1)
您的问题是您的SQL查询:SELECT * FROM USER, CUSTOMER
;
从CUSTOMER中的每一行中选择USER的所有行!
很确定这不是你想要的。
如果您告诉我们每个表中的列,我们可以帮助您进一步。你可能想(左)?加入两个表(一个猜测)。
* edit:根据您的数据库架构,您的查询应为:
SELECT * FROM USER, CUSTOMER WHERE USER.custID = CUSTOMER.custID
这将为每位客户提供一行。
答案 1 :(得分:0)
你应该在这些表之间指定专栏,例如。
$userSQL = "SELECT * FROM USER, CUSTOMER WHERE USER.CUSTOMER_ID = CUSTOMER.ID";
或者更好地使用JOIN法规
$userSQL = "SELECT * FROM USER u JOIN CUSTOMER c ON u.CUSTOMER_ID = c.ID";