Reputation: 356
I would like to get user values from an HTML form to an array and then insert it into a mysql database. it insert correct values into the database, then it gives out the following error:
Notice: Undefined index: TopicNum in C:\wamp\www\Capturing System\All_Topics_widget.php on line 153
The following is right at the top of the page:
<?php
include 'scripts/functions/init.php';
Restrict();
?>
$userid = $_SESSION['userid'];
$KM_Number = $_SESSION['C_KM'];
$query = "SELECT * FROM knowledge_modules where km_number = "."'".$KM_Number."'";
$Knowledge = mysql_query($query) or die(mysql_error);
$row = mysql_fetch_assoc($Knowledge);
$Module_type = 'Knowledge Modules';
$Purpose_M = $_SESSION['Purpose_KM'];
$KM = 'KM';
$NUM = $_SESSION['KM'];
$KT = 'KT';
if (empty($_POST)=== false)
{
$R_fields = array('Topic_Num','Title','Weight');
foreach($_POST as $key=>$value)
{
if (empty($value) && in_array($key,$R_fields)=== true)
{
$errors[] = 'fields marked with (*) are required';
break 1;
}
}
if(empty($errors)=== true)
{
if(topic_exists($_POST['TopicNum']))
{
$errors[] = 'Sorry, the Topic already exist!';
}
}
}
?> The following code is within the HTML Tags:
<h2>Tools - </h2>
<form action="All_Topics_widget.php" method="POST" enctype="multipart/form-data">
<fieldset>
<table border="0">
<tr>
<td><label for="TopicNum">Topic Number:* <?php echo $KM.'-'.$NUM.'-'.$KT ?></label></td>
<td><input type="text" size="5" name="TopicNum" /></td>
</tr>
<tr>
<td><label for="Title">Title:*</label></td>
<td><input type="Text" size="35" name="Title"/></td>
</tr>
<tr>
<td><label for="Weight">Weight:*</label></td>
<td><input type="text" size="05" name="Weight" /></td>
</tr>
</table>
</fieldset>
<fieldset>
<table border="0">
<tr>
<td><input id="Submit" type="submit" value="Capture" /></td>
<td><input id="Reset1" type="reset" value="reset" /></td>
<td><a href="list_topics.php">Capture Topics</a></td>
</tr>
</table>
</fieldset>
<?php
$Topics = $KM.'-'.$NUM.'-'.$KT.$_POST['TopicNum'];//This is line 153
if(isset($_GET['success']) && empty($_GET['success']))
{
echo 'Succefully Captured';
}
else
{
if(empty($_POST) == false && empty($errors)== true)
{
//Capture data from the fields to an array
$capture_topic = array(
'TopicNum' => $Topics,
'Title'=>$_POST['Title'],
'Weight'=>$_POST['Weight'],
'Purpose_M'=>$Purpose_M,
'userid'=>$userid);
//Submit the data into the database
capture_topic($capture_topic);
//redirect to the Modules widget page till the user press NEXT Page
header('Location: All_Topics_widget.php?success');
exit();
}
else if(empty($errors)== false)
{
//Display errors
echo output($errors);
}
}
?>
</form>
Upvotes: 1
Views: 1979
Reputation: 474
Look at this
header('Location: All_Topics_widget.php?success');
exit();
After you successfully processed POST data, you redirect the script to itself. And POST data do not exist after this redirect.
Rewrite
$Topics = $KM.'-'.$NUM.'-'.$KT.$_POST['TopicNum'];//This is line 153
if(isset($_GET['success']) && empty($_GET['success']))
{
echo 'Succefully Captured';
}
else
{
if(empty($_POST) == false && empty($errors)== true)
as
if(isset($_GET['success']) && empty($_GET['success']))
{
echo 'Succefully Captured';
}
else
{
$Topics = $KM.'-'.$NUM.'-'.$KT.$_POST['TopicNum'];
if(empty($_POST) == false && empty($errors)== true)
to suppress the notification.
Please, note what in array of required fields
$R_fields = array('Topic_Num','Title','Weight');
You expect to see a filed named Topic_Num, but in your form it's name is TopicNum
Upvotes: 1
Reputation: 12433
This Notice
is appearing because you are referencing $_POST['TopicNum']
on line 153, when this $_POST[]
variable is no longer set.
Try moving line 153 inside if(empty($_POST) == false && empty($errors)== true) {}
if(empty($_POST) == false && empty($errors)== true)
{
$Topics = $KM.'-'.$NUM.'-'.$KT.$_POST['TopicNum'];//This is line 153
...
}
Here is why it is causing the Notice
-
1=> You submit/POST the form, thus setting $_POST['TopicNum']
and used on line 153.
2=>You input the values into the database on line 172 capture_topic($capture_topic);
3=>You redirect to the page on 174 header('Location: All_Topics_widget.php?success');
, thus clearing the $_POST[]
globals, so when it is called again on 153 it is now undefined. by moving it inside your check for if(empty($_POST) == false && empty($errors)== true)
it will only be reference when the form has been submitted/POSTed
Upvotes: 1