I get the following error on line 8: Undefined index: privacy_policy
which is $privacy_policy = mysqli_real_escape_string($mysqli, $_POST['privacy_policy']);
I was wondering how can I fix this problem?
Here is line 8.
$privacy_policy = mysqli_real_escape_string($mysqli, $_POST['privacy_policy']);
Here is the PHP.
if (isset($_POST['submitted'])) {
$mysqli = mysqli_connect("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"SELECT users.*
FROM users
WHERE user_id=3");
$privacy_policy = mysqli_real_escape_string($mysqli, $_POST['privacy_policy']);
if (mysqli_num_rows($dbc) == 0) {
$mysqli = mysqli_connect("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"INSERT INTO users (user_id, privacy_policy)
VALUES ('$user_id', '$privacy_policy')");
}
if ($dbc == TRUE) {
$dbc = mysqli_query($mysqli,"UPDATE users
SET privacy_policy = '$privacy_policy'
WHERE user_id = '$user_id'");
开发者_开发技巧 echo '<p class="changes-saved">Your changes have been saved!</p>';
}
if (!$dbc) {
print mysqli_error($mysqli);
return;
}
}
Here is the HTML.
<form method="post" action="index.php">
<fieldset>
<ul>
<li><input type="checkbox" name="privacy_policy" id="privacy_policy" value="yes" <?php if (isset($_POST['privacy_policy'])) { echo 'checked="checked"'; } else if($privacy_policy == "yes") { echo 'checked="checked"'; } ?> /></li>
<li><input type="submit" name="submit" value="Save Changes" class="save-button" />
<input type="hidden" name="submitted" value="true" />
<input type="submit" name="submit" value="Preview Changes" class="preview-changes-button" /></li>
</ul>
</fieldset>
</form>
If the warning is saying that $_POST['privacy_policy']
is undefined then that is (almost) undoubtedly true.
Your privacy policy value comes from a check box and if the check box is unchecked when you submit the form, then there will be no entry for it in the $_POST
array. If it is checked when you submit the form then the value in $_POST['privacy_policy']
will be yes
.
Before you assign the value in $_POST['privacy_policy']
to something, check that it is set with isset
. This will stop your warning from appearing.
update
I would deal with an unchecked check box like this:
$privacy_policy = "no";
if(isset($_POST['privacy_policy']))
{
$privacy_policy = mysqli_real_escape_string($mysqli, $_POST['privacy_policy']);
}
Add a hidden field with the default value if the "privacy_policy" checkbox is not selected. Then the $_POST variable will contain a value. When the checkbox is selected, it will override the value of the hidden field (as long as it follows the hidden field).
<form method="post" action="index.php">
<!-- add a default value for the privacy_policy -->
<input type="hidden" name="privacy_policy" value="no"/>
<fieldset>
<ul>
<li><input type="checkbox" name="privacy_policy" id="privacy_policy" value="yes" <?php if($privacy_policy == "yes") { echo 'checked="checked"'; } ?> /></li>
<li><input type="submit" name="submit" value="Save Changes" class="save-button" />
<input type="hidden" name="submitted" value="true" />
<input type="submit" name="submit" value="Preview Changes" class="preview-changes-button" /></li>
</ul>
</fieldset>
</form>
EDIT:
As Matt Ellen also correctly points out, you can check to see that $_POST['privacy_policy']
is set using isset()
or array_key_exists()
and set a default in the PHP as well.
$privacy_policy = array_key_exists('privacy_policy', $_POST) ? $_POST['privacy_policy'] : false;
精彩评论