dweb Posted June 23, 2012 Share Posted June 23, 2012 Hi I'm really confused by an error I keep getting, my code is; <?php if($_SERVER['REQUEST_METHOD'] == "POST") { if(isset($_POST['action']) == 'update') { $name = mysql_real_escape_string($_POST['myname']); mysql_query("UPDATE `users` SET `name` = '".$name."' WHERE `id` = '".$_SESSION['idnum']."'"); echo "1 record added"; } } ?> it all works ok and records are updated, but when the form is submitted I keep getting the error Notice: Undefined index: myname in e:\www\site\info.php on line 12 I've looked around online but all the solutions i've tried have failed any suggestions? thanks Link to comment https://forums.phpfreaks.com/topic/264645-undefined-index-error/ Share on other sites More sharing options...
Pikachu2000 Posted June 23, 2012 Share Posted June 23, 2012 The error shows to be on line 12, but there aren't 12 lines of code. Is that the right file, and is there more code? isset($_POST['action']) will never == 'update', BTW. Link to comment https://forums.phpfreaks.com/topic/264645-undefined-index-error/#findComment-1356323 Share on other sites More sharing options...
Mahngiel Posted June 23, 2012 Share Posted June 23, 2012 Notice: Undefined index: myname in e:\www\site\info.php on line 12 Whatever the code is surrounding the request for $_POST['myname'] needs to be double checked. Are you submitting a form input with that name? Is that code within the if() statement of the submit button value? PHP was telling what's wrong, but you're not listening. Link to comment https://forums.phpfreaks.com/topic/264645-undefined-index-error/#findComment-1356330 Share on other sites More sharing options...
Ricky. Posted June 23, 2012 Share Posted June 23, 2012 Simply var_dump($_POST) and see whether you are actually receiving data for 'myname' or not. Also, before using $_POST['myname'] , you need to check if it really exists or not . Link to comment https://forums.phpfreaks.com/topic/264645-undefined-index-error/#findComment-1356332 Share on other sites More sharing options...
Pikachu2000 Posted June 23, 2012 Share Posted June 23, 2012 See comments in code. I'm assuming you've connected to the DB prior to this code. <?php if($_SERVER['REQUEST_METHOD'] == "POST") { if(isset($_POST['action']) == 'update') { // this will evaluate TRUE if $_POST['action'] has any value, and the query will execute $name = mysql_real_escape_string($_POST['myname']); mysql_query("UPDATE `users` SET `name` = '$name' WHERE `id` = {$_SESSION['idnum']}"); // bad practice echo "1 record added"; // will display regardless of whether the update succeeded or failed. } } Better way: <?php if($_SERVER['REQUEST_METHOD'] == "POST") { if( isset($_POST['action']) && $_POST['action'] == 'update' ) { $name = mysql_real_escape_string($_POST['myname']); $qurey = "UPDATE `users` SET `name` = '$name' WHERE `id` = {$_SESSION['idnum']}"; if( mysql_query($query) ) { if( mysql_affected_rows() > 0 ) { echo mysql_affected_rows() . " records updated"; } else { echo "No records found to update."; } } else { echo "Query failed to execute."; // you should also log the actual error for later review } } } EDIT: Added missing closing brace . . . . Link to comment https://forums.phpfreaks.com/topic/264645-undefined-index-error/#findComment-1356373 Share on other sites More sharing options...
dweb Posted June 23, 2012 Author Share Posted June 23, 2012 Hi the full code looks like; <?php session_start(); $usr= '1'; include 'connect/db.php'; require 'connect/config.php'; ?> <?php if($_SERVER['REQUEST_METHOD'] == "POST") { if(isset($_POST['action']) == 'update') { $name = mysql_real_escape_string($_POST['myname']); mysql_query("UPDATE `users` SET `name` = '".$name."' WHERE `id` = '".$_SESSION['idnum']."'"); echo "1 record added"; } } ?> any ideas now you can see the full code? Link to comment https://forums.phpfreaks.com/topic/264645-undefined-index-error/#findComment-1356375 Share on other sites More sharing options...
Pikachu2000 Posted June 23, 2012 Share Posted June 23, 2012 Did you even read my previous post and try that code? Link to comment https://forums.phpfreaks.com/topic/264645-undefined-index-error/#findComment-1356376 Share on other sites More sharing options...
dweb Posted June 23, 2012 Author Share Posted June 23, 2012 Did you even read my previous post and try that code? yep i did try your example but get Notice: Undefined index: myname and Notice: Undefined variable: query Link to comment https://forums.phpfreaks.com/topic/264645-undefined-index-error/#findComment-1356377 Share on other sites More sharing options...
Pikachu2000 Posted June 23, 2012 Share Posted June 23, 2012 The index 'nickname' doesn't exist in the code I posted, so I have no clue why you're getting that. The second one, you'll see I misspelled $query as $qurey, so change that. Link to comment https://forums.phpfreaks.com/topic/264645-undefined-index-error/#findComment-1356379 Share on other sites More sharing options...
dweb Posted June 23, 2012 Author Share Posted June 23, 2012 yes sorry I copied the error from another page by mistake, then corrected my post with the correct error Link to comment https://forums.phpfreaks.com/topic/264645-undefined-index-error/#findComment-1356386 Share on other sites More sharing options...
Mahngiel Posted June 23, 2012 Share Posted June 23, 2012 Notice: Undefined index: myname Whatever the code is surrounding the request for $_POST['myname'] needs to be double checked. Are you submitting a form input with that name? Is that code within the if() statement of the submit button value? Link to comment https://forums.phpfreaks.com/topic/264645-undefined-index-error/#findComment-1356387 Share on other sites More sharing options...
dweb Posted June 23, 2012 Author Share Posted June 23, 2012 Hi everyone I've read and tried all your ideas and none work, it seems impossible to fix My only option is to turn off errors Thanks for all your help anyway Link to comment https://forums.phpfreaks.com/topic/264645-undefined-index-error/#findComment-1356392 Share on other sites More sharing options...
Mahngiel Posted June 23, 2012 Share Posted June 23, 2012 Lol @ just turning off the errors. You have been told several times what's wrong with your code. But, since you're not going to read what anybody told you, perhaps it's best you do. Link to comment https://forums.phpfreaks.com/topic/264645-undefined-index-error/#findComment-1356395 Share on other sites More sharing options...
dweb Posted June 23, 2012 Author Share Posted June 23, 2012 Lol @ just turning off the errors. You have been told several times what's wrong with your code. But, since you're not going to read what anybody told you, perhaps it's best you do. I promise that i've tried every single persons comment without any luck, and i've also spend a long time looking at other posts and forums, but nothing seems to work if you have any other ideas, i'll be interested to try other things to try and solve it thanks Link to comment https://forums.phpfreaks.com/topic/264645-undefined-index-error/#findComment-1356404 Share on other sites More sharing options...
Mahngiel Posted June 23, 2012 Share Posted June 23, 2012 There are no other ideas that could possibly exist! Your error undefined index: myname is coming from your PHP script asking for it right here: $name = mysql_real_escape_string($_POST['myname']); You either a) have not submitted the form - to wit you should really scroll up and read Pikachu's extremely detailed response or b) the form input name is not 'myname'. Fix this before coming back to say you don't know what's wrong. Link to comment https://forums.phpfreaks.com/topic/264645-undefined-index-error/#findComment-1356413 Share on other sites More sharing options...
Pikachu2000 Posted June 23, 2012 Share Posted June 23, 2012 Suppressing the errors will not fix the problem. $_POST['myname'] has no value, which will cause the query to update the `name` field with an empty string. The only difference will be that now you won't have any information as to why that's happening. Link to comment https://forums.phpfreaks.com/topic/264645-undefined-index-error/#findComment-1356417 Share on other sites More sharing options...
dweb Posted June 23, 2012 Author Share Posted June 23, 2012 finally managed to get it to work, re-wrote whole page and works good now thanks for everyones help Link to comment https://forums.phpfreaks.com/topic/264645-undefined-index-error/#findComment-1356478 Share on other sites More sharing options...
dweb Posted June 23, 2012 Author Share Posted June 23, 2012 Just a quick question; In the example Pikachu2000 gave, the following code was used $query = "UPDATE `users` SET `nickname` = '$nickname' WHERE `id` = {$_SESSION['s_uid']}"; what's the advantage of using brackets {$_SESSION['s_uid']} instead of using $query = "UPDATE `users` SET `nickname` = '$nickname' WHERE `id` = '".$_SESSION['s_uid']."'"; just wondering thanks Link to comment https://forums.phpfreaks.com/topic/264645-undefined-index-error/#findComment-1356480 Share on other sites More sharing options...
Pikachu2000 Posted June 23, 2012 Share Posted June 23, 2012 I find it leaves less chance for errors than doing all that concatenation and open and close quoting. Link to comment https://forums.phpfreaks.com/topic/264645-undefined-index-error/#findComment-1356483 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.