Notice: Undefined index: lname

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 1985 Views - Last Post: 28 May 2009 - 05:29 PM Rate Topic: -----

#1 chesjon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 16-May 09

Notice: Undefined index: lname

Post icon  Posted 28 May 2009 - 09:31 AM

Hi, I am getting this error:

Notice: Undefined index: lname in C:\PHP\Apache\new1.php on line 2

here is the code:
<?php
 $lname = $_POST['lname'];
 
// increment buttons
echo "<form method=post action=new1.php>
<input type=submit name=runs value=R+>
<input type=submit name=hr value=HR+>
<input type=submit name=rbis value=RBIs+>
</form>";

// set connection to db
$con = mysql_connect("localhost", "root", "");

// kill execution if connection fails
if (!$con) {
   die ('Cannot connect: ' . mysql_error());
   }
   
// select db to connect to
mysql_select_db("Brewers", $con);
if(isset($_POST['runs'])) {
mysql_query("UPDATE Players SET Runs=Runs+1 WHERE LastName = '$lname'");
}
if(isset($_POST['hr'])) {
mysql_query("UPDATE Players SET HomeRuns=HomeRuns+1 WHERE LastName = '$lname'");
}
if(isset($_POST['rbis'])) {
mysql_query("UPDATE Players SET RBIs=RBIs+1 WHERE LastName = '$lname'");
}

$result = mysql_query("SELECT * FROM Players WHERE LastName = '$lname'");

// Create table for results
echo "<table border = '1'>
<th>Position</th>
<th>First Name</th>
<th>Last Name</th>
<th>Runs</th>
<th>Home Runs</th>
<th>RBI's</th>";


while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Position'] . "</td>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Runs'] . "</td>";
echo "<td>" . $row['HomeRuns'] . "</td>";
echo "<td>" . $row['RBIs'] . "</td>";
echo "</tr>";
 }
echo "</table>";

// close connection to db
mysql_close($con);

?>



The $lname = $_POST['lname']; is grabbing the value from this form:
<?php
// input last name of player to edit
echo "<form method=post action=new1.php>
Enter last name of player to update: <input type=text name=lname>
<input type=submit name=submit value=Enter>
</form>";
?>



Can anyone tell me why it is giving me this error?

Is This A Good Question/Topic? 0
  • +

Replies To: Notice: Undefined index: lname

#2 ShaneK  Icon User is offline

  • require_once("brain.php"); //Fatal error :/
  • member icon

Reputation: 240
  • View blog
  • Posts: 1,224
  • Joined: 10-May 09

Re: Notice: Undefined index: lname

Posted 28 May 2009 - 09:34 AM

Greetings

PHP likes it when you verify that things exist before setting them into variables.
All you gotta do is change this code:
$lname = $_POST['lname'];

To this code:
$lname = (isset($_POST['lname']) ? $_POST['lname'] : "");


It's basically just an if statement, it's eqiuvelent would be like:
if(isset($_POST['lname'])){ 
	  $lname = $_POST['lname'];
}else{
	  $lname = ""; //because it's not set...
}


Yours,
Shane~

This post has been edited by ShaneK: 28 May 2009 - 09:38 AM

Was This Post Helpful? 0
  • +
  • -

#3 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2983
  • View blog
  • Posts: 10,307
  • Joined: 08-August 08

Re: Notice: Undefined index: lname

Posted 28 May 2009 - 09:40 AM

It works for me.

Try deleting and retyping the line. Maybe there's some sort of hidden character in it.
Was This Post Helpful? 0
  • +
  • -

#4 ShaneK  Icon User is offline

  • require_once("brain.php"); //Fatal error :/
  • member icon

Reputation: 240
  • View blog
  • Posts: 1,224
  • Joined: 10-May 09

Re: Notice: Undefined index: lname

Posted 28 May 2009 - 09:54 AM

I'm sure the difference is the level in error reporting. His works, it just has flaws. If you do what I said then it won't report that error anymore, also you could just say
error_reporting(0);
but that's the lazy way and also it would be extremely difficult to know if you had other errors. (In the future and what not.)

Yours,
Shane~
Was This Post Helpful? 0
  • +
  • -

#5 chesjon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 16-May 09

Re: Notice: Undefined index: lname

Posted 28 May 2009 - 09:54 AM

