jiameow Posted March 15, 2013 Share Posted March 15, 2013 I was wondering how can you solve this type of error "undefined index? please answer Quote Link to comment https://forums.phpfreaks.com/topic/275705-php-error-undefined-index/ Share on other sites More sharing options...
jazzman1 Posted March 15, 2013 Share Posted March 15, 2013 When you defined it Quote Link to comment https://forums.phpfreaks.com/topic/275705-php-error-undefined-index/#findComment-1418853 Share on other sites More sharing options...
jiameow Posted March 15, 2013 Author Share Posted March 15, 2013 how do you do that? I can't understand it :x Quote Link to comment https://forums.phpfreaks.com/topic/275705-php-error-undefined-index/#findComment-1418855 Share on other sites More sharing options...
requinix Posted March 15, 2013 Share Posted March 15, 2013 Here's a how-to for using a great resource to solve this kind of problem. Quote Link to comment https://forums.phpfreaks.com/topic/275705-php-error-undefined-index/#findComment-1418856 Share on other sites More sharing options...
jiameow Posted March 15, 2013 Author Share Posted March 15, 2013 we can't seem to make it work Quote Link to comment https://forums.phpfreaks.com/topic/275705-php-error-undefined-index/#findComment-1418858 Share on other sites More sharing options...
Maq Posted March 15, 2013 Share Posted March 15, 2013 POST YOUR CODE. Quote Link to comment https://forums.phpfreaks.com/topic/275705-php-error-undefined-index/#findComment-1418861 Share on other sites More sharing options...
jiameow Posted March 15, 2013 Author Share Posted March 15, 2013 <?phpinclude_once "add2.php";connect();?><?phpif (!isset($_POST['submit'])){echo "<form method=\"post\" action=\"admin-addservice.php\" name=\"Registration Form\">";echo "<br/>";echo 'Name of Service: ' . '<br/>' . '<input type=\"text\" name=\"service\" style="width:400px;"/>' . '<br/>' . '<br/>';echo "Description: " . "<br/>";echo '<textarea cols="80" rows="10" name=\"description\" maxlength=\"1000\" style=\"overflow: hidden;\">';echo '</textarea>' . '<br/>' . '<br/>';echo "Price: " . '<br/>';echo "<input type=\"text\" name=\"price\" style='width:200px;'/>" . '<br/>';echo "<input type=\"submit\" name=\"submit\" value=\"Finish Registration!\" />";echo "</form>";?><?php}else {$service = protect($_POST['service']);$description = protect($_POST['description']);$price = protect($_POST['price']);$errors = array(); Quote Link to comment https://forums.phpfreaks.com/topic/275705-php-error-undefined-index/#findComment-1418870 Share on other sites More sharing options...
jazzman1 Posted March 15, 2013 Share Posted March 15, 2013 Where do you declare the protect function in that script? Quote Link to comment https://forums.phpfreaks.com/topic/275705-php-error-undefined-index/#findComment-1418877 Share on other sites More sharing options...
jiameow Posted March 15, 2013 Author Share Posted March 15, 2013 in our else statement Quote Link to comment https://forums.phpfreaks.com/topic/275705-php-error-undefined-index/#findComment-1418878 Share on other sites More sharing options...
jazzman1 Posted March 15, 2013 Share Posted March 15, 2013 in our else statement No, that's wrong! Quote Link to comment https://forums.phpfreaks.com/topic/275705-php-error-undefined-index/#findComment-1418879 Share on other sites More sharing options...
jiameow Posted March 15, 2013 Author Share Posted March 15, 2013 where should we put it then? Quote Link to comment https://forums.phpfreaks.com/topic/275705-php-error-undefined-index/#findComment-1418880 Share on other sites More sharing options...
jazzman1 Posted March 15, 2013 Share Posted March 15, 2013 You need to create that function before to use it. Quote Link to comment https://forums.phpfreaks.com/topic/275705-php-error-undefined-index/#findComment-1418881 Share on other sites More sharing options...
jiameow Posted March 15, 2013 Author Share Posted March 15, 2013 here is our full code   <?phpinclude_once "add2.php";connect();mysql_connect("localhost","root","");mysql_select_db("patient"); if (!isset($_POST['submit'])){echo "<form method=\"post\" action=\"admin-addservice.php\" name=\"Registration Form\">";echo "<br/>";echo 'Name of Service: ' . '<br/>' . '<input type=\"text\" name=\"services\" style="width:400px;"/>' . '<br/>' . '<br/>';echo "Description: " . "<br/>";//echo '<textarea cols="80" rows="10" name=\"description\" maxlength=\"1000\" style=\"overflow: hidden;\">';//echo '</textarea>' . '<br/>' . '<br/>';echo "<input type=\"text\" name=\"\">";echo "Price: " . '<br/>';echo "<input type=\"text\" name=\"price\" style='width:200px;'/>" . '<br/>';echo "<input type=\"submit\" name=\"submit\" value=\"Finish Registration!\" />";echo "</form>";}else {$service = protect($_POST['services']);$description = protect($_POST['description']);$price = protect($_POST['price']);$errors = array();if(!$service) {$errors[] = "Invalid Name of Service!"; }if(!$description) {$errors[] = "Invalid Description!"; }if(!$price) {$errors[] = "Invalid Price!"; }if (count($errors) > 0) {foreach($errors AS $error) {echo ("<SCRIPT LANGUAGE='JavaScript'>window.alert('$error')window.location.href='admin-addservice.php'</SCRIPT>");}}else {$sql = "INSERT INTO services(services , description , price) VALUES('$_POST[services]' , '$_POST[description]' , '$_POST[price]')";$res = mysql_query($sql) or die (mysql_error());?><script type=text/javascript>function show_alert() {var msg = "Successfully Saved!";alert(msg);}</script><?phpecho "<br/>";echo '<script type="text/javascript"> show_alert(); </script>';echo "<br/>";echo "<br/>";echo "<br/>";}}?> Quote Link to comment https://forums.phpfreaks.com/topic/275705-php-error-undefined-index/#findComment-1418882 Share on other sites More sharing options...
jiameow Posted March 15, 2013 Author Share Posted March 15, 2013 we dont have any knowledge about php so far :/ Quote Link to comment https://forums.phpfreaks.com/topic/275705-php-error-undefined-index/#findComment-1418883 Share on other sites More sharing options...
jazzman1 Posted March 15, 2013 Share Posted March 15, 2013 we dont have any knowledge about php so far :/ Â Me too. Â What do you have in the add2.php file? Quote Link to comment https://forums.phpfreaks.com/topic/275705-php-error-undefined-index/#findComment-1418886 Share on other sites More sharing options...
jiameow Posted March 15, 2013 Author Share Posted March 15, 2013 it holds the protect function and connection Quote Link to comment https://forums.phpfreaks.com/topic/275705-php-error-undefined-index/#findComment-1418887 Share on other sites More sharing options...
jiameow Posted March 15, 2013 Author Share Posted March 15, 2013 Me too.  What do you have in the add2.php file? <?php  function protect($string) { $string = mysql_real_escape_string($string); $string = strip_tags($string); $string = addslashes($string);  return $string;  } function connect () { $con = mysql_connect("localhost", "root",'') or die(mysql_error()); $db = mysql_select_db("patient", $con);  }  ?> Quote Link to comment https://forums.phpfreaks.com/topic/275705-php-error-undefined-index/#findComment-1418888 Share on other sites More sharing options...
jazzman1 Posted March 15, 2013 Share Posted March 15, 2013 (edited) Ok, then! Â Put the error_reporting(-1) to the top of the file and tell us what error(s) do you get? Â Copy/Paste my script! Â Ops....there is no return $con in connect() function inside the add2.php file! Â Â Â Â function connect () { $con = mysql_connect("localhost", "root",'') or die(mysql_error()); $db = mysql_select_db("patient", $con); return $con; } Â <?php error_reporting(-1); include_once "add2.php"; connect(); if (!isset($_POST['submit'])){ echo "<form method=\"post\" action=\"admin-addservice.php\" name=\"Registration Form\">"; echo "<br/>"; echo 'Name of Service: ' . '<br/>' . '<input type=\"text\" name=\"services\" style="width:400px;"/>' . '<br/>' . '<br/>'; echo "Description: " . "<br/>"; //echo '<textarea cols="80" rows="10" name=\"description\" maxlength=\"1000\" style=\"overflow: hidden;\">'; //echo '</textarea>' . '<br/>' . '<br/>'; echo "<input type=\"text\" name=\"\">"; echo "Price: " . '<br/>'; echo "<input type=\"text\" name=\"price\" style='width:200px;'/>" . '<br/>'; echo "<input type=\"submit\" name=\"submit\" value=\"Finish Registration!\" />"; echo "</form>"; }else { $service = protect($_POST['services']); $description = protect($_POST['description']); $price = protect($_POST['price']); $errors = array(); if(!$service) { $errors[] = "Invalid Name of Service!"; } if(!$description) { $errors[] = "Invalid Description!"; } if(!$price) { $errors[] = "Invalid Price!"; } if (count($errors) > 0) { foreach($errors AS $error) { echo ("<SCRIPT LANGUAGE='JavaScript'> window.alert('$error') window.location.href='admin-addservice.php' </SCRIPT>"); } } else { $sql = "INSERT INTO services(services , description , price) VALUES('$_POST[services]' , '$_POST[description]' , '$_POST[price]')"; $res = mysql_query($sql) or die (mysql_error()); ?> <script type="text/javascript"> function show_alert() { var msg = "Successfully Saved!"; alert(msg); } </script> <?php echo "<br/>"; echo '<script type="text/javascript"> show_alert(); </script>'; echo "<br/>"; echo "<br/>"; echo "<br/>"; } } Edited March 15, 2013 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/275705-php-error-undefined-index/#findComment-1418889 Share on other sites More sharing options...
jiameow Posted March 15, 2013 Author Share Posted March 15, 2013 Â Ok, then! Â Put the error_reporting(-1) to the top of the file and tell us what error(s) do you get? Â Copy/Paste my script! Â <?php error_reporting(-1); include_once "add2.php"; connect(); if (!isset($_POST['submit'])){ echo "<form method=\"post\" action=\"admin-addservice.php\" name=\"Registration Form\">"; echo "<br/>"; echo 'Name of Service: ' . '<br/>' . '<input type=\"text\" name=\"services\" style="width:400px;"/>' . '<br/>' . '<br/>'; echo "Description: " . "<br/>"; //echo '<textarea cols="80" rows="10" name=\"description\" maxlength=\"1000\" style=\"overflow: hidden;\">'; //echo '</textarea>' . '<br/>' . '<br/>'; echo "<input type=\"text\" name=\"\">"; echo "Price: " . '<br/>'; echo "<input type=\"text\" name=\"price\" style='width:200px;'/>" . '<br/>'; echo "<input type=\"submit\" name=\"submit\" value=\"Finish Registration!\" />"; echo "</form>"; }else { $service = protect($_POST['services']); $description = protect($_POST['description']); $price = protect($_POST['price']); $errors = array(); if(!$service) { $errors[] = "Invalid Name of Service!"; } if(!$description) { $errors[] = "Invalid Description!"; } if(!$price) { $errors[] = "Invalid Price!"; } if (count($errors) > 0) { foreach($errors AS $error) { echo ("<SCRIPT LANGUAGE='JavaScript'> window.alert('$error') window.location.href='admin-addservice.php' </SCRIPT>"); } } else { $sql = "INSERT INTO services(services , description , price) VALUES('$_POST[services]' , '$_POST[description]' , '$_POST[price]')"; $res = mysql_query($sql) or die (mysql_error()); ?> <script type="text/javascript"> function show_alert() { var msg = "Successfully Saved!"; alert(msg); } </script> <?php echo "<br/>"; echo '<script type="text/javascript"> show_alert(); </script>'; echo "<br/>"; echo "<br/>"; echo "<br/>"; } } invalid name of service! Â "Undefined index: service D:\xampp\htdocs\def\admin-addservice.php on line 93 Quote Link to comment https://forums.phpfreaks.com/topic/275705-php-error-undefined-index/#findComment-1418892 Share on other sites More sharing options...
jazzman1 Posted March 15, 2013 Share Posted March 15, 2013 (edited) Which is the line 93? Edited March 15, 2013 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/275705-php-error-undefined-index/#findComment-1418894 Share on other sites More sharing options...
jiameow Posted March 15, 2013 Author Share Posted March 15, 2013 Which is the line 93? $service = protect($_POST['services']); Quote Link to comment https://forums.phpfreaks.com/topic/275705-php-error-undefined-index/#findComment-1418896 Share on other sites More sharing options...
jazzman1 Posted March 15, 2013 Share Posted March 15, 2013 There is only 63 rows inside that particular file  Are you sure that that file is called - admin-addservice.php? Quote Link to comment https://forums.phpfreaks.com/topic/275705-php-error-undefined-index/#findComment-1418897 Share on other sites More sharing options...
jiameow Posted March 15, 2013 Author Share Posted March 15, 2013 yes Quote Link to comment https://forums.phpfreaks.com/topic/275705-php-error-undefined-index/#findComment-1418898 Share on other sites More sharing options...
jazzman1 Posted March 15, 2013 Share Posted March 15, 2013 And.... you posted the entire script? Quote Link to comment https://forums.phpfreaks.com/topic/275705-php-error-undefined-index/#findComment-1418899 Share on other sites More sharing options...
jiameow Posted March 15, 2013 Author Share Posted March 15, 2013 yup the entire php script Quote Link to comment https://forums.phpfreaks.com/topic/275705-php-error-undefined-index/#findComment-1418901 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.