Jump to content

why this error??


loudog

Recommended Posts

why d0o i get this "Notice: Undefined index: id in C:\wamp\www\GuestBook\delete_ac.php on line 8"

 

(page1)

title delete.php

 

<?php
	mysql_connect("localhost", "root", "") or die ("connot conect to server");
mysql_select_db("test_take_two") or die ("connot select db");

$sql="SELECT * FROM test_mysql";
$result= mysql_query($sql);
?>

<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><table width="400%" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF"> </td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Delete data in mysql</strong></td>
</tr>
<tr>
<td align ="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
<td align ="center" bgcolor="#FFFFFF"><strong>Name</strong></td>
<td align ="center" bgcolor="#FFFFFF"><strong>Lastname</strong></td>
<td align ="center" bgcolor="#FFFFFF"><strong>Email</strong></td>
<td slign="center" bgcolor="#FFFFFF"> </td>
</tr>

<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td bgcolor="#FFFFFF"><?php echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['name']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['lastname'];?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['email']; ?></td>
<td bgcolor="#FFFFFF"><a href="delete_ac.php?id=<?php echo $rows['id']; ?>">delete</a>
</td>
</tr>
<?php
}

mysql_close();
?>
</table></td>
</tr>
</table>

(page2)

title delete_ac.php

 

<?php

mysql_connect("localhost", "root", "") or die ("connot conect to server");
mysql_select_db("test_take_two") or die("connot select to  database");

//get value of id that sent from address bar

[i][b]this is line 8->[/b][/i] $id=$_GET['id'];

//Delete data in myssql from row that has this id

$sql = "DELETE FROM test_mysql WHERE id='$id'";
$result=mysql_query($sql);

// If successfully deleted

if($result){
echo "Deleted Successfully";
echo "<br>";
echo "<a href='delete.php'>Back to main page</a>";
}
else{
echo "ERROR";
}

// CLOSE CONNECTION
mysql_close();

?>

Link to comment
Share on other sites

Because $_GET['id'] does not exist. You should check for it such as

 

<?php

mysql_connect("localhost", "root", "") or die ("connot conect to server");
mysql_select_db("test_take_two") or die("connot select to  database");

//get value of id that sent from address bar

if(!isset($_GET['id'])) {
echo "ERROR: no id";
exit();
}
$id=$_GET['id'];


//Delete data in myssql from row that has this id

$sql = "DELETE FROM test_mysql WHERE id='$id'";
$result=mysql_query($sql);

// If successfully deleted

if($result){
echo "Deleted Successfully";
echo "<br>";
echo "<a href='delete.php'>Back to main page</a>";
}
else{
echo "ERROR";
}

// CLOSE CONNECTION
mysql_close();

?>

 

Becase of the level of error reporting set in your PHP configuration, you will get that error whenever a variable hasn't been defined prior to it's use i.e

 

/*
sample1.php
the following will produce a warning error
*/
echo "My name is: ".$name;


/*
sample2.php
the following will NOT produce a warning error
*/
$name = "Neil";
echo "My name is: ".$name;

Link to comment
Share on other sites

It's not an error, it's a Notice. It's telling you exactly what it says, that you are referring to an array index before setting it.

In nearly all cases you can safely ignore this.

You can change your error_reporting setting to not show Notices, either at runtime with error_reporting() function or in the php.ini file.

Link to comment
Share on other sites

You can change your error_reporting setting to not show Notices, either at runtime with error_reporting() function or in the php.ini file.

 

Please don't suggest doing that. Doing so will also hide information about real problems, such as a legitimate visitor doing something that your code didn't take into account or a hacker trying to break into your script. Error_reporting should always be set to at least E_ALL.

 

Code should not generate any type of errors, warnings, or notices during its normal execution. Only for unexpected things and a $_GET index not being set is not an unexpected thing.

Link to comment
Share on other sites

the only time that you want error_reporting of any kind hidden is when the server is live

I think you mean when the website is live, not the server. If you are on a shared host it is usually not possible to modify the php.ini file. If you have various websites on your shared host you can control error reporting and the output of errors/warnings to logs and or the screen via a .htaccess file for each individual site. For a live site you should have the display_errors flag to off. A development site on the same server you should have it set to on.

 

On a dedicated server I would set the main php.ini file to not output errors or warnings and as with the shared host control the error output via .htaccess for each website on the server. 

Link to comment
Share on other sites

the only time that you want error_reporting of any kind hidden is when the server is live

I think you mean when the website is live, not the server. If you are on a shared host it is usually not possible to modify the php.ini file. If you have various websites on your shared host you can control error reporting and the output of errors/warnings to logs and or the screen via a .htaccess file for each individual site. For a live site you should have the display_errors flag to off. A development site on the same server you should have it set to on.

 

On a dedicated server I would set the main php.ini file to not output errors or warnings and as with the shared host control the error output via .htaccess for each website on the server.

yeah website sorry

Link to comment
Share on other sites

Code should not generate any type of errors, warnings, or notices during its normal execution.

In general, yes. I still believe there are cases where ignoring notices is perfectly safe to do.

For a production web app, you'd actually want to log everything to file.

Maybe I was being a little prejudice, but the code posted didn't appear to be part of a live production app of significant size of sophistication. In which case I wouldn't expect it to be perfect code, or free of all warnings and notices.

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.