Jump to content

anyone check this code plss!


mtvaran

Recommended Posts

try
   {
$sql   = "SELECT StudentID,CourseID FROM student,course WHERE StudentID =" . $_POST['sid']  AND CourseID =".$_POST['cid'] ; //-----> error line

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

    if (mysql_num_rows($query) > 0)
    {
        throw new Exception('StudentID already taken');
    }
}

 

NB: basicaly im inserting data into a table from another two table field.

Link to comment
Share on other sites

still i get error msg. im bit new & stupid with php. so i just post all of my coding so could any1 check this for me plss

<?php

$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

  mysql_select_db("uni", $con)or trigger_error('MySQL error: ' . mysql_error());
   try
   {
$sql   = "SELECT StudentID,CourseID FROM take WHERE StudentID =" . $_POST['sid'] . " AND CourseID =" .$_POST['cid'] ;
    $query = mysql_query($sql) or trigger_error('MySQL error: ' . mysql_error());

    if (mysql_num_rows($query) > 0)
    {
        echo'StudentID already taken';
    }
}
if (!empty($_POST['sid']) && !empty($_POST['cid'])) //-------> error line
{
    $ct = 0;
    $student = $_POST['sid'];
    foreach ($_POST['cid'] as $key => $course)
    {
     $sql = "INSERT INTO take (StudentID, CourseID) VALUES('".mysql_real_escape_string($student)."','".mysql_real_escape_string($course)."')";
     $query = mysql_query($sql) or trigger_error('MySQL error: ' . mysql_error());
     if (mysql_affected_rows() > 0){$ct++;}
     }
    echo $ct . ' rows added.';
}  

mysql_close($con);

?>

 

Link to comment
Share on other sites

<?php

$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

  mysql_select_db("uni", $con)or trigger_error('MySQL error: ' . mysql_error());
   try
   {
$sql   = "SELECT StudentID,CourseID FROM take WHERE StudentID =" . $_POST['sid'] . " AND CourseID =" .$_POST['cid'] ;
    $query = mysql_query($sql) or trigger_error('MySQL error: ' . mysql_error());

    if (mysql_num_rows($query) > 0)
    {
        echo'StudentID already taken';
    }
}
if (!empty($_POST['sid']) && !empty($_POST['cid'])) //-------> error line
{
    $ct = 0;
    $student = $_POST['sid'];
    foreach ($_POST['cid'] as $key => $course)
    {
     $sql = "INSERT INTO take (StudentID, CourseID) VALUES('".mysql_real_escape_string($student)."','".mysql_real_escape_string($course)."')";
     $query = mysql_query($sql) or trigger_error('MySQL error: ' . mysql_error());
     if (mysql_affected_rows() > 0){$ct++;}
     }
    echo $ct . ' rows added.';
}  

mysql_close($con);

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.