I have two tables:
'courses' with these fields:COURSE_ID(auto increment),start_date,end_date,title
'courses_students' with these fields:ID(auto increment),COURSE_ID,STUDENT_ID.
I want to insert some values in my mysql table called "courses_students" from my other table called "courses". Users can see in a page the data from 'courses'(courses names,starting dates,ending dates) and they must select which course they want to attend,by clicking the button 'attent course'.
Everytime someone clicks the submit button,the values are inserted in courses_students table,but not correctly.The problem is that everytime,the COURSE_ID from 'courses_students' has the value of the last COURSE_ID from 'courses'.And,other strange problem is that the values are inserted twice,everytime.
This is the code:
<?php
$link = mysql_connect('localhost','root','');
if(!$link){
die('Could not connect: '.mysql_error());
}
mysql_selectdb("db");
?>
<ul>
<?php
$sql = "SELECT * FROM courses";
$result = mysql_query($sql);
while($file = mysql_fetch_array($result)){
echo '<ul>';
echo '<li>';
$STUDENT_ID = $_SESSION['ID'];
$COURSE_ID = $file['COURSE_ID']; //**It dislays the CORRECT ID for each course!**
echo 'the course id: ' .$COURSE_ID;
echo 'course name: ' .$file['title'];
echo 'Starting: ' .$file['start_date'];
echo ' ending: '.$file['end_date'];
echo '<form action="lista_cursuri.php" method="post"> <input type="submit" name="submit" value="attent course!"> </form>';
echo '</li>';
}
?>
</ul>
<?php
if(isset($_POST['submit'])){
$sql1 = "INSERT INTO `courses_students` (COURSE_ID,STUDENT_ID) VALUES ($COURSE_ID,$STUDENT_ID)";
$result = mysql_query($sql1);
}
?>
I can't manage to see where the problem is.Maybe this is not the correct procedure.
mysql_error()say?$_SESSION['ID']. Issession_start();include in your file(s) ? It's not posted/mentioned in your question. This is required and will not work otherwise. @AdiCrainic