我正在尝试使用以下代码登录用户。
该代码似乎可以正常工作,并且用户登录正常。但是,当我要求它回显带有用户ID(仅数字)的MYSQL结果时,我得到以下结果:
x 111
在我的数据库中找不到字母x,我正在尝试使用它:
preg_replace("/[^0-9]/", "", $row['user_id']);
摆脱x,但无法正常工作,字母x仍然保留。
请有人可以告诉我我要去哪里了吗
<?php ob_start();
require 'connect.php';
if(!isset($_SESSION)) {
session_start(); }
if($_SERVER["REQUEST_METHOD"] == "POST") {
// username and password sent from form
$myusername = mysqli_real_escape_string($mysqli,$_POST['username']);
$mypassword = mysqli_real_escape_string($mysqli,$_POST['password']);
$sql = "SELECT user_id FROM user_data WHERE username = '$myusername' and password = '$mypassword'";
$result = mysqli_query($mysqli,$sql);
$row = mysqli_fetch_array($result,MYSQLI_ASSOC);
//$active = $row['loggedIn'];
$count = mysqli_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count == 1) {
if (!isset($_SESSION["myusername"])) {
$_SESSION['login_user'] = preg_replace("/[^0-9]/", "", $row['user_id']);
$res = $_SESSION['login_user'];
$sql = "UPDATE user_data SET loggedIn='True' WHERE username = '$myusername'";
echo $res;
//header('Location: ' . $_SERVER['HTTP_REFERER']);
die;
}else {
echo "Your Login Name or Password is invalid";
}
}
}
?>
<script>
if ( window.history.replaceState ) {
window.history.replaceState( null, null, window.location.href );
}
</script>
<div class="form">
<h1>Log In</h1>
<form action="" method="post" name="login">
<input type="text" name="username" placeholder="Username" class="lg_box" required />
<input type="password" name="password" placeholder="Password" class="lg_box"required />
<input name="submit" type="submit" value="Login" />
</form>
<p>Not registered yet? <a href='registration.php'>Register Here</a></p>
</div>
<style>
.lg_box {
width:100%;
height:35px;
border:1px solid #ccc;
margin-top:10px;
border-radius: 4px;
padding:10px;
margin-bottom:20px;
}
</style>