1

I have a PHP variable $admin which corresponds to a checkbox. When the form is submitted with the checkbox ticked, the value is passed (set as 1) and everything is fine, however if it's not ticked the variable causes an undefined index error:

Notice: Undefined index: admin in C:...\admin.php

This is the php section of the form that queries the db:

  if($row == 1)
    {
        echo '<div id="errormsg">This username is already taken</div>';
    }
    else

    {
    $add = mysqli_query($dbcon, "INSERT INTO users (id, firstname, lastname, username, password, admin) VALUES
    (null, '$fname', '$lname', '$user', '$pass', '$admin') ") or die ("Can't insert data");
    echo '<div id="create-success">Successfully added user!</div>';
    }

And the HTML form:

  <form action="admin.php" method="post" onSubmit="return validate(this)">
<fieldset>
    <label class="reg">Username *</label> <input type="text" name="user" /><br />
    <label class="reg">Password *</label> <input type="password" name="pass" /><br />
    <label class="reg">Repeat Password *</label> <input type="password" name="rpass" /><br />
    <label class="reg">First name:</label> <input type="text" name="fname" /><br />
    <label class="reg">Last name:</label> <input type="text" name="lname" /><br />
    <label class="reg">Admin?:</label> <input type="checkbox" value="1" name="admin" /><br/>
</fieldset>
    <input type="submit" name="submit" value="Create User" />
</form>

Ha anyone any ideas why NOT checking the box causes an error specific to the $admin variable?

3
  • I'm guessing that when the box is not checked it's sending no or NULL value. You should intercept the POST first, set the variable manually yourself. That way you know it has a valid value. Commented Apr 16, 2014 at 19:14
  • You should show the portion of your script that deals with handling values in $_POST. in all likelihood, this is where your problem lies. You may also consider not telling the user if a username has already been taken. This is considered bad practice from a security standpoint. Commented Apr 16, 2014 at 19:17
  • The POST's are simply set as $admin = $_POST['admin']; etc Commented Apr 16, 2014 at 19:29

2 Answers 2

4

Checkboxes are only actually passed to the script if they are checked.

So you need to check for there existance in your php code, something like this:

if ( $_POST && isset( $_POST['admin'] ) {
    $admin = $_POST['admin'];
} else {
    $admin = 0;
}

EDIT:

In your script I would do this

if($row == 1) {
    echo '<div id="errormsg">This username is already taken</div>';
} else {
    $admin = ( $_POST && isset( $_POST['admin'] ) ? $_POST['admin'] : 0;
    $add = mysqli_query($dbcon, "INSERT INTO users 
               (id, firstname, lastname, username, password, admin) 
         VALUES(null, '$fname', '$lname', '$user', '$pass', '$admin') ") or die ("Can't insert data");
    echo '<div id="create-success">Successfully added user!</div>';
}
Sign up to request clarification or add additional context in comments.

4 Comments

I thought it could be something to do with that. Where would that code fit into my script?
Anywhere before you try use the $admin variable
I was after something like this but its not stopped the error unfortunately
What is the exact error message and show the line that is it occuring on
1

First, you have to check is that admin variable in Post Array. Please check code.

<?php
if($row == 1)
{
 echo '<div id="errormsg">This username is already taken</div>';
}
else
{
// check is exist in post array
$isAdminCheck = (isset($_POST['admin']) && $_POST['admin'] == 1 )? $_POST['admin']:0;

$add = mysqli_query($dbcon, "INSERT INTO users (id, firstname, lastname, username,   password, admin) VALUES    (null, '$fname', '$lname', '$user', '$pass', '$isAdminCheck') ") or die ("Can't insert data");
echo '<div id="create-success">Successfully added user!</div>';
}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.