Welcome! Log In Create A New Profile

Advanced

SQL issues

Posted by Tobler 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
SQL issues
November 07, 2007 05:37PM
Hi guys, I'm close, but I'm struggling with some of the sql relating to updating the course_student table. When I pass my registration data to the page that handles the sql request I pass my course variable which contains the course name. I then try to obtain the cid from the db using this variable in the following request: $sql_cid = "SELECT 'cid' FROM 'course' WHERE 'cname' = '$cname'"winking smiley. Clearly I'm going wrong somewhere, but not sure where. Any help?
avatar
Mac
Re: SQL issues
November 08, 2007 09:39AM
SELECT 'cid' FROM 'course' WHERE 'cname' = '$cname'

SELECT cid FROM course WHERE 'cname' = '$cname'

No '
Re: SQL issues
November 08, 2007 11:14AM
Thanks mac.

Although this results in something being displayed(specifically: "Resource id #6" - for every registration I make) I still don't get the specific cid out of the table. I seem not to extract the data correctly.

Since the table of courses isn't that large, I might instead try getting the data using an array. I haven't found any references on the web thus far that use a variable from a form in the sql query.
Re: SQL issues
November 08, 2007 01:54PM
just a thought, have you thought about passing through the course ID?

in some strange occurrences, there could be 2 courses with the same name but all courses will have a unique ID number (cid).

In regards to the "Resource", this is how i do my queries:

// this adds you query string into a variable
$courseQuery = "SELECT * FROM course ORDER BY cname";

// this does the actual query on the DB
$courseResult = mysql_query($courseQuery) or die(mysql_error());

// if i print_r($courseResult) here, i will get that resourse_id #6 but do it inside the while loop and your a winner!

// here is where we will actually extract the variable we are looking for. this will loop through each record.
while ($row = mysql_fetch_array($courseResult)){
// print_r($row);
echo $row['cid'];
echo '<br />' . $row['canme'];
}

if this doesnt help, please just shout!!
Gilham
Re: SQL issues
November 08, 2007 03:29PM
Thanks G. Head is fried, heading home now, will give it a shot then. I pretty much have the same up to the loop(which is where I start having the issues). I'll report my success tomorrow thumbs up
Re: SQL issues
November 09, 2007 12:44PM
Gilham, so I had previously done pretty much the same as you had suggested. I struggled to compare the values of the variables, so I've now gone the lazy route and posted a table for the user to see & a drop-down with only the cid which then gets passed along to the next page. It works now, so I'm not complaining, but I'm still going to figure this all out in the future because I know there must be a much more efficient way of doing it than I have smile
Re: SQL issues
November 12, 2007 09:11AM
Hey Tobler

would you please drop your code in here so i can see what you are doing?

i dont 100% know what you are trying to do. i am more than sure we can figure it out

Gilham
Re: SQL issues
November 12, 2007 12:43PM
Hi Gilham,

apologies, on re-reading my posts I realise how obscure everything sounds!

I was trying to pass the course name from the registration page(basically just the form) to a page that does the sql queries(reg_confirm.php). I then wanted to inject the passed variable($cname) into an sql query to extract the relevant cid from the db. I then wanted to pass the cid, sno & year to the course_register table.

The $cname variable was passed through correctly, and I was also able to get the last created sno. I tripped up when trying to capture the cid from the db using the $cname variable in the WHERE portion of the query(as posted above). It just didn't work.

I eventually changed it all to pass the cid from the form, and that works.

I'll check my code this evening & post the original tomorrow(if I still have it), so that next years students may benefit from this issue.
Re: SQL issues
November 13, 2007 09:20AM
Awesome Tobler

Its always better to pass through ID's and work with them because then you dont get duplicate results.

Even though, using $cname should still work.

Gilham
Re: SQL issues
November 14, 2007 03:27PM
Thanks, I'll keep that in mind. It's a bit hectic here at the moment, so I haven't had time to get the code.

Thanks for all the help.
Sorry, only registered users may post in this forum.

Click here to login