PHP again guys.... still in the dark i guess | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

PHP again guys.... still in the dark i guess

i tried running this code and i know that i created an array of $errors but i don't know why am getting this error: Notice: Undefined variable: errors in C:\xampp\htdocs\server.php on line 50 Here is the code: <? session_start(); $username = ""; $email = ""; $errors = array(); //connecting to the database $db = mysqli_connect('localhost', 'root', '', 'registration'); include('errors.php'); //if the register button is clicked if (isset($_POST['register'])) { $username =mysql_real_escape_string($_POST['username']); $email =mysql_real_escape_string($_POST['email']); $password_1 =mysql_real_escape_string($_POST['password_1']); $password_2 =mysql_real_escape_string($_POST['password_2']); //ensure that form fields are filled properly if ($username == "") { array_push($errors, "username is requied"); // add error to errors array } if ($email == "") { array_push($errors, "Email is requied"); // add error to errors array } if ($password_1 == "") { arry_push($errors, "Password is requied"); // add error to errors array } if ($password_1 != $password_2) { array_push($errors, "The two passwords does not match"); // add error to errors array } // if ther are no errors, save user too database if (count($errors) == 0) { $password = md5($password_1); //encrypt password before storing in database (security) $sql = "INSERT INTO users (username, email,password) VALUES ('$username', '$email', '$password')"; mysqli_query($db, $sql); $_SESSION['username'] = $username; $_SESSION['success'] = "You are now logged in"; header('location: index.php'); // redirects to homepage } } ?> <?php if (count($errors) > 0): ?> <div class="error"> <?php foreach ($errors as $error): ?> <p><?php echo $error; ?></p> <?php endforeach ?> </div> <?php endif ?> i really could use your help please

14th Dec 2017, 1:51 PM
Okoro Ikechukwu Stephen
Okoro Ikechukwu Stephen - avatar
4 Answers
+ 1
can you send the error .php too?
15th Dec 2017, 11:15 AM
Lexfuturorum
Lexfuturorum - avatar
+ 1
ohh yeah set too $username = htmlspecialchars($_POST["username"]) to prevent sql injektion attacks or worse php injektion witch can get your server crashed or infected by any rootkit/virus
15th Dec 2017, 11:23 AM
Lexfuturorum
Lexfuturorum - avatar
+ 1
same for any userinput
15th Dec 2017, 11:27 AM
Lexfuturorum
Lexfuturorum - avatar
+ 1
ok im guesing the error is in line 50 you wrote $error instead of $errors
15th Dec 2017, 11:38 AM
Lexfuturorum
Lexfuturorum - avatar