loudog Posted May 18, 2011 Share Posted May 18, 2011 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(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/236759-why-this-error/ Share on other sites More sharing options...
anupamsaha Posted May 18, 2011 Share Posted May 18, 2011 Do you have a column name 'id' in the table 'test_take_two'? Quote Link to comment https://forums.phpfreaks.com/topic/236759-why-this-error/#findComment-1217055 Share on other sites More sharing options...
JonnoTheDev Posted May 18, 2011 Share Posted May 18, 2011 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; Quote Link to comment https://forums.phpfreaks.com/topic/236759-why-this-error/#findComment-1217056 Share on other sites More sharing options...
markjoe Posted May 18, 2011 Share Posted May 18, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/236759-why-this-error/#findComment-1217057 Share on other sites More sharing options...
Maq Posted May 18, 2011 Share Posted May 18, 2011 @loudog, in the future, please place tags around your code. Quote Link to comment https://forums.phpfreaks.com/topic/236759-why-this-error/#findComment-1217058 Share on other sites More sharing options...
PFMaBiSmAd Posted May 18, 2011 Share Posted May 18, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/236759-why-this-error/#findComment-1217063 Share on other sites More sharing options...
fugix Posted May 18, 2011 Share Posted May 18, 2011 the only time that you want error_reporting of any kind hidden is when the server is live... otherwise you always want error reporting shown for debugging Quote Link to comment https://forums.phpfreaks.com/topic/236759-why-this-error/#findComment-1217084 Share on other sites More sharing options...
JonnoTheDev Posted May 18, 2011 Share Posted May 18, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/236759-why-this-error/#findComment-1217261 Share on other sites More sharing options...
fugix Posted May 18, 2011 Share Posted May 18, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/236759-why-this-error/#findComment-1217281 Share on other sites More sharing options...
markjoe Posted May 19, 2011 Share Posted May 19, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/236759-why-this-error/#findComment-1217374 Share on other sites More sharing options...
Maq Posted May 19, 2011 Share Posted May 19, 2011 In general, yes. I still believe there are cases where ignoring notices is perfectly safe to do. IMO, notices/warnings = side effects = bad. Quote Link to comment https://forums.phpfreaks.com/topic/236759-why-this-error/#findComment-1217571 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.