In this code i got undefined index:category in this i get the values from table name coursemaster col(course_code)now when i click upload the dropdown value is not inserted into student_table. So any one help me.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Code Igniter</title>
</head>
<?php include_once('header.php'); ?>
<?php include_once('menu.php'); ?>
<br />
<div class="gray_bg">
<div class="container">
<div class="row welcome_inner">
<div class="span12">
<h1><span class="colored">///</span> Upload</h1>
</div>
</div>
</div>
</div>
<div id="details">
</div></div></center>
<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
File to import:<br />
<input size='30' type='file' name='filename'>
Enter subject code:<input type="text" name="fname1" />
Enter subject code:<input type="text" name="fname2" />
<input type="submit" name="submit" value="Upload"></form>
<?php
$host="localhost";
$username="root";
$password="";
$db_name="slseatapp";
mysql_connect("$host", "$username", "$password")or die("cannot connect to server");
mysql_select_db("$db_name")or die("cannot select db");
$query="SELECT id,course_code FROM `coursemaster` ORDER BY `coursemaster`.`id`";
$result = mysql_query($query);
?>
<select name="category">
<?php
while($nt=mysql_fetch_array($result)) {
echo '<option value="'.$nt['course_code'].'">'.$nt['course_code'].'</option>';
}
?>
</select>
<?php
if($_POST){
echo 'The course_code selected is'.$_POST['category'];
}
?>
<?php
mysql_connect("localhost", "root", "") or die("Error connecting to database: ".mysql_error());
mysql_select_db("slseatapp") or die(mysql_error());
//Upload File
if (isset($_POST['submit'])) {
if (is_uploaded_file($_FILES['filename']['tmp_name'])) {
//Import uploaded file to Database
$row = 1;
$handle = fopen($_FILES['filename']['tmp_name'], "r");
$var = $_POST['category'];
$var1 = $_POST['fname1'];
$var2 = $_POST['fname2'];
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
//Update Database Values
$import="insert into student_table (id,register_number,name,course_code,subject_code,exam_name) VALUES('".mysql_real_escape_string($data[0])."', '".mysql_real_escape_string($data[1])."','".mysql_real_escape_string($data[2])."','$var','$var1','$var2')";
$import="replace into student_table (id,register_number,name,course_code,subject_code,exam_name) VALUES('".mysql_real_escape_string($data[0])."', '".mysql_real_escape_string($data[1])."','".mysql_real_escape_string($data[2])."','$var','$var1','$var2')";
mysql_query($import) or die(mysql_error());
}
fclose($handle);
echo"uploaded successfully";
}
}
?>
<br />
<?php include_once('footer.php'); ?>
</body>
</html>
Seems your HTML is not perfect, try this
<select name="category">
<?php
while($nt=mysql_fetch_array($result)) {
echo '<option value="'.$nt['course_code'].'">'.$nt['course_code'].'</option>';
}
?>
</select>
firsly you have miss "
around name of select try to change this:
<select name=category>
<?php
while($nt=mysql_fetch_array($result)) {
echo "<option value='".$nt['course_code']."'>".$nt['course_code']."</option>";
}
?>
</select>
to this:
<select name="category">
<?php
while($nt=mysql_fetch_array($result)) {
echo "<option value='".$nt['course_code']."'>".$nt['course_code']."</option>";
}
?>
</select>
You have too many forms, and you want to update all in ne. You should use only one form and only one submit button to upload all your values try to use only one form and only one submit for all your values. You have a form for the inout type file and a form for the select category, you should use only one for each other.
Try