Jump to content

Undefined index ERROR


dweb

Recommended Posts

Hi

 

I'm really confused by an error I keep getting, my code is;

 

<?php
if($_SERVER['REQUEST_METHOD'] == "POST")
{
if(isset($_POST['action']) == 'update')
{
$name = mysql_real_escape_string($_POST['myname']);
mysql_query("UPDATE `users` SET `name` = '".$name."' WHERE `id` = '".$_SESSION['idnum']."'");
echo "1 record added";
}
}
?>

 

it all works ok and records are updated, but when the form is submitted I keep getting the error

 

Notice: Undefined index: myname in e:\www\site\info.php on line 12

 

I've looked around online but all the solutions i've tried have failed

 

any suggestions?

 

thanks

Link to comment
Share on other sites

Notice: Undefined index: myname in e:\www\site\info.php on line 12

Whatever the code is surrounding the request for $_POST['myname'] needs to be double checked.  Are you submitting a form input with that name?  Is that code within the if() statement of the submit button value?

 

PHP was telling what's wrong, but you're not listening.

Link to comment
Share on other sites

See comments in code. I'm assuming you've connected to the DB prior to this code.

 

<?php
if($_SERVER['REQUEST_METHOD'] == "POST") {
if(isset($_POST['action']) == 'update') { // this will evaluate TRUE if $_POST['action'] has any value, and the query will execute
	$name = mysql_real_escape_string($_POST['myname']);
	mysql_query("UPDATE `users` SET `name` = '$name' WHERE `id` = {$_SESSION['idnum']}"); // bad practice
	echo "1 record added"; // will display regardless of whether the update succeeded or failed.
}
}

 

Better way:

<?php
if($_SERVER['REQUEST_METHOD'] == "POST") {
if( isset($_POST['action']) && $_POST['action'] == 'update' ) {
	$name = mysql_real_escape_string($_POST['myname']);
	$qurey = "UPDATE `users` SET `name` = '$name' WHERE `id` = {$_SESSION['idnum']}";
	if( mysql_query($query) ) {
		if( mysql_affected_rows() > 0 ) {
			echo  mysql_affected_rows() . " records updated";
		} else {
			echo "No records found to update.";
		}
	} else {
		echo "Query failed to execute.";
		// you should also log the actual error for later review
                }
}
}

 

EDIT: Added missing closing brace . . . .

Link to comment
Share on other sites

Hi

 

the full code looks like;

 

<?php
session_start();
$usr= '1';
include 'connect/db.php';
require 'connect/config.php';
?>
<?php
if($_SERVER['REQUEST_METHOD'] == "POST")
{
if(isset($_POST['action']) == 'update')
{
$name = mysql_real_escape_string($_POST['myname']);
mysql_query("UPDATE `users` SET `name` = '".$name."' WHERE `id` = '".$_SESSION['idnum']."'");
echo "1 record added";
}
}
?>

 

any ideas now you can see the full code?

 

Link to comment
Share on other sites

Notice: Undefined index: myname

 

Whatever the code is surrounding the request for $_POST['myname'] needs to be double checked.  Are you submitting a form input with that name?  Is that code within the if() statement of the submit button value?

Link to comment
Share on other sites

Lol @ just turning off the errors.

 

You have been told several times what's wrong with your code.  But, since you're not going to read what anybody told you, perhaps it's best you do.

 

I promise that i've tried every single persons comment without any luck, and i've also spend a long time looking at other posts and forums, but nothing seems to work

 

if you have any other ideas, i'll be interested to try other things to try and solve it

 

thanks

Link to comment
Share on other sites

There are no other ideas that could possibly exist!  Your error undefined index: myname is coming from your PHP script asking for it right here:

$name = mysql_real_escape_string($_POST['myname']);

You either a) have not submitted the form - to wit you should really scroll up and read Pikachu's extremely detailed response or b) the form input name is not 'myname'. 

 

Fix this before coming back to say you don't know what's wrong.

Link to comment
Share on other sites

Suppressing the errors will not fix the problem. $_POST['myname'] has no value, which will cause the query to update the `name` field with an empty string. The only difference will be that now you won't have any information as to why that's happening.

Link to comment
Share on other sites

Just a quick question;

 

In the example Pikachu2000 gave, the following code was used

 

$query = "UPDATE `users` SET `nickname` = '$nickname' WHERE `id` = {$_SESSION['s_uid']}";

 

what's the advantage of using brackets {$_SESSION['s_uid']}

 

instead of using

 

$query = "UPDATE `users` SET `nickname` = '$nickname' WHERE `id` = '".$_SESSION['s_uid']."'";

 

just wondering

 

thanks

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.