PHP undefined key and variable

0 votes
I followed your tutorial here about user registration using PHP and MySQL here: https://youtu.be/qjwc8ScTHnY

However, Im getting these errors:

Warning: Undefined array key "username" in C:\xampp\htdocs\Sublime\Web1\server.php on line 17

Warning: Undefined array key "email" in C:\xampp\htdocs\Sublime\Web1\server.php on line 18

Warning: Undefined array key "password" in C:\xampp\htdocs\Sublime\Web1\server.php on line 19

Warning: Undefined array key "confirmpassword" in C:\xampp\htdocs\Sublime\Web1\server.php on line 20


This are those line:

$username = mysqli_real_escape_string($db, $_POST['username']);
$email = mysqli_real_escape_string($db, $_POST['email']);
$password = mysqli_real_escape_string($db, $_POST['password']);
$confirmpassword = mysqli_real_escape_string($db, $_POST['confirmpassword']);

I did a work around by doing this:

if (isset($_POST['username']) || isset($_POST['email']) || isset($_POST['password']) || isset($_POST['confirmpassword'])) {

    $username = mysqli_real_escape_string($db, $_POST['username']);
    $email = mysqli_real_escape_string($db, $_POST['email']);
    $password = mysqli_real_escape_string($db, $_POST['password']);
    $confirmpassword = mysqli_real_escape_string($db, $_POST['confirmpassword']);
}

But now Im getting undefined variable at line 30 which are $password and $confirmpassword which is the line:

if ($password != $confirmpassword) {array_push($errors, "Password must be the same");};

Please help! And why am I getting these errors? Thanks!
Dec 29, 2020 in PHP by Dann carlo
• 120 points
45,704 views

1 answer to this question.

0 votes

Hi, @Dann,

If you are using some variable then please check if its set or not

<?=$user_name;?> you need to chnage to <?=(isset($user_name)?$user_name:""?> and same for ot
answered Dec 30, 2020 by anonymous
• 65,770 points

Related Questions In PHP

0 votes
1 answer

Error:“Notice: Undefined variable”, “Notice: Undefined index”, and “Notice: Undefined offset” using PHP

Hello @kartik, The best way for getting input string is: $value ...READ MORE

answered Apr 1, 2020 in PHP by Niroj
• 82,840 points
37,596 views
0 votes
1 answer

What is meant by passing the variable by value and reference in PHP?

Hello, When the variable is passed as value ...READ MORE

answered Mar 27, 2020 in PHP by Niroj
• 82,840 points
3,286 views
0 votes
0 answers

What's quicker and better to determine if an array key exists in PHP?

$key = 'jim'; // example 1 if (isset($array[$key])) { ...READ MORE

Jun 1, 2022 in PHP by Kichu
• 19,040 points
515 views
0 votes
0 answers

How to push both value and key into PHP array

Take a look at this code: $GET = ...READ MORE

Jul 25, 2022 in PHP by Kithuzzz
• 38,000 points
1,598 views
0 votes
1 answer

How to validate E-mail and URL of Php form?

hey, The code below shows a simple way ...READ MORE

answered Feb 13, 2020 in PHP by manish
2,382 views
0 votes
1 answer

Complete PHP form with proper validation and syntax

Hey @kartik, It's quite simple to have php ...READ MORE

answered Feb 19, 2020 in PHP by Niroj
• 82,840 points
1,564 views
0 votes
2 answers

Define a SQL query? What is the difference between SELECT and UPDATE Query? How do you use SQL in SAS?

HI.. SQL is Structured Query Language, which is ...READ MORE

answered Aug 8, 2020 in PHP by anonymous
10,602 views
0 votes
1 answer

Vertically centering text in a div

Try this code: .smallUnitBox { ...READ MORE

answered Sep 3, 2018 in Blockchain by digger
• 26,740 points
876 views
0 votes
1 answer
0 votes
0 answers

Anyone can help me out to understand the semantic of (document.getElementBYId("demo").innerHTML="Hello") ?

Hello guys, Can Someone helps me to find ...READ MORE

Jan 17, 2020 in Web Development by anonymous
• 37,520 points
954 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP