1

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.

12
  • What does mysql_error() say? Commented Nov 13, 2013 at 15:31
  • where in this code are you updating a table? Commented Nov 13, 2013 at 15:32
  • One thing, you're using sessions $_SESSION['ID']. Is session_start(); include in your file(s) ? It's not posted/mentioned in your question. This is required and will not work otherwise. @AdiCrainic Commented Nov 13, 2013 at 15:33
  • @JohnConde .i don't receive any error.Values are inserted but,as i said,the problem is with course_id field. Commented Nov 13, 2013 at 15:34
  • 1
    Yes, but you are not storing them in the form. Commented Nov 13, 2013 at 15:41

3 Answers 3

3

Salut

Change mysql_fetch_array which is a multidimensional array once numerically indexed and once by field name which gives you double results to mysql_fetch_assoc

and $COURSE_ID is always last because for each loop it is overwritten

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

1 Comment

Buna, Thanks, after changing,it inserts now the values only one time.But the bigges problem remains.I will check the solution from the others. :)
2

Try this:

Part 1:

while($file = mysql_fetch_assoc($result)){
echo '<ul>';
echo '<li>';

$STUDENT_ID = $_SESSION['ID'];  
$COURSE_ID = $file['COURSE_ID'];
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='hidden' name='courseid' value='".$COURSE_ID."' >
      <input type='submit' name='submit' value='attent course!''> </form>";
echo '</li>';

} 

Part 2:

if(isset($_POST['submit'])){
  $course = $_POST['courseid'];
  $sql1 = "INSERT INTO `courses_students` (COURSE_ID,STUDENT_ID) VALUES     ($course,$STUDENT_ID)";
  $result = mysql_query($sql1);

}

1 Comment

It worked! Thank you very much...it was very important to manage this!
0

Try this:

$sql1 = "INSERT INTO `courses_students` (COURSE_ID,STUDENT_ID) 
VALUES ('".$COURSE_ID."','".$STUDENT_ID."')"; $result = mysql_query($sql1);

2 Comments

Use proper indents next time you give an answer (I edited it). Otherwise your code doesn't "look" like code ;-)
No problemo, I understand. You can try this trick. Use 4 spaces followed by your 1st line of code, should work.

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.