Jump to content

How to delete a composite key in PHP


sugarplum_19

Recommended Posts

I am trying to delete a row of data in the data base, but in this table the primary key is a composite key, (SID, CID). How can i delete a row of data below shows the code that I have done

 

 

function delete_studentcourse($SID, $CID) {

        $esc_SID = mysql_real_escape_string($SID, $this->conn);

        $esc_CID = mysql_real_escape_string($CID, $this->conn);

        $sql = "DELETE FROM studentcourses where SID && CID='($SID, $CID, $grade, $comments)'";

        $result = mysql_query($sql, $this->conn);

 

        if (!$result)

            die("SQL Error: " . mysql_error());

        else {

            $numofrows = mysql_affected_rows($this->conn);

            return $numofrows;

        }

    }

 

 

<?php include 'db_studentcourse.php'; //import the class in this web page  ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

    <head>

        <title>Delete Student-course</title>

    </head>

    <body>

        <?php

 

 

        if (!$_POST) { //page loads for the first time

        ?>

            <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">

                Enter the student id you want to delete:<input type="text" name="SID" /><br />

                Enter the course id you want to delete:<input type="text" name="CID" /><br />

                <input type="submit" value="Save" />

            </form>

<?php

        } else {

            $db1 = new db_studentcourse();

            $db1->openDB();

            $numofrows = $db1->delete_studentcourse($SID);

            $numofrows = $db1->delete_studentcourse($CID);

            echo "Success. Number of rows affected:<strong>{$numofrows}<strong>";

            $db1->closeDB();

        }

?>

    </body>

</html>

 

 

 

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.