May 1, 201214 yr I am receiving a Undefined index ERROR, but am unsure why. Any reasons why? <?php $msg = ""; include ("connect_to_mysql.php"); if ($_POST['title'] != "") { $title = $_POST['title']; $score = $_POST['score']; $next_game = $_POST['next_game']; $words = $_POST['words']; $words = stripslashes($words); $title2 = $_POST['title2']; $words2 = $_POST['words2']; $words2 = stripslashes($words2); $query = mysql_query("INSERT INTO hometext (title, score, next_game, words, title2, words2) VALUES ('$title','$score','$next_game','$words','$title2','$words2')") or die (mysql_error()); $msg = "entered"; } ?> <html> <head> <title>Add</title> <script type="text/javascript" src="tinymce/jscripts/tiny_mce/tiny_mce.js"></script> <script type="text/javascript"> tinyMCE.init({ // General options mode : "textareas", theme : "advanced", elements : "content", theme_advanced_font_sizes : "Big text=30px,Small text=small,My Text Size=.mytextsize" }); </script> </head> <body> <div> <p>Add post to database</p> <form action="add.php" method="POST"> Title:<br /> <input type="text" name="title" /><br /><br /> Score:<br /> <input type="text" name="score" /><br /><br /> Next Game:<br /> <input type="text" name="next_game" /><br /><br /> Article:<br /> <textarea name="words" class="tinymce" cols="50" rows="10"></textarea> <br /> Title 2:<br /> <input type="text" name="title2" /><br /><br /> Article 2:<br /> <textarea name="words2" class="tinymce" cols="50" rows="10"></textarea> <br /><br /> <input type="submit" name="submit" value="Submit" /> <?php echo $msg; ?> </form> </div> </body> </html>
May 1, 201214 yr Write ur varibles like this to avoid index errors $name = (isset($_POST['name'])) ? $_POST['name'] : false; or you can write a simple function to handle input values, heres my function to do just that function input($type, $value, $forDb = false) { if ($type && $value) { switch ($type) { case 'post' : if (isset ($_POST[$value])) { if ($forDb === true) { if (get_magic_quotes_gpc() == 1) { return @ mysqli_real_escape_string(connect(), stripslashes($_POST[$value])); } else { return @ mysqli_real_escape_string(connect(), $_POST[$value]); } } else { return $_POST[$value]; } } break; case 'get' : if (isset ($_GET[$value])) { if ($forDb === true) { if (get_magic_quotes_gpc() == 1) { return @ mysqli_real_escape_string(connect(), stripslashes($_GET[$value])); } else { return @ mysqli_real_escape_string(connect(), $_GET[$value]); } } else { return $_GET[$value]; } } break; } } }
May 1, 201214 yr The first time you run this script, the form won't have been submitted so the $_POST indices won't be recognised. You will only be able to check if they are set with isset. One way round this would be to use a hidden field in your form which you can check for. Replace if ($_POST['title'] != "") { with if (isset($_POST['submitted'])) { and add <input type="hidden" name="submitted" value="TRUE" /> to the end of your form.
May 1, 201214 yr The first time you run this script, the form won't have been submitted so the $_POST indices won't be recognised. You will only be able to check if they are set with isset. One way round this would be to use a hidden field in your form which you can check for. Replace if ($_POST['title'] != "") { with if (isset($_POST['submitted'])) { and add <input type="hidden" name="submitted" value="TRUE" /> to the end of your form. actually an extra hidden field is not needed all he has to do is check for the submit button being clicked like this if (isset($_POST['submit'])) { //Your code }
May 3, 201214 yr He means you're repeating your code. DRY = 'dont repeat yourself' There are more effecient ways of writing that.
May 4, 201214 yr He means you're repeating your code. DRY = 'dont repeat yourself' There are more effecient ways of writing that. Yes i know what he means lol Edited May 4, 201214 yr by webdesigner93
Create an account or sign in to comment