2

I have a form with more then 4 fields and all data inserted into those fields are related, so i want to achieve something different.
For example - I have 4 fields with names class class_time class_teacher & batch_name and each time i fill and submit the form the data will be submitted into database, basically it's a time-table script.
So if first time i added maths 12-01 ashok IAS these values and submitted the form and it got saved, now if second time i add same time and teacher name with different batch then it should show an error and form should not submit, because same teacher can not appear at two different classes at the same time. How could i achieve this with PHP.
Here are some code which i tried.

$sql2 = "SELECT * FROM `s_timetable`";
$res = mysql_query($sql2) or die(mysql_error());
$fetch_data = mysql_fetch_array($res);

if ($fetch_data['class_info'] && $fetch_data['teacher_info']) == $_POST['class'] && $_POST['teacher'] 
{

}
2
  • You shouldn't use any mysql_*-functions. They have been deprecated since php 5.5 and completely removed in php 7.0 Commented Sep 13, 2016 at 6:53
  • As your already having time and teacher from POST, make a query if the entry exists, if not then make a entry else reject the entry with an alert. Commented Sep 13, 2016 at 6:54

4 Answers 4

3

before inserting new data you can check the things by below given query and if that query return more than 0 rows you can show an error message

select * from `tablename` where `class_time` = '".$class_time."' and `class_teacher`='".$class_teacher."'

You shouldn't use any mysql_*-functions. They have been deprecated since php 5.5 and completely removed in latest php versions

Sign up to request clarification or add additional context in comments.

Comments

1

Instead of doing this in PHP you could just add a composite unique key that would not allow having the same teacher at the same time in the database.

ALTER TABLE s_timetable ADD CONSTRAINT myUniqueConstraint UNIQUE(class_time, class_teacher);

Now everytime you would try to insert new data it would return an SQL error if the same teacher is there at the same time.

Multiple column index documentation

Comments

1

You need to check first that user is already in the table or not.

$sql = mysql_query("select * from table_name where class_time ='" . $_REQUEST['class_time'] . "' AND class_teacher ='" . $_REQUEST['class_teacher'] . "'");

$record = mysql_num_row($sql);

if ($record) {
    echo "Record already exits";
    exit();
} else {
//insert query
}

Note : Stop using mysql it is deprecated.

3 Comments

batch name not required only class time and teacher name is ok
@VivekSingh Yup batch name not required, thanks for help.
Updated answer.
0

Simply add a query that will select class_time and class_teacher so that if it already exist you can produce an error msg.

   $checksched = mysqli_query($con,"SELECT * FROM s_timetable  WHERE class_time = '".$inputted_time."'  and  class_teacher = 'inputted_teacher' ")
   $match  = mysqli_num_rows($checksched);

if ($match > 0){
echo "<script>
alert('Teacher already exist!');
</script>";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.