Jump to content

Undefined Array Key MySQL

 

So I am new at coding, and we needed to make a simple website with some simple functions. Everything seems to work and stuff, but I can't really find the reason why there is text where it says "Undefined Array Key" Isn't there a way to remove this? 

 

Everything in my database and in the code should have the same name. 

code2.PNG

1865199639_Undefinedarraykey.PNG

Link to comment
Share on other sites

Link to post
Share on other sites

I saw some similar looking errors on this website we use for our milsim stuff. We starting getting the errors after our webhost provide moved from I believe 2.0 php version to 2.5 php or something like that.

Link to comment
Share on other sites

Link to post
Share on other sites

You can hide the warnings by putting all the code from insertinput.php into a function and calling the function with an @.

 

So something like:
 

Quote

 

<?php

@insert();

function insert{

//insert function content goes here

}//end insert function
?>

 

 

SPEC LIST:

  • CPU: AMD Ryzen 9 5950X w/ NZXT Kraken Z73 360mm Liquid Cooler
  • GPU: NVIDIA RTX 3090 FE
  • RAM: Corsair Vengeance LPX 32GB (4 x 8GB) 5000MHz CL18
  • Motherboard: MSI MEG X570 Godlike
  • SSD: Samsung 980 Pro PCIe 4.0 1TB (x3)
  • PSU: Corsair AX1600i
  • Case: NZXT H710
  • Monitor: Alienware AW2521H 25inch 360Hz 1ms
Link to comment
Share on other sites

Link to post
Share on other sites

No matter if you submitted the form or not, you're trying to insert a row into the database.

IF the form was not submitted,  PHP doesn't create the  navn  , land and email entries in the $_POST array.

 

You can work around that by checking if the array entries exist and if so, storing the value into variables. then use the variables to compose the sql query string/text. 

The function isset tests if an entry in an array or a variable or object exists  ... and I'm using the short version of  if then else : if  (condition ) ? true statement : false statement ;  to put something in each variable.

 

I'm also figuring out if the form was submitted by checking if the button is added into the $_POST variables, which will happen only if the form was submitted. Another method would to check if $_SERVER['REQUEST_METHOD'] exists and has the value POST but we're getting off topic.

 

$navn =  ( isset($_POST['navn']) == TRUE ) ? $_POST['navn'] :  '';
$land =  ( isset($_POST['land']) == TRUE ) ? $_POST['land'] :  '';
$email =  ( isset($_POST['email']) == TRUE ) ? $_POST['email'] :  '';

// assume the name of the button is "button"  - alternatively you could add a hidden field in the form
// example : <input name="submitted" type="hidden" value="whatever" />

$submitted = ( isset($_POST['button']) == TRUE ) ? $_POST['button'] :  FALSE; // if the form was not submitted, value will be FALSE
if ($submitted !== FALSE) {
  // there was the $_POST['button'] variable, so we probably have some data .
  //
  // add checks if you want ex don't try to insert in database if name is empty or something else is empty
  // you can do stuff here
}

 

Also, you should NEVER  just insert stuff into the database directly from the user input, from the POST array.  You need to escape it, using a function like mysqli_real_escape_string : https://www.php.net/manual/en/mysqli.real-escape-string.php

 

The function has two parameters, the mysql connection and the variable you want escaped ..

Look at the examples in the link above for how to use it.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×