supanoob Posted March 9, 2009 Share Posted March 9, 2009 I keep getting the following errors with the following bit of code, but i cant find out what the problem is :S Notice: Undefined index: myForm in C:\wamp\www\post_ratings.php on line 13 Notice: Undefined index: rate in C:\wamp\www\post_ratings.php on line 17 Notice: Undefined index: rate in C:\wamp\www\post_ratings.php on line 25 0 <?php $dbuser = "zixel"; $dbpass = "meh"; $dbloc = "localhost"; //db location $dbname = "hub_beta"; $db_conn = mysql_connect($dbloc,$dbuser,$dbpass); // test for conn $db = mysql_select_db( $dbname ); // test for db // fetch POST params and sanitize $rate = 1; $post_rate = $_POST['myForm']; echo "$post_rate"; if ($_POST['rate'] == '+1') { $sql2="UPDATE post_ratings SET rating_up=rating_up+1 where post_id=$rate"; if(mysql_query($sql2)); echo "Post Rated Up."; } if ($_POST['rate'] == '-1') { $sql2="UPDATE post_ratings SET rating_down=rating_down+1 where post_id=$rate"; if(mysql_query($sql2)); echo "Post Rated Down."; } // test for result // do other stuff $query="select post_id, rating_up, rating_down from post_ratings where post_id = '$rate'"; $result=mysql_query($query); if (!$result) { die(mysql_error()); } $num_rows=mysql_num_rows($result); $row=mysql_fetch_array($result); $rating_up=($row['rating_up']); $rating_down=($row['rating_down']); $rating = ($rating_up-$rating_down); echo "$rating"; ?> Link to comment https://forums.phpfreaks.com/topic/148576-undefined-index/ Share on other sites More sharing options...
DjMikeS Posted March 9, 2009 Share Posted March 9, 2009 It really think that it's not wise to put your mysql password in your post... Second, do some debugging. See if it can select the database.... if (!$db) { die ('Can\'t use database : ' . mysql_error()); } Link to comment https://forums.phpfreaks.com/topic/148576-undefined-index/#findComment-780198 Share on other sites More sharing options...
supanoob Posted March 9, 2009 Author Share Posted March 9, 2009 It really think that it's not wise to put your mysql password in your post... Second, do some debugging. See if it can select the database.... if (!$db) { die ('Can\'t use database : ' . mysql_error()); } yeah good call, but its a wamp5 server. ill check that now thanks. That didnt show any errors :S i do have another bit of code that links to this one, but it has AJAX in it, this is below: <html> <head> <!-- easy ajax - download from http://www.prototypejs.org/--> <script src="../scripts/prototype.js" type="text/javascript"></script> <script type="text/javascript"> function liveStuff() { var ajax = new Ajax.Updater( { success:'results', }, '../post_ratings.php', { parameters: $('myForm').serialize(true) } ); } </script> </head> <body> <form id="myForm" method="post" action="javascript:liveStuff();" onsubmit="liveStuff();"> <select> <option value="+1">+1</option> <option value="-1">-1</option> </select> <INPUT type="submit" value="Rate"> </form> <div id=results></div> </body> </html> Link to comment https://forums.phpfreaks.com/topic/148576-undefined-index/#findComment-780200 Share on other sites More sharing options...
DjMikeS Posted March 9, 2009 Share Posted March 9, 2009 Undefined index refers to an array. You are trying to get a key from an array but php is unable to find it and therefore gives you an undefined index warning. Do you use uppercase characters in your mysql field names? If so, you should also use them in your array... To see what the array contains, you could do: print_r ($arrayname); Link to comment https://forums.phpfreaks.com/topic/148576-undefined-index/#findComment-780201 Share on other sites More sharing options...
supanoob Posted March 9, 2009 Author Share Posted March 9, 2009 Undefined index refers to an array. You are trying to get a key from an array but php is unable to find it and therefore gives you an undefined index warning. Do you use uppercase characters in your mysql field names? If so, you should also use them in your array... To see what the array contains, you could do: print_r ($arrayname); i have checked all them :S and that doesnt do anything Link to comment https://forums.phpfreaks.com/topic/148576-undefined-index/#findComment-780203 Share on other sites More sharing options...
kickstart Posted March 9, 2009 Share Posted March 9, 2009 Hi It is complaining that $_POST['myForm'] does not exist. Normal setup it would not complain about this but you have the error reporting level set quite high. All the best Keith Link to comment https://forums.phpfreaks.com/topic/148576-undefined-index/#findComment-780204 Share on other sites More sharing options...
supanoob Posted March 9, 2009 Author Share Posted March 9, 2009 Hi It is complaining that $_POST['myForm'] does not exist. Normal setup it would not complain about this but you have the error reporting level set quite high. All the best Keith so how can i change this? Link to comment https://forums.phpfreaks.com/topic/148576-undefined-index/#findComment-780212 Share on other sites More sharing options...
DjMikeS Posted March 9, 2009 Share Posted March 9, 2009 @kickstart....true...how did I miss that one... @supanoob... You haven't specified a name for your form. You should do so in your HTML code... <html> <head> <!-- easy ajax - download from http://www.prototypejs.org/--> <script src="../scripts/prototype.js" type="text/javascript"></script> <script type="text/javascript"> function liveStuff() { var ajax = new Ajax.Updater( { success:'results', }, '../post_ratings.php', { parameters: $('myForm').serialize(true) } ); } </script> </head> <body> <form id="myForm" name="myForm" method="post" action="javascript:liveStuff();" onsubmit="liveStuff();"> <select> <option value="+1">+1</option> <option value="-1">-1</option> </select> <INPUT type="submit" value="Rate"> </form> <div id=results></div> </body> </html> Link to comment https://forums.phpfreaks.com/topic/148576-undefined-index/#findComment-780216 Share on other sites More sharing options...
kickstart Posted March 9, 2009 Share Posted March 9, 2009 Hi If you want to change it then use error_reporting(E_ALL | E_NOTICE);. Or if you want to keep the error reporting level very strict then check that the array members (ie, $_POST fields) exist before trying to use their values. All the best Keith Link to comment https://forums.phpfreaks.com/topic/148576-undefined-index/#findComment-780218 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.