Jump to content

Uni project Undefined index :S


jamesyrawr

Recommended Posts

I'm building a site for a uni project and i seem to be getting these errors back from it

this script allows the user to update their info once they have logged in so if you notice anything else i need please say :)

 

the errors i get are:

Notice: Undefined index: firstname in C:\wamp\www\flerp\update_conn.php on line 14

 

Notice: Undefined index: lastname in C:\wamp\www\flerp\update_conn.php on line 15

 

Notice: Undefined index: email in C:\wamp\www\flerp\update_conn.php on line 16

 

 

<?php
$host="localhost";
$username="root";
$password=""; 
$db_name="flerpusers";
$tbl_name="users";


mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");


$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];





$sql = "UPDATE `$tbl_name` SET `firstname` = '$firstname',`lastname` = '$lastname',`email` = '$email'";

mysql_query($sql) or die ("Error: ".mysql_error());

echo "Database updated. <a href='editinfo.php'>Return to edit info</a>";

?>

Link to comment
https://forums.phpfreaks.com/topic/215879-uni-project-undefined-index-s/
Share on other sites

You are referencing $_POST['firstname'], $_POST['lastname'], and $_POST['email'] when those variables don't exist, hence undefined index errors ($_POST is an array and is referenced using the index names of its elements.)

 

You would need to determine why those variables don't exist.

At a wild guess, I would say either

 

1.  Your form is using the get method, not the post method

2.  Or, your input tags have different names to what you are looking for in the script

3.  Or, the input tags are not inside the form you are submitting

You might want to reference these variables with an if isset statement to stop the error when the page loads before submitting the form!

Best practice is to reference the form name.

 

e.g

 

if(isset($_POST['form_name'])){

$firstname = $_POST['firstname'];

$lastname = $_POST['lastname'];

$email = $_POST['email'];

 

// etc... do other stuff //

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.