2个表,users
和useradvert
(两者在sql中都有关系正常工作,只是无法从第二个表useradvert
中提取数据)。与表User
相关的表id
(主键 - useradvert
)(索引键 - id
)。以下是摘录。
//从table-users和table-useradvert调用记录,加入
if(isset($_POST['username'])){
$userName = $_POST['username'];
$query = "SELECT users.id, users.name, users.username, users.telno, useradvert.id, useradvert.name2, useradvert.color2, useradvert.hobby2, useradvert.radiobtn, useradvert.kupon, useradvert.image, useradvert.image2 ". "FROM users
LEFT JOIN useradvert ON useradvert.id = users.id"." WHERE username= ?";
$stmt = $conn->prepare($query);
$stmt->bind_param('s',$userName);
$stmt->execute();
$res = $stmt->get_result();
$row = $res->fetch_array();
$_SESSION['id'] = $row['id'];
$_SESSION['name'] = $row['name'];
$_SESSION['username'] = $row['username'];
$_SESSION['telno'] = $row['telno'];
$_SESSION['name2'] = $row['name2'];
$_SESSION['color2'] = $row['color2'];
$_SESSION['hobby2'] = $row['hobby2'];
$_SESSION['radiobtn'] = $row['radiobtn'];
$_SESSION['kupon'] = $row['kupon'];
$_SESSION['image'] = $row['image'];
$_SESSION['image2'] = $row['image2'];
}
?>
继续摘录......
</head>
<body>
<div id="apDiv3">
<p> </p>
<p> </p>
<p> </p>
<p><span class="TabbedPanelsContent">
<?php
//display record from table- users (parent table can display)
echo $_SESSION['id']."<br/>";
echo $_SESSION['name']."<br/>";
echo $_SESSION['username']."<br/>";
echo $_SESSION['telno']."<br/>";
?>
<?php
//display records from table -useradvert (child table cannot display)
while($row = $res->fetch_array()){
"<br/>";
"<br/>";
"<br/>";
echo $_SESSION['id']."<br/>";
echo $_SESSION['name2']."<br/>";
echo $_SESSION['color2']."<br/>";
echo $_SESSION['hobby2']."<br/>";
echo $_SESSION['radiobtn']."<br/>";
echo $_SESSION['kupon']."<br/>";
echo $_SESSION['image']."<br/>";
echo $_SESSION['image2']."<br/>";
}
?>
请帮忙
答案 0 :(得分:0)
下面的答案(我正在分享这个,所以世界上有同样问题的人都可以将此作为指导。)
if(isset($_POST['username'])){
$userName = $_POST['username'];
$query = "SELECT id, name, username, telno FROM users WHERE username = ?";
$stmt = $conn->prepare($query);
$stmt->bind_param('s', $userName);
$stmt->execute();
$res = $stmt->get_result();
$row = $res->fetch_array();
$_SESSION['id'] = $row['id'];
$_SESSION['name'] = $row['name'];
$_SESSION['username'] = $row['username'];
$_SESSION['telno'] = $row['telno'];
$query = "SELECT useradvert.name2, useradvert.color2, useradvert.hobby2,useradvert.radiobtn, useradvert.kupon, useradvert.image, useradvert.image2 FROM users INNER JOIN useradvert ON users.id=useradvert.id ";
$stmt = $conn->prepare($query);
$stmt->execute();
$res = $stmt->get_result();
$row2 = $res->fetch_array();
// $_SESSION['name'] = $row2['name'];
$_SESSION['name2'] = $row2['name2'];
$_SESSION['color2'] = $row2['color2'];
$_SESSION['hobby2'] = $row2['hobby2'];
$_SESSION['radiobtn'] = $row2['radiobtn'];
$_SESSION['kupon'] = $row2['kupon'];
$_SESSION['image'] = $row2['image'];
$_SESSION['image2'] = $row2['image2'];
}
?>
在同一页面下面提取..
<?php
//display record from table- users (parent table)
echo $_SESSION['id']."<br/>";
echo $_SESSION['name']."<br/>";
echo $_SESSION['username']."<br/>";
echo $_SESSION['telno']."<br/>";
?>
在同一页面下面提取..
<?php
//display record from table- useradveret -(child table)
while($row = $res->fetch_array()){
// echo $_SESSION['name']."<br/>";
echo $_SESSION['name2']."<br/>";
echo $_SESSION['color2']."<br/>";
echo $_SESSION['hobby2']."<br/>";
echo $_SESSION['radiobtn']."<br/>";
echo $_SESSION['kupon']."<br/>";
echo $_SESSION['image']."<br/>";
echo $_SESSION['image2']."<br/>";}
?>