如何使用 PHP 在数据库中创建 user_id?

How to get the user_id created in Database using PHP?

我尝试使用或不使用SESSION。也许我在使用 SESSION 时遇到问题。 在 phpMyAdmin 中我使用 userid int(11) auto_increment.

所有使用 POST 方法创建的内容都有效。

<?php
session_start();    
$con = mysqli_connect("mysql11.000webhost.com", "a7083778_user",      "abcd12734", "a70830778_data");

$username = $_POST["username"];
$password = $_POST["password"];

$statement = mysqli_prepare($con, "SELECT * FROM user WHERE username = ? AND password = ?");
mysqli_stmt_bind_param($statement, "ss", $username, $password);
mysqli_stmt_execute($statement);

mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $userID, $name, $age ,$username, $password, $userPoints);

$response = array();
$response["success"] = false;  
$_SESSION["userid"] = $userID;
while(mysqli_stmt_fetch($statement)){
    $response["success"] = true;
$response["userID"] = $_SESSiON["userid"]; 
    $response["name"] = $name;
    $response["age"] = $age;
    $response["username"] = $username;
    $response["password"] = $password;
$response["userPoints"] = $userPoints;
}

echo json_encode($response);
 ?>

请试试这个,我认为它对你有用

<?php
session_start();    
$conn = new mysqli("mysql11.000webhost.com", "a7083098_user", "abcd1234", "a7083098_data");
$username = $_POST["username"];
$password = $_POST["password"];
$qry = "SELECT * FROM user WHERE username = $username AND password = $password";
$result =  $conn->query($qry);
$response = array();
while($row = $result->fetch_assoc())
{
 $response["success"] = true;
 $response["userID"] = $row['userId'];
 $_SESSiON["userid"] = $response["userID"];   //Store userId in session
 $response["name"] = $row['name'];
 $response["age"] = $row['age'];
 $response["username"] = $row['username'];
 $response["password"] = $row['password'];
 $response["userPoints"] = $row['userPoints'];
}
echo json_encode($response);
session_destroy();

?>