isset值始终返回1 [英] isset value always returns 1

查看:101
本文介绍了isset值始终返回1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于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/zh/function .isset.php

这篇关于isset值始终返回1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