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?
$_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.