dqxmf02844 2016-07-24 00:59
浏览 181

注册和登录

Im countering sort of a weird problem at this moment. I made a simple login form, it works halfway thru. You can indeed write your name in and such but once you hit the Submit button, I get some " Undefined index: nameX" symbols eventho I have added the variable for it before the function and such.

This is the content of the sql_connection.sql (well part of it you have to see atleast)

/* Registration */
    $nameX = $_POST['name'];

    $query = isRegistered($_POST['name']); //this will check if they are valid or not

    if(isset($_POST['submit'])) {
        if(!empty($_POST['name'])) {
            if($query == true) {
                echo "Welcome back";
            }
        }
        else {
            echo "No accounts found";
        }
    }

function isRegistered($name) {
        global $handler;

        $query = "SELECT ID FROM players WHERE Username= '".$name."'";

        $result = mysqli_query($handler, $query);

        if(mysqli_num_rows($result) != 0) {
            while($row = mysqli_fetch_assoc($result)) {
                return true;
            }
        }
        else {
            return false;
        }
    }

and this is the actual form

<div id="login">
            <form action="includes/sql_connection.php" method="get">
                First name: <input type="text" name="name"><br>
                <input id="button" type="submit" name="submit" value="Sign-Up">
            </form>
        </div>

The error

Notice: Undefined index: name in C:\wamp\www\php\includes\sql_connection.php on line 101

and the error line (line: 101)

$nameX = $_POST['name'];
  • 写回答

1条回答 默认 最新

  • douli1306 2016-07-24 01:11
    关注

    A couple of quick observations

    Your form triggers a GET request, but your code is looking at POST inputs. You'll never see anything. John Conde pointed that out.

    You also have a logic flaw

    $nameX = $_POST['name'];    
    $query = isRegistered($_POST['name']);
    
    if(isset($_POST['submit'])) {
        if(!empty($_POST['name'])) {
    

    You're using $_POST['name'] first, then checking whether it's non-empty later. You're accessing form inputs first, then checking whether there are any inputs later (with the isset). That will cause your isRegistered to be called with undefined variables when there is no form input. This order should be reversed.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。