注意:未定义的索引:第5行的C:\ xampp \ htdocs \ locallocal \ admin \ userUpdate.php中的userid

时间:2014-12-06 23:21:13

标签: php

<?php
include 'header.php';
include("../php/functions.php");
if(!isset($_SESSION)) session_start();
if(isset($_POST['userID']))$userID = $_POST['userID'];
$username=sanitiseInput ($_POST['username']);
$firstname=sanitiseInput ($_POST['firstname']);
$surname=$_POST['surname'];
$emailadd=$_POST['emailadd'];

$dayob=$_POST['dayob'];
$monthob=$_POST['monthob'];
$yearob=$_POST['yearob'];
$dob=$yearob."-".$monthob."-".$dayob;
$user_type = $_POST['user_type'];
$tnc=(isset($_POST['tnc'])?1:0);

// check user is old enough, in this example users must be 16

$latestbirthday=mktime(0, 0, 0,date("m"),date("d"),date("Y")-16); // the last value controls min age
//$birthday=mktime(0, 0, 0, substr($dob,5,2), substr($dob,8,2), substr($dob,0,4));
$birthday=mktime(0, 0, 0, $monthob, $dayob, $yearob);

$validage=(($birthday-$latestbirthday)>0?false:true);

$conn = createConnection();

// Check submitted and calculated variables before storing
if($tnc && isset($firstname) && isset($surname) && $validage) {
    if ($_POST['userpass']!="" && $_POST['secondpass']!="" || $_POST['userpass']!=null && $_POST['secondpass']!=null){
        $userpass=$_POST['userpass'];
        $secondpass=$_POST['secondpass'];       
        $salt=getSalt(16);
        $cryptpass=makeHash($userpass,$salt,50);
        // Update user
        $query=mysqli_query($conn,"update users set username='$username', firstname='$firstname', surname='$surname', emailadd='$emailadd', dob='$dob', usertype='$user_type', tnc='$tnc', salt='$salt', userpass='$cryptpass' where userid='$userID'") or die(mysql_error());

    }else{
        // Update user
        $query=mysqli_query($conn,"update users set username='$username', firstname='$firstname', surname='$surname', emailadd='$emailadd', dob='$dob', usertype='$user_type', tnc='$tnc' where userid='$userID'") or die(mysql_error());
        var_dump($query);
        die();      
    }
    // check user updated, if so display message
    if($query){
    echo '<p style="color:red;">User has been successfully updated.</p>';
    echo '<p>You can return to the <a href="edit_user.php">Edit User Page</a></p>'; 
    } else { 
        //feedback there was a problem adding the user
        echo "<p>There was a problem adding your details.</p>";
        echo '<p>You need to return to the <a href="edit_user.php">Edit User Page</a> and try again</p>'; 
        }
}else { 
        //Updation failed either due to disabled javascript or other attempt to bypass validation
?>
        <header>
            <h1>Updation failed</h1>
        </header>
        <?php 
        if($userpass!=$secondpass){ echo "<p>The passwords do not match.</p>"; }
        if(!filter_var($emailadd, FILTER_VALIDATE_EMAIL)){ echo "<p>The email address is invalid.</p>"; }
        ?>
        <p>You need to return to the <a href="edit_user.php">Edit User Page</a> and try again</p>       
<?php 
}

include 'footer.php';
$db->close();
?>

我正在尝试将用户从管理员更新为普通用户。当我更新时出现此错误提示:未定义的变量:第40行的C:\ xampp \ htdocs \ locallocal \ admin \ userUpdate.php中的userID 布尔(真)。因为我是php的新学习者,所以一点点解释都会有所帮助。提前谢谢。

这就是我要更新的用户详细信息

<?php 
include 'header.php';

