0

我知道编程时最好的方法不是禁用错误,自从我开始制作我的简单网络论坛以来,我一直遇到一系列错误,我试图解决一些问题,但我遇到了一些问题,这undefined variable是最常见的错误,例如我似乎必须声明一些类似的错误,A='';A=values; 我不知道如何解决下面的错误

Notice: Undefined variable: act in C:\xampp\htdocs\mysite\forum part two\login.php on line 74

第 74 行的代码是这样的

case "login";

login和我的其他代码login.php

switch($act){

default;
index();
break;

case "login";
login();
break;

}

我的代码login.php是这个

<?php
session_start();
//This displays your login form
function index(){
echo "<form action='?act=login' method='post'>" 
    ."Username: <input type='text' name='username' size='30'><br>"
    ."Password: <input type='password' name='password' size='30'><br>"
    ."<input type='submit' value='Login'>"
    ."</form>";    
}
//This function will find and checks if your data is correct
function login(){
//Collect your info from login form
$username = $_REQUEST['username'];
$password = $_REQUEST['password'];
//Connecting to database
$connect = mysql_connect("localhost", "root", "nokiae71");
if(!$connect){
die(mysql_error());
}
//Selecting database
$select_db = mysql_select_db("forumStructure", $connect);
if(!$select_db){
die(mysql_error());
}
//Find if entered data is correct
$result = mysql_query("SELECT * FROM users WHERE username='$username' AND password='$password'");
$row = mysql_fetch_array($result);
$id = $row['id'];
$select_user = mysql_query("SELECT * FROM users WHERE id='$id'");
$row2 = mysql_fetch_array($select_user);
$user = $row2['username'];

if($username != $user){
die("Username is wrong!");
}
$pass_check = mysql_query("SELECT * FROM users WHERE username='$username' AND id='$id'");
$row3 = mysql_fetch_array($pass_check);
$email = $row3['email'];
$select_pass = mysql_query("SELECT * FROM users WHERE username='$username' AND id='$id' AND email='$email'");
$row4 = mysql_fetch_array($select_pass);
$real_password = $row4['password'];
if($password != $real_password){
die("Your password is wrong!");
}
//Now if everything is correct let's finish his/her/its login
session_register("username", $username);
session_register("password", $password);
echo "Welcome, ".$username." please continue on our <a href=index.php>Index</a>";
}

switch($act){
default;
index();
break;
case "login";
login();
break;
}
?> 
4

3 回答 3

1

可能你没有定义$act变量..

$act = isset($_GET['act']) ? $_GET['act'] : '';

和 incase语句不要使用分号

case "whatever":
               ^
于 2012-12-05T08:11:57.517 回答
1

将其添加到您的代码之上:

$act = !empty($_REQUEST['act']) ? $_REQUEST['act'] : null;

即使你把所有这些全局的东西都放在一个名为 example 的文件中,然后将它包含到其他页面中,这可能session_start();$act = ...更好gloabls.inc.php

于 2012-12-05T08:13:49.250 回答
1

这很可能意味着您没有声明变量 $act。

您可以通过在 switch 语句周围添加条件来处理此问题:

if (isset($act)) {
    switch($act) {
        // Do what you need here
    }
}
else {
    echo '$act is not set';
}

这样,您可以使用 else 条件来处理 $act 为空或未设置而不抛出警告的情况。

于 2012-12-05T08:17:04.660 回答