Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
312 views
in Technique[技术] by (71.8m points)

php - isset value always returns 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;
    }                                          
?>

And this is my form:

<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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It happens because isset() returns a boolean value.

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

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

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

See more at: http://php.net/manual/en/function.isset.php


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...