if(isset($_SESSION['usertype']) && $_SESSION['usertype']>2){
?>

<h1>Update User</h1>

<?php

if(isset($_REQUEST['id'])){

include('../config.php');

$id=$_REQUEST['id'];

//this query will select the user data which is to be used to fill up the form

$query=mysqli_query($conn,"select userid, username, firstname, surname, dob, emailadd, usertype,
                                    EXTRACT(YEAR FROM dob) AS dobYear,
                                    EXTRACT(MONTH FROM dob) AS dobMonth,
                                    EXTRACT(DAY FROM dob) AS dobDay
                                    from users where userid='$id'") or die(mysqli_error($conn));

$num=mysqli_num_rows($query);

//just a little validation, if a record was found, the form will be shown

//it means that there's an information to be edited

if($num>0){

$row=mysqli_fetch_assoc($query);

//extract($row);
print_r($row);

$uId =  $row['userid'];
$username =  $row['username'];
$firstname =  $row['firstname'];
$surname =  $row['surname'];
$email = $row['emailadd'];
$dobYear = $row['dobYear'];
$dobMonth = $row['dobMonth'];
$dobDay = $row['dobDay'];
$usertype= $row['usertype'];
?>

<form id="updateuser" name="updateuser" method="post" action="userUpdate.php">

<input type='hidden' name="dobYear" id='dobYear' value="<?php echo $dobYear ?>" />
<input type='hidden' name='dobMonth' id='dobMonth' value='<?php echo $dobMonth ?>' />
<input type='hidden' name='dobDay' id='dobDay' value='<?php echo $dobDay ?>' />

<label for="username">Username : </label><input type="text" id="username" name="username" value="<?php echo $username ?>" required size="15" /><span id="usernameFb"></span><br />
<label for="firstname">First name : </label><input type="text" id="firstname" name="firstname" value="<?php echo $firstname ?>" required size="15" /><span id="firstnameFb"></span><br />
<label for="surname">Surname : </label><input type="text" id="surname" name="surname" value="<?php echo $surname ?>" required size="15" /><span id="surnameFb"></span><br />
<label for="emailadd">Email Address : </label><input type="email" id="emailadd" name="emailadd" value="<?php echo $email ?>" required size="30" /><span id="emailFb"></span><br />

<p></p>
<fieldset style="width:45%; border-radius:5px; border: 1px solid #DEDEDE; background:#EFEFEF">
            <legend>Date of Birth</legend>
            <label class="datelabel" for="dayob" style="display:inline;">Day</label>
            <select name="dayob" id="dayob">
            </select>

            <label class="datelabel" for="monthob" style="display:inline;">Month</label>
            <select name="monthob" id="monthob">
                <option value="1" <?php echo ($dobMonth==1)?'selected="selected"':"" ?>>January</option>
                <option value="2" <?php echo ($dobMonth==2)?'selected="selected"':"" ?>>February</option>
                <option value="3" <?php echo ($dobMonth==3)?'selected="selected"':"" ?>>March</option>
                <option value="4" <?php echo ($dobMonth==4)?'selected="selected"':"" ?>>April</option>
                <option value="5" <?php echo ($dobMonth==5)?'selected="selected"':"" ?>>May</option>
                <option value="6" <?php echo ($dobMonth==6)?'selected="selected"':"" ?>>June</option>
                <option value="7" <?php echo ($dobMonth==7)?'selected="selected"':"" ?>>July</option>
                <option value="8" <?php echo ($dobMonth==8)?'selected="selected"':"" ?>>August</option>
                <option value="9" <?php echo ($dobMonth==9)?'selected="selected"':"" ?>>September</option>
                <option value="10" <?php echo ($dobMonth==10)?'selected="selected"':"" ?>>October</option>
                <option value="11" <?php echo ($dobMonth==11)?'selected="selected"':"" ?>>November</option>
                <option value="12" <?php echo ($dobMonth==12)?'selected="selected"':"" ?>>December</option>
            </select>
            <label class="datelabel" for="yearob" style="display:inline;">Year</label>
            <select name="yearob" id="yearob">
            <?php /* <option value="<?php echo $dobYear?>" selected="selected"></option>*/?>
            </select><span id="ageFb"></span>
        </fieldset>

<label for="userpass">Password : </label><input type="password" id="userpass" name="userpass" size="15" /><span id="userpassFb"></span><br />
<label for="passstrresult" style="display:inline-block;">Strength</label><div id="passstrresult" style="height:20px;display:inline-block;margin-left:15px;"></div>
<label for="secondpass">Password : </label><input type="password" id="secondpass" name="secondpass" size="15" /><span id="secondpassFb"></span><br />

<label for="user_type" style="display:inline-block;">Select User Type :</label>
            <select name="user_type" id="user_type">
                <option value="1" <?php echo (isset($usertype)&&($usertype==1))?'selected="selected"':"" ?>>Suspend</option>
                <option value="2" <?php echo (isset($usertype)&&($usertype==2))?'selected="selected"':"" ?>>Registered</option>
                <option value="3" <?php echo (isset($usertype)&&($usertype>2))?'selected="selected"':"" ?>>Admin</option>
            </select>
<label for="tnc">I agree to the Terms and Conditions</label><input type="checkbox" name="tnc" id="tnc" required /><span id="tncFb"></span><br />

    <!-- so that we could identify what record is to be updated -->

    <input type='hidden' name='id' value='<?php echo $uId ?>' />    
    <p>        
    <input class="submit" id="update" name="update" type="submit" value="Update">
    </p>    
</form>

<?php

}else{

echo "<div>User not found.</div>";

}

}
else{

echo "<div> You are not authorized to view this page";

}


}else{
    echo "<h1>Update User</h1><br>";
    echo "Sorry, Only ''Admin'' can access to this Page...";
} ?>

<!-- js -->
<script src="../js/functions.js"></script>
<script src="../js/update_user.js"></script>

<script>
document.onreadystatechange = function(){
    if(document.readyState=="complete") {
        prepareUpdation();

        //Set the Day value of user Day of Birth
        var valsearch = document.getElementById("dobDay").value;
        var s = document.getElementById("dayob");       
        // Loop through all the items in drop down list
        for (i = 0; i< s.options.length; i++){          
        if (s.options[i].value === valsearch){          
        // Item is found. Set its property and exit
        s.options[i].selected = true;
        break;
        }
        }

        //Set the year value of user Year of Birth
        var valsearch = document.getElementById("dobYear").value;
        var s = document.getElementById("yearob");      
        // Loop through all the items in drop down list
        for (i = 0; i< s.options.length; i++){          
        if (s.options[i].value === valsearch){          
        // Item is found. Set its property and exit
        s.options[i].selected = true;
        break;
        }
        }
    }       

};
</script>
<?php 
include 'footer.php';
?>

1 个答案:

答案 0 :(得分:0)

您的标题的标题指出关联数组中的索引不存在。 这意味着当表单通过POST发送时,数组$ _POST中没有字段'userID',它填充了表单中的数据。

if(isset($_POST['userID']))$userID = $_POST['userID'];

由于缺少字段isset($ _ POST ['userID'])为false且变量$ userID将不会被初始化。

这会导致代码下面的错误。当没有这样的变量时,代码假定变量$ userID。

如果第4行中的var_dumping $ _POST包含字段userID,请检查传输的数据。

<强>更新 在表格中写道:

$uId =  $row['userid'];
.
.
.
<input type='hidden' name='id' value='<?php echo $uId ?>' />

所以你必须寻找像$ _POST ['id']

这样的字段