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
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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 //

}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.