Page 1 of 1

error

Posted: Mon Jun 19, 2006 1:21 pm
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]

Posted: Mon Jun 19, 2006 1:35 pm
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.

Posted: Tue Jun 20, 2006 4:55 am
by bluelad
Sorry. New to your rules.
At which line am i supposed to insert it

Posted: Tue Jun 20, 2006 5:10 am
by technofreak
Bluelad,

Use

Code: Select all

instead of

Code: Select all

, thats all...

Posted: Tue Jun 20, 2006 8:52 pm
by bluelad
I'll remember that. But i havent got my answer?

Posted: Tue Jun 20, 2006 8:58 pm
by Robert Plank
Change

Code: Select all

if ($_POST['submit'])
To

Code: Select all

if (array_key_exists('submit', $_POST) && $_POST['submit'])

Posted: Tue Jun 20, 2006 11:04 pm
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.

Posted: Tue Jun 20, 2006 11:35 pm
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.

Posted: Tue Jun 20, 2006 11:49 pm
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.

Posted: Tue Jun 20, 2006 11:54 pm
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)

Posted: Wed Jun 21, 2006 12:01 am
by RobertGonzalez
Thanks Ninja. :wink:

Posted: Wed Jun 21, 2006 1:56 am
by bluelad
Thanks a lot guys. I am basiclly a novice and learning myself. So u may pardon my "mistakes".
Thakns a lot again