如何将一行连接到数据库中的表? (PhpmyAdmin)

时间:2019-02-20 06:04:06

标签: php mysqli phpmyadmin

我目前正在为我们学校使用一个系统,并且我有一个注册/登录/考勤系统。

问题是我想让一个位于表行中的用户连接到另一个表。对他来说,只有他才能访问该表,因为似乎每当我们登录另一个用户时,它都会在同一网页上重定向。

我有办法解决这个问题吗?先感谢您。 :)

  <?php
$mymail = $_POST["mymail"];
$mypass = $_POST["mypass"];

echo "$mymail";

//database connection to check inside table and query email and password
$servername = "localhost";
$username = "root";
$password = "admin";
$dbname = "sistema";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT * FROM userlogin where mymail ='" . $mymail . "' and mypass = '" . $mypass . "'";
$result = $conn->query($sql);

// echo $sql;

if ($result->num_rows > 0) {
    // output data of each row
    // while($row = $result->fetch_assoc()) {
    //     echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["idnumber"]. "<br>";
    // }

header("Location: http://localhost/sistema/attendance/index.php"); /* Redirect browser */

} else {
header("Location: http://localhost/sistema/signup/index2.html"); /* Redirect browser */
    // echo "0 results";
}
$conn->close();
?>

///这只是与数据库的连接以及查询,我分离了html表单

2 个答案:

答案 0 :(得分:1)

您可以在会话

中存储他的用户ID
$_SESSION["id"]

您必须在每个页面上开始会话,并且无论如何您都可以在网站上使用该用户ID  拨打unset($_SESSION["id"]); & unset($_SESSION["id"]);

如果他单击注销

dictlist=[['A', '0.0'], ['B', '0.0'], ['C', '10.12'], ['D', '0.0'], ['E', '0.0'], ['F', '0.0'], ['G', '2.15'], ['H', '0.0'], ['I', '0.0'], ['J', '0.0'], ['K', '0.0'], ['L', '0.0']]

答案 1 :(得分:0)

<?php
//Start session
    session_start();
    $servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

   $conn = mysqli_connect($dbhost, $dbuser, $dbpass,$dbname);

  if(! $conn){
      die('not conected :'.mysqli_error());
  }

#$conn =new mysqli("localhost","root","","mystudy");
    $email = $_POST['email'];
    $password = $_POST['psw'];
    $qry="SELECT * FROM users_table WHERE user_email='$email' AND user_password='$password'";
    $result=mysqli_query($conn,$qry);
    if($result) {
        if(mysqli_num_rows($result) > 0) {
            //Login Successful
        $users = mysqli_fetch_assoc($result);
        session_regenerate_id();
        $_SESSION['SESS_user_ID'] = $users['id'];
        $_SESSION['SESS_user'] = $users['user_name'];
        $_SESSION['SESS_user_EMAIL'] = $users['user_email'];
        session_write_close();
        header("Location: /any your path url/page.php");
        }else{
        header("Location: /any your path url/error_page.php");
        }
?>

index.php

<?php
//Start session
    session_start();
    $userid=$_SESSION['SESS_user_ID'];
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT * FROM users_table WHERE id='$userid'";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {


?>

<table>
  <tr>
    <th>ID</th>
    <th>NAME</th>
    <th>EMAIL</th>
  </tr>
  <tr>
  <?php
  while($row = mysqli_fetch_assoc($result)) {


    <td><?php echo $row["id"];?></td>
    <td><?php echo $row["username"];?></td>
    <td><?php echo $row["email"];?></td>
    <?php } }?>
  </tr>
</table>