error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bluelad
Forum Commoner
Posts: 34
Joined: Mon Jun 05, 2006 8:34 am

error

Post by bluelad »

Pimptastic | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I am a having a bad problem..

Code: Select all

<html>
<head><title>Cool links</title>
</head>
<body bgcolor="#0A5C8E" link="#EBEBEB" vlink="#EBEBEB" alink="#FFB340">
<?php
$link=mysql_connect("localhost", "root", "");
$db=mysql_select_db("chetan_db", $link);

if ($_POST['submit'])
{
$link_id=$_POST['id'];
$link_name=$_POST['name'];
$link_desc=$_POST['desc'];
$sql="insert into link values ('', '" . $link_name . "', '" . $link_desc ."')";

$query=mysql_query($sql);
}
?>

<form action="insert.php" method="post">
<table width="100%" align="center" border="1" cellspacing="0" cellpadding="0" style="border-collapse:collapse" bordercolor="#808080">
<tr>
<td width="10%" align="center"><font color="#FFB340" size="2.5">URL</font></td>
<td  width="90%"><input type="text" name="name" value="" size="25"></td>
</tr>
<td width="10%" align="center"><font color="#FFB340" size="2.5">Description</font></td>
<td width="90%"><input type="description" name="desc" value="" size="65"></td>
</tr>
</table>
<br>
<div align="center"><input type="submit" name="submit" value="Submit"></div>
</form>
<br>
<div align="center"><a href="index.php"><font color="#FFB340" size="2.5">Back to Link</font></a> | <a href="http://127.0.0.1"><font color="#FFB340" size="2.5">Back to Forum</a>
</div> 

</body>
</html>
Whenever i run this script it shows the following error

Code: Select all

Notice: Undefined index: id in d:\program files\easyphp1-8\www\link\insert.php on line 11
Can someone fix this error.


Pimptastic | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

Post by bdlang »

The E_NOTICE error is normally harmless, but indicates that you've not explicitly initiated or assigned a variable prior to testing or attempting to get output. In this case, it appears that $_POST['id'] doesn't exist in your $_POST array. Check your form, and at the top of your script, add this line of code:

Code: Select all

echo '<pre>'; print_r($_POST); echo '</pre>';

Please use the PHP tags when posting PHP code, makes it alot easier to read and troubleshoot. Indicating the actual line of code (in this case line 11) is also helpful. We don't necessarily want to spend our time counting lines.
bluelad
Forum Commoner
Posts: 34
Joined: Mon Jun 05, 2006 8:34 am

Post by bluelad »

Sorry. New to your rules.
At which line am i supposed to insert it
User avatar
technofreak
Forum Commoner
Posts: 74
Joined: Thu Jun 01, 2006 12:30 am
Location: Chennai, India
Contact:

Post by technofreak »

Bluelad,

Use

Code: Select all

instead of

Code: Select all

, thats all...
bluelad
Forum Commoner
Posts: 34
Joined: Mon Jun 05, 2006 8:34 am

Post by bluelad »

I'll remember that. But i havent got my answer?
Robert Plank
Forum Contributor
Posts: 110
Joined: Sun Dec 26, 2004 9:04 pm
Contact:

Post by Robert Plank »

Change

Code: Select all

if ($_POST['submit'])
To

Code: Select all

if (array_key_exists('submit', $_POST) && $_POST['submit'])
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

Post by bdlang »

bluelad wrote:I'll remember that. But i havent got my answer?
Everything you need to check is spelled out in my original post. Your problem is obvious if you look at the contents of $_POST and look over your form.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

It is giving you that notice, because you are trying to use a variable that has not been initialized

I usually turn off notices because I don't want to have to initialize a variable before using it... this is how you do that. make this your first line.

Code: Select all

error_reporting(E_ALL ^ E_NOTICE);
Either that or contact your host and tell them to suppress php notices. Or... just check that the variable is initialized before using it like these guys said.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Couple things right off the bat...

1) Please use a more descriptive title. 'error' tells no one anything.
2) Your problem is not a 'really bad' problem. It is a notice telling you that you did not check for something before assigning the something.
3) As has already been mentioned, please use the appropriate tags when posting.

Now to your problem...

The undefined index is thrown when you try to use an array index that has no value yet. When you assign $link_id = $_POST['id'] there is a strong chance that the id field is either not set, not appropriately labeled or incorrectly referenced. What this means in simple English is that you need to make sure that you have a field in your form called 'id' (notice the case), make sure it is spelled correctly and make sure it has a value. Then, before you assign it, check for isset() and !empty().

DO NOT reduce your error reporting level just to shut PHP up. All that does is place you in the dark to bad code. Fix the code error, don't ignore it.

Code: Select all

<?php
$link_id = 0;
$link_name = '';
$link_desc = '';

if (isset($_POST['submit']))
{
    if ( isset($_POST['id']) && !empty($_POST['id']) )
    {
        $link_id = $_POST['id'];
    }

    if ( isset($_POST['name']) && !empty($_POST['name']) )
    {
        $link_name = $_POST['name'];
    }

    if ( isset($_POST['desc']) && !empty($_POST['desc']) )
    {
        $link_desc = $_POST['desc'];
    }

    $sql = "insert into link values ('', '$link_name', '$link_desc')";

    $query = mysql_query($sql) or die("Could not query the table: " . mysql_error());
}
?>
There are quite a few other techniques you need to use to validate your input, and there are easier and faster ways to run the checks and assignments. But this should wipe out your notices.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

Everah wrote:Couple things right off the bat...

1) Please use a more descriptive title. 'error' tells no one anything.
2) Your problem is not a 'really bad' problem. It is a notice telling you that you did not check for something before assigning the something.
3) As has already been mentioned, please use the appropriate tags when posting.

Now to your problem...

The undefined index is thrown when you try to use an array index that has no value yet. When you assign $link_id = $_POST['id'] there is a strong chance that the id field is either not set, not appropriately labeled or incorrectly referenced. What this means in simple English is that you need to make sure that you have a field in your form called 'id' (notice the case), make sure it is spelled correctly and make sure it has a value. Then, before you assign it, check for isset() and !empty().

DO NOT reduce your error reporting level just to shut PHP up. All that does is place you in the dark to bad code. Fix the code error, don't ignore it.

Code: Select all

<?php
$link_id = 0;
$link_name = '';
$link_desc = '';

if (isset($_POST['submit']))
{
    if ( isset($_POST['id']) && !empty($_POST['id']) )
    {
        $link_id = $_POST['id'];
    }

    if ( isset($_POST['name']) && !empty($_POST['name']) )
    {
        $link_name = $_POST['name'];
    }

    if ( isset($_POST['desc']) && !empty($_POST['desc']) )
    {
        $link_desc = $_POST['desc'];
    }

    $sql = "insert into link values ('', '$link_name', '$link_desc')";

    $query = mysql_query($sql) or die("Could not query the table: " . mysql_error());
}
?>
There are quite a few other techniques you need to use to validate your input, and there are easier and faster ways to run the checks and assignments. But this should wipe out your notices.
Yea I guess I shouldn't really promote bad coding practices. Everah is right... it is always best to do things the right way rather than the easy way (which can often lead to bigger problems)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Thanks Ninja. :wink:
bluelad
Forum Commoner
Posts: 34
Joined: Mon Jun 05, 2006 8:34 am

Post by bluelad »

Thanks a lot guys. I am basiclly a novice and learning myself. So u may pardon my "mistakes".
Thakns a lot again
Post Reply