kcire arraveug
kcire arraveug

Reputation: 213

Undefined index in inserting records on DB

I am working on a system and i want to check if a record exist. If a record exist then it will not record the data and instead will return to the form. If the data does not exist then it will proceed to recording the data to DB.

HTML form:

 <form name="studentform" onSubmit="return validate_form ( );" action="queries/insert.php" method="post">

Student Number: <input type="text" name="studentnumber"/>
    College:
 <select name="college" id=a></select>

&nbsp;&nbsp;Course:
<select name="course" id=b></select>

 <input type="radio" name="status" value="regular" />Regular&nbsp;&nbsp;
<input type="radio" name="status" value="irregular" />Irregular 
    <br><br>
    <hr>
    <br>
   Name:
    <input type="text" name="lname"> &nbsp;
    <input type="text" name="fname"> &nbsp;
    <input type="text" name="mname">   


Address: 
<input type="text" name="address" />

<br><br>

 Gender: 
 <select name="gender">
<option value="">---</option>  
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>



<input type="submit" value="Submit">
</form>

PHP form:

 $query = ("SELECT studentnumber FROM students where studentnumber = '$_POST[studentnumber]'");
 $result=mysql_query($query);

 if($result)
    {
      if(mysql_num_rows($result) >= 1)
      { 
      echo "<script type='text/javascript'>alert('User already exist'); location.href = '../admin_home.php';</script>";
      }
      }
   else{

$sql="INSERT INTO students (studentnumber, college, course, status, lname, fname, mname, address, gender)
VALUES
('$_POST[studentnumber]','$_POST[college]','$_POST[course]','$_POST[status]','$_POST[lname]','$_POST[fname]','$_POST[mname]','$_POST[address]','$_POST[gender]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }

  echo "<script type='text/javascript'>alert('Record Successfully Added'); location.href = '../admin_home.php';</script>";
}

I don't know why but i always get the undefined index error. Maybe i've done something wrong somewhere. Thanks !!

Upvotes: 1

Views: 2308

Answers (3)

Ayush
Ayush

Reputation: 42450

The problem is in your queries:

 $query = ("SELECT studentnumber FROM students where studentnumber = '$_POST[studentnumber]'");

$_POST[studentnumber] is not correct. It needs to be $_POST['studentnumber']. Notice the quotes around the key.

I suggest doing it this way:

$query = sprintf("SELECT studentnumber FROM students where studentnumber = '%s'"
                  , mysql_real_escape_string($_POST['studentnumber']));

Change all your queries accordingly.

Upvotes: 1

Grim...
Grim...

Reputation: 16953

"Undefined index" is referring to your array ($_POST, probably), and it should be a notice, not an error. Can you post the exact message?

In the meantime, switch your first line for

$query = "SELECT studentnumber FROM students where studentnumber = '".mysql_real_escape_string($_POST['studentnumber'])."'";

Also, it's helpful for debugging to print out the query to make sure it looks like you'd expect:

print $query."<br />";  // obviously

[edit]As you've now posted the error message, it becomes far more simple - $_POST['studentnumber'] does not exist. Check your form.

A good way to debug posted results is to use the code

print '<pre>';
print_r($_POST);
print '</pre>';

Upvotes: 1

jogesh_pi
jogesh_pi

Reputation: 9782

try with this:

if( isset($_POST['submit']) ){

  $student_num = mysql_real_escape_string( $_POST['studentnumber'] );
  // Set all the require form fields here with mysql_real_escape_string() fun

  if( !empty($student_num) ){
    // Your Query Here
  }
  else{
   echo 'Value not Set in Student Number Field!';
  }
}

Edit: first check all the fields after isset($_POST['submit']) so that you confirm about all the values are properly getting or not

after getting all the required values start your query

Upvotes: 0

Related Questions