My code is suppose to save a year like 1999 to the mysql database but it wont. It will check to see if the user has entered only numbers and is at least 4 numbers long or if nothing has been entered correctly but it wont save the correct year? How can I fix this problem.
Here is the PHP code.
if(isset($_POST['year']) && intval($_POST['year']) && strlen($_POST['year']) == 4) {
$year = mysqli_real_escape_string($mysqli, $purifier->purify(htmlentities(strip_tags($_POST['year']))));
} else if($_POST['year'] && strlen($_POST['year']) < 4) {
echo '<p class="error">year is not correct!</p>';
} else if($_POST['year'] == NULL) {
// do something
}
Here is where the code will be going.
if (mysqli_num_rows($dbc) == 0) {
$mysqli = mysqli_connect("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"INSERT INTO users (user_id, year)
VALUES ('$user_id', '$year')");
}
if ($dbc == TRUE) {
$dbc = mysqli_query($mysqli,"UPDATE users
SET year = '$year'
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 code together.
if (isset($_POST['submit'])) {
if(isset($_POST['year']) && intval($_POST['year']) && strlen($_POST['year']) == 4) {
$year = mysqli_real_escape_string($mysqli, $purifier->purify(htmlentities(strip_tags($_POST['year']))));
} else if($_POST['year'] && strlen($_POST['year']) < 4) {
echo '<p class="error">year is not correct!</p>';
} else if($_POST['year'] == NULL) {
if (mysqli_num_rows($dbc) == 0) {
$mysqli = mysqli_connect("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"INSERT INTO users (user_id, year)
VALUES ('$user_id', '$year')");
}
if ($dbc == TRUE) {
$dbc = mysqli_query($mysqli,"UPDATE users
SET year = '$year'
WHERE user_id = '$user_id'");
echo '<p class="changes-saved">Your changes have been saved!</p>';
}
if (!$dbc) {
print mysqli_error($mysqli);
return;
}
}
}
The problem has to be on this line.
if(isset($_POST['year']) && intval($_POST['year']) && strlen($_POST['year']) == 4) {
$year = mysqli_real_escape_string($mysqli, $purifier->purify(htmlentities(strip_tags($_POST['year']))));