Using $ _POST or $ _GET to retrieve the variables from a form, you may encounter error:
Notice: Undefined index
To avoid this error, use function isset ().
isset — Determine if a variable is set and is not NULL
if (isset($_POST['value'])) {
// Some instructions if $_POST['value'] exist
} else {
// $_POST['value'] is not set, do something and raise an error
}
if(isset($_GET['action'];)) {
$action = $_GET['action'];
} else {
// ERROR CODE
}
You should use isset() instead of empty(), so you avoid the undefined index error:
$term = $_POST['term'];
if (empty($term)){
$term = $_GET['term'];
}
change to :
if(!isset($_POST['term'])){
if (isset($term) && $term ) {
$term = $_GET['term'];
}
}
// if the form's submit button is clicked, we need to process the form
if (isset($_POST['submit'])) {
// make sure the 'id' in the URL is valid
if (is_numeric($_POST['id'])) {
// get variables from the URL/form
$id = $_POST['id'];
$name = $_POST['name'];
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];
// check that name,username, password and email are not empty
if ($name == '' || $username == '' || $password == '' || $email == '') {
// if they are empty, show an error message and display the form
$error = 'ERROR: Please fill in all required fields!';
}
else {
// if everything is OK, update the record in the database
if ($stmt = $mysqli->prepare("UPDATE admin_login SET name = ?, username = ?, password = ?, email = ? WHERE id=?")) {
$stmt->bind_param("ssssi", $name, $username, $password, $email, $id);
$stmt->execute();
$stmt->close();
}
// show an error message if the query has an error
else {
echo "ERROR: could not prepare SQL statement.";
}
// redirect the user once the form is updated
header("Location: admin_user.php");
}
}
// if the 'id' variable is not valid, show an error message
else {
echo "Error!";
}
}
Previous page: The Basics of PHP Objects
Next page: Deprecated: mysql_db_query()