Jump to content

Some help with relational DB query


BigChief

Recommended Posts

I'm wondering if anyone can help me with a little problem I have.

 

I have the following tables:

 

users

courses

skillsets

cidsid

 

I have a page which we will call courses for now and this page allows you to add a new course placing the data from that page into the courses table.

 

The courses page pulls data from the skillsets table to allow you to add a course to a specific skillset.

 

Here is my problem:

 

On submit the courses page inserts the data into the courses table but I would like it to take the course id it has just created based on the name given to the new course, take the skillname from the same row, lookup the skillname on the same row but in the skillsets table, take the ID that relates to it in the skillsets table and post both the skillsetid and the new course id into the table cidsid.

 

I know that sounds a wee bit confusing so here is what I thought (but obviously am wrong) the sql should be.

 

$courseid = mysql_query ("SELECT courseid FROM courses WHERE coursename = " . $_POST['cname'] );

$skillid = mysql_query ("SELECT skillid FROM skillsets WHERE skillname = " . $_POST['skillname'] );

mysql_query ("INSERT INTO cidsid (cid, sid) VALUES EQUALS (".$courseid.", ".$skillid.")");

 

Any help would be greatly appreciated.

Link to comment
Share on other sites

I finally got it:

 

$query = "SELECT courseid FROM courses WHERE coursename = '" . $_POST['cname'] ."'" ;

$result = mysql_query ($query);

$row = mysql_fetch_array($result);

$courseid = $row['courseid'];

 

$query = "SELECT skillid FROM skillsets WHERE skillname = '" . $_POST['skillname'] ."'";

$result = mysql_query ($query);

$row = mysql_fetch_array($result);

$skillid = $row['skillid'];

 

$query = "INSERT INTO cidsid (cid, sid) VALUES (".$courseid.", ".$skillid.")";

$success = mysql_query ($query);

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.