Lee Posted April 4, 2010 Share Posted April 4, 2010 Hi, I'm still a bit of a noob with php, so this code might be a bit ugly, but anyway I'm making a form to send data to a mysql table. As a rule I was told to check isset on one of the input fields, which should surpress the undefined index error. I did this but I still get: Undefined index: year in D:\wamp\www\practice\karaoke-site\admin\functions.php on line 37 Can anyone tell me where I've gone wrong? Thanks function SetupForm($params) { if(isset ($_POST ['year']) ) { $CompanyName = $_POST['name']; $email = $_POST['email']; $area = $_POST['area']; $description = $_POST['description']; $year = $_POST['year']; } $CompanyName = $params['name']; $email = $params['email']; $area = $params['area']; $description = $params['description']; $year = $params['year']; $errors = array(); if(strlen($CompanyName) < 1) $errors[] = 'Company name must be at least 2 characters.'; if(strlen($area) < 2) $errors[] = 'You must enter the area where you operate.'; if(strlen($description) < 1) $errors[] = 'You must enter a description for your company.'; if( (strlen($year) < 4 )&&(!is_numeric($year)) ) $errors[] = 'You must enter a 4 digit year i.e. 2009.'; if(!preg_match('/^[A-Za-z][\w._-]+@\w[\w-.]+\.[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?$/' , $email)) $errors[] = 'You must enter a valid email address'; if($errors) return $errors; $query = sprintf ( "INSERT into setup set name = '%s', email = '%s', area = '%s', description = '%s', year = '%s' ", mysql_real_escape_string($params['name']), mysql_real_escape_string($params['email']), mysql_real_escape_string($params['area']), mysql_real_escape_string($params['description']), mysql_real_escape_string($params['year']) ); $result = mysql_query($query); if (!$result) { return false; } else { return true; } } Link to comment https://forums.phpfreaks.com/topic/197532-checked-if-isset-but-still-get-undefined-index-whats-wrong-here/ Share on other sites More sharing options...
TeddyKiller Posted April 4, 2010 Share Posted April 4, 2010 What? You define $_POST as variables, but you define $params as the same variables. What is the objective of this? Also, if you have a submit button, it'll be better to check isset on the submit button. Rather than a form field? It's hard to help without the html form too and all that. Link to comment https://forums.phpfreaks.com/topic/197532-checked-if-isset-but-still-get-undefined-index-whats-wrong-here/#findComment-1036713 Share on other sites More sharing options...
Lee Posted April 4, 2010 Author Share Posted April 4, 2010 What? You define $_POST as variables, but you define $params as the same variables. What is the objective of this? Also, if you have a submit button, it'll be better to check isset on the submit button. Rather than a form field? It's hard to help without the html form too and all that. Hi, thanks for the reply. At the top of the function, I set $CompanyName = $_POST['name']; and so on.... to grab the input from the form. Then later I set $CompanyName = $params['name']; and so on.... to do the mysql query. I don't really know how else to shorten that. This is the form: <?php require_once ('functions.php'); if(isset($_POST['submit'])) { $result = SetupForm($_POST); if($result === true) { echo 'Setup details successfully submitted!'; die(); } } ?> <legend>Setup Form</legend> <?php if(isset($result) && $result) { echo '<div align="center"><ul>'; foreach($result as $error) echo '<li>' . $error . '</li>'; echo '</ul></div>'; } ?> <div align="center" > <form id="setup" name="setup" method="post" action="setup.php"> <b>Enter your name:</b><br /> <input name="name" type="text" id="name" size="60" /> <br /> <br /> <b>Enter the area where you are based:</b><br /> <input name="area" type="text" id="area" size="60" /> <br /> <br /> <b>Your email address: </b> <br /> <input name="email" type="text" id="email" size="60" maxlength="80" /> <b><br /> <br /> Short description about you:</b><br /> <input name="description" type="text" id="description" size="60" /> <b><br /> <br /> Enter the year that you started: </b> <br /> <input name="description2" type="text" id="description2" size="4" maxlength="4" /> <br /> <br /> <label> <input type="submit" name="submit" value="submit" /> </label> </form> </div> Link to comment https://forums.phpfreaks.com/topic/197532-checked-if-isset-but-still-get-undefined-index-whats-wrong-here/#findComment-1036724 Share on other sites More sharing options...
TeddyKiller Posted April 4, 2010 Share Posted April 4, 2010 <input name="description2" type="text" id="description2" size="4" maxlength="4" /> This should be name="year" I'm guessing? Also, you don't need $CompanyName = $_POST['name']; $email = $_POST['email']; $area = $_POST['area']; $description = $_POST['description']; $year = $_POST['year']; Because it's changed $_POST to $params, so using $_POST anywherei n the function is completely useless. Link to comment https://forums.phpfreaks.com/topic/197532-checked-if-isset-but-still-get-undefined-index-whats-wrong-here/#findComment-1036727 Share on other sites More sharing options...
TeddyKiller Posted April 4, 2010 Share Posted April 4, 2010 Also, I'm certain this will give you a better result. <?php function SetupForm($params) { if(isset($param['submit'])){ $errors = array(); if(strlen($params['name']) < 1) { $errors[] = 'Company name must be at least 2 characters.'; } if(strlen($params['area']) < 2) { $errors[] = 'You must enter the area where you operate.'; } if(strlen($params['description']) < 1) { $errors[] = 'You must enter a description for your company.'; } if( (strlen($params['year']) < 4 )&&(!is_numeric($year)) ) { $errors[] = 'You must enter a 4 digit year i.e. 2009.'; } if(!preg_match('/^[A-Za-z][\w._-]+@\w[\w-.]+\.[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?$/' , $params['email'])) { $errors[] = 'You must enter a valid email address'; } if($errors) { return $errors; } else { $CompanyName = mysql_real_escape_string($params['name']); $email = mysql_real_escape_string($params['email']); $area = mysql_real_escape_string($params['area']); $description = mysql_real_escape_string($params['description']); $year = mysql_real_escape_string($params['year']); $query = sprintf ("INSERT into setup SET name='$CompanyName', email='$email', area='$area', description='$description', year='$year'"); $result = mysql_query($query); if (!$result) { return false; } else { return true; } } } } ?> Link to comment https://forums.phpfreaks.com/topic/197532-checked-if-isset-but-still-get-undefined-index-whats-wrong-here/#findComment-1036731 Share on other sites More sharing options...
Lee Posted April 4, 2010 Author Share Posted April 4, 2010 <input name="description2" type="text" id="description2" size="4" maxlength="4" /> This should be name="year" I'm guessing? OOPS!! What a rookie mistake, gives away that I am using dreamweaver to quickly copy & paste form fields. Thanks But at least I know now that there is something more important wrong with my function. So I'm guessing I should define those variables that grab the form data outside the function? Link to comment https://forums.phpfreaks.com/topic/197532-checked-if-isset-but-still-get-undefined-index-whats-wrong-here/#findComment-1036732 Share on other sites More sharing options...
TeddyKiller Posted April 4, 2010 Share Posted April 4, 2010 Or you can just use my post Link to comment https://forums.phpfreaks.com/topic/197532-checked-if-isset-but-still-get-undefined-index-whats-wrong-here/#findComment-1036735 Share on other sites More sharing options...
Lee Posted April 4, 2010 Author Share Posted April 4, 2010 Ah thanks, that's much cleaner lol. However, with that code, it is not collecting the data from the form and the errors do not return now. Sorry, I'm really struggling to get to grips with functions (which is why I am trying this exercise), so although I can see what you have written is doing, I'm a little lost now about collecting the form data and returning the error. Link to comment https://forums.phpfreaks.com/topic/197532-checked-if-isset-but-still-get-undefined-index-whats-wrong-here/#findComment-1036741 Share on other sites More sharing options...
TeddyKiller Posted April 4, 2010 Share Posted April 4, 2010 Oh sorry. if(isset($param['submit'])){ Should be if(isset($params['submit'])){ Link to comment https://forums.phpfreaks.com/topic/197532-checked-if-isset-but-still-get-undefined-index-whats-wrong-here/#findComment-1036743 Share on other sites More sharing options...
Lee Posted April 4, 2010 Author Share Posted April 4, 2010 Haha, I'm making you do it now. That works a treat now. I just realised how the form data is being sent doh! <?php if(isset($_POST['submit'])) { $result = SetupForm($_POST); if($result === true) { echo 'Congratulations - Your setup details have been submitted!'; die(); } } ?> Thanks a lot, that has really helped. Link to comment https://forums.phpfreaks.com/topic/197532-checked-if-isset-but-still-get-undefined-index-whats-wrong-here/#findComment-1036747 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.