Jump to content

why is this error making????


mtvaran

Recommended Posts

well i have a task which is,

i have two table in my database which are student (StudentID, StudentName)& course(CourseID, CourseName). i have displayed the field st_id from student (drop-down list) & course_id from course (multi select list). now i need to insert into another table called take which contain the feild StudentID and CourseID. so i have to select one st_id and one or more course_id then submit. 

there is the code for that so cud you pls check it 4 me.

?php


$result  = mysql_query("SELECT * FROM course") or trigger_error('MySQL error: ' . mysql_error());


echo '<select name ="cid[]" multiple="multiple" size="10">';

while($row = mysql_fetch_array($result))
{ 
    echo '<option value="' . $row['CourseID'] . '">' . $row['CourseName'] . '</option>';     
}

echo '</select>';

// ----------------

$result  = mysql_query("SELECT * FROM student") or trigger_error('MySQL error: ' . mysql_error());


echo '<select name="sid">';

while($row = mysql_fetch_array($result))
{
    
    echo '<option value="' . $row['StudentID'] . '">' . $row['StudentName'] . '</option>';   
}

echo '</select>';

// ----------------

if (!empty($_POST['sid']) && !empty($_POST['cid']))
{
    $student = mysql_real_escape_string($_POST['sid']);
    $sql = "INSERT INTO take (StudentID, CourseID) VALUES";

    foreach ($_POST['cid'] as $key => $course)
    {
        $sql .= ($key != 0) ? ", " : " ";
        $sql .= "('" . $student . "', '" . mysql_real_escape_string($course) . "')";
    }

    $query = mysql_query($sql) or trigger_error('MySQL error: ' . mysql_error());

    if (mysql_affected_rows($query) > 0)
    {
        echo mysql_affected_rows($query) . ' rows added.';
    }
}

?>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.