View PostShaneK, on 28 May, 2009 - 08:34 AM, said:

Greetings

PHP likes it when you verify that things exist before setting them into variables.
All you gotta do is change this code:
$lname = $_POST['lname'];

To this code:
$lname = (isset($_POST['lname']) ? $_POST['lname'] : "");


It's basically just an if statement, it's eqiuvelent would be like:
if(isset($_POST['lname'])){ 
	  $lname = $_POST['lname'];
}else{
	  $lname = ""; //because it's not set...
}


Yours,
Shane~



Thanks! I changed the code to:

$lname = (isset($_POST['lname']) ? $_POST['lname'] : "");

and it got rid of the error but now when I click on one of the three buttons, it outputs the table headers but gets rid of the player name and does not increment it.
Was This Post Helpful? 0
  • +
  • -

#6 ShaneK  Icon User is offline

  • require_once("brain.php"); //Fatal error :/
  • member icon

Reputation: 240
  • View blog
  • Posts: 1,224
  • Joined: 10-May 09

Re: Notice: Undefined index: lname

Posted 28 May 2009 - 10:01 AM

Did it work before? (It should have ran previously even with that notice.)
If not then the error is in sending/receiving the POST data...if it did then that's extremely weird and shouldn't (and I don't see how it could possibly be) the result of that edited code. If you copied and pasted the code you might want to double check and make sure that I named the POST index correctly.

Yours,
Shane~

This post has been edited by ShaneK: 28 May 2009 - 10:02 AM

Was This Post Helpful? 0
  • +
  • -

#7 chesjon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 16-May 09

Re: Notice: Undefined index: lname

Posted 28 May 2009 - 10:09 AM

View PostShaneK, on 28 May, 2009 - 09:01 AM, said:

Did it work before? (It should have ran previously even with that notice.)
If not then the error is in sending/receiving the POST data...if it did then that's extremely weird and shouldn't (and I don't see how it could possibly be) the result of that edited code. If you copied and pasted the code you might want to double check and make sure that I named the POST index correctly.

Yours,
Shane~



Actually without that code it does not display the results either... but when i change the update lines like this

mysql_query("UPDATE Players SET Runs=Runs+1 WHERE LastName = '$lname'");

change it to an exact entry in the database

mysql_query("UPDATE Players SET Runs=Runs+1 WHERE LastName = 'Braun'");

then it works.... so it has something to do with that $lname variable and I cannot figure out how to get it to work with it.
Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2983
  • View blog
  • Posts: 10,307
  • Joined: 08-August 08

Re: Notice: Undefined index: lname

Posted 28 May 2009 - 10:10 AM

View PostShaneK, on 28 May, 2009 - 12:54 PM, said:

I'm sure the difference is the level in error reporting. His works, it just has flaws.

But I can't duplicate that error at all:
<?php
error_reporting(E_ALL|E_STRICT);
$lname = $_POST['lname'];
echo "lname: ".$lname;
?>


Was This Post Helpful? 0
  • +
  • -

#9 ShaneK  Icon User is offline

  • require_once("brain.php"); //Fatal error :/
  • member icon

Reputation: 240
  • View blog
  • Posts: 1,224
  • Joined: 10-May 09

Re: Notice: Undefined index: lname

Posted 28 May 2009 - 10:12 AM

Try this:
mysql_query("UPDATE Players SET Runs=Runs+1 WHERE LastName = '".mysql_real_escape_string($lname)."'");


And if that doesn't work try outputting the query without submitting it, just to see what it's actually trying to send to the database. Along with the query, also output the variable. (Make sure you add a seperator though.)

@CT:
Something's wrong in the submission or reception code, I'm also trying to duplicate it and can't manage to.

Yours,
Shane~

This post has been edited by ShaneK: 28 May 2009 - 10:15 AM

Was This Post Helpful? 0
  • +
  • -

#10 ShaneK  Icon User is offline

  • require_once("brain.php"); //Fatal error :/
  • member icon

Reputation: 240
  • View blog
  • Posts: 1,224
  • Joined: 10-May 09

Re: Notice: Undefined index: lname

Posted 28 May 2009 - 10:18 AM

Okay, I got the error.

Quote

Enter last name of player to update:

Notice: Undefined index: lname in C:\wamp\www\test_var.php on line 7
NULL


It only happens when I load the page up (without sending POST data, I mean).

Code used to generate it:
error_reporting(E_STRICT | E_NOTICE);
print "<form method=post action=test_var.php>
Enter last name of player to update: <input type=text name=lname>
<input type=submit name=submit value=Enter>
</form>";
$lname = $_POST['lname'];
die(var_dump($lname));


Yours,
Shane~

This post has been edited by ShaneK: 28 May 2009 - 10:26 AM

Was This Post Helpful? 0
  • +
  • -

#11 chesjon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 16-May 09

Re: Notice: Undefined index: lname

Posted 28 May 2009 - 10:21 AM

View PostShaneK, on 28 May, 2009 - 09:12 AM, said:

Try this:
mysql_query("UPDATE Players SET Runs=Runs+1 WHERE LastName = '".mysql_real_escape_string($lname)."'");


And if that doesn't work try outputting the query without submitting it, just to see what it's actually trying to send to the database. Along with the query, also output the variable. (Make sure you add a seperator though.)

@CT:
Something's wrong in the submission or reception code, I'm also trying to duplicate it and can't manage to.

Yours,
Shane~


do you mean echo the mysql_query statement? If that is what you mean, I did that and the output was

Resource id#4

I also outputted the $lname variable and it had the correct value.
Was This Post Helpful? 0
  • +
  • -

#12 ShaneK  Icon User is offline

  • require_once("brain.php"); //Fatal error :/
  • member icon

Reputation: 240
  • View blog
  • Posts: 1,224
  • Joined: 10-May 09

Re: Notice: Undefined index: lname

Posted 28 May 2009 - 10:24 AM

Try saying:
echo("UPDATE Players SET Runs=Runs+1 WHERE LastName = '".mysql_real_escape_string($lname)."'."<br>\n$lname");


Yours,
Shane~
Was This Post Helpful? 0
  • +
  • -

#13 chesjon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 16-May 09

Re: Notice: Undefined index: lname

Posted 28 May 2009 - 10:30 AM

View PostShaneK, on 28 May, 2009 - 09:24 AM, said:

Try saying:
echo("UPDATE Players SET Runs=Runs+1 WHERE LastName = '".mysql_real_escape_string($lname)."'."<br>\n$lname");


Yours,
Shane~


That code gives me a parse error
Was This Post Helpful? 0
  • +
  • -

#14 ShaneK  Icon User is offline

  • require_once("brain.php"); //Fatal error :/
  • member icon

Reputation: 240
  • View blog
  • Posts: 1,224
  • Joined: 10-May 09

Re: Notice: Undefined index: lname

Posted 28 May 2009 - 10:32 AM

My bad, wasn't working with syntax highlighting.
Try this:
echo("UPDATE Players SET Runs=Runs+1 WHERE LastName = '".mysql_real_escape_string($lname)."'<br>\n$lname");

Place it right below the actual query, so you could see pretty much what the query sees when it's trying to execute....

Yours,
Shane~

This post has been edited by ShaneK: 28 May 2009 - 10:34 AM

Was This Post Helpful? 0
  • +
  • -

#15 chesjon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 16-May 09

Re: Notice: Undefined index: lname

Posted 28 May 2009 - 10:37 AM

View PostShaneK, on 28 May, 2009 - 09:32 AM, said:

My bad, wasn't working with syntax highlighting.
Try this:
echo("UPDATE Players SET Runs=Runs+1 WHERE LastName = '".mysql_real_escape_string($lname)."'<br>\n$lname");

Place it right below the actual query, so you could see pretty much what the query sees when it's trying to execute....

Yours,
Shane~



Here is the output:

UPDATE Players SET Runs=Runs+1 WHERE LastName = 'braun'
braun

View Postchesjon, on 28 May, 2009 - 09:35 AM, said:

View PostShaneK, on 28 May, 2009 - 09:32 AM, said:

My bad, wasn't working with syntax highlighting.
Try this:
echo("UPDATE Players SET Runs=Runs+1 WHERE LastName = '".mysql_real_escape_string($lname)."'<br>\n$lname");

Place it right below the actual query, so you could see pretty much what the query sees when it's trying to execute....

Yours,
Shane~



Here is the output:

UPDATE Players SET Runs=Runs+1 WHERE LastName = 'braun'
braun


BUT when I click on any of the three buttons... the value in $lname disappears
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2