isset 值总是返回 1

2022-01-05 00:00:00 sql php phpmyadmin isset

我是 PHP 的新手.我正在尝试制作登录和注销网页.现在,我创建了一个可以添加记录的页面.我的代码总是从我构建的表单中返回 1,而不管表单中的值如何.我的代码-

I am a newbie when it comes to PHP. I am trying to make a login and logout webpage. Now, I have created a page which would add records. MY code always returns 1 from the form that i built, irrespective of the value in the form. My code-

<?php
    $servername = "localhost";
    $username = "username";
    $password = "password";
    $alpha = isset($_GET["username"]);
    $beta = isset($_GET["password"]);
    $gama = isset($_GET["hobby"]);
    // Create connection
    $conn = mysqli_connect("localhost","root" ,"","member");

    // Check connection
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }

    $sql = "INSERT INTO data (username,password,hobby)
        VALUES ($alpha, $beta, $gama)";

    if ($conn->query($sql)=== TRUE) {
        echo $alpha."Your record is added into our database ";
    } else {
        echo $sql.$conn->error;
    }                                          
?>

这是我的表格:

<form action="add.php">
  <table> 
    <tr>
        <td>Username :</td>  <td><input type="text" name="username"></td>
    </tr>
    <tr>    
        <td>Password :</td>   <td><input type="text" name="username"></td>
    </tr>
    <tr>
        <td>Hobbies:</td>   <td><textarea name="hobby" id="hobby" cols="22" rows="7"></textarea></td>
    </tr>
    <tr>
        <td>    </td><td><input type="submit" name="submit"></td>
    </tr>

  </table>
</form>

推荐答案

发生这种情况是因为 isset() 返回一个布尔值.

It happens because isset() returns a boolean value.

如果你想直接打印username字段,你可以:

If you want to directly print the username field you can just:

if(isset($_GET["username"])) { 
    echo $_GET["username"];
}

所以你应该明白变量 $alpha, $beta, $gamma 不保存你期望的实际字符串,而是一个布尔值价值

So you should understand that the variables $alpha, $beta, $gamma do not hold the actual string you expect but a boolean value

查看更多信息:http://php.net/manual/en/function.isset.php

相关文章