Welcome! Log In Create A New Profile

Advanced

Prac

Posted by Mac 
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
Re: Prac
May 23, 2006 12:50PM
Hi I need help with these two concepts please


1

I provide a list of checkboxes, one for each of the courses
I use $course_sql = "SELECT * FROM course order by cname";
this works well
I want to display only the courses where cname is not null
if I use $course_sql = "SELECT * FROM course order by cname WHERE cname not null"; I get an error

I have also tried cname<>null, I have even tried = null but also gives an error.



2

I receive data from the page mentioned above which includes date and the sno
I send with input type submit and a form method post
I catch with $course = array ($_POST['course']);

I thought that I could be able to use the $course as an array of data containing only cid's but I do not seem to get this sorted out


I have confirmed that this data actually arrives see code below

<?php
include("header.php"winking smiley;
$course = array ($_POST['course']);
$date = $_POST['date'];
$sno = $_POST['sno'];
// connects to the database
$link - mysql_connect()
or die ("no link to the mysql".mysql_error());
mysql_select_db("registration"winking smiley
or die("no link to db".mysql_error());

echo "<br>sno $sno<br>";
echo " year of study is $date<br>";

foreach ($course as $courses) {
echo "<br>course id's in the array are: $courses<br>";
}

foreach ($course as $courses) {
$query = "INSERT INTO course_student VALUES('$sno','$courses','$date'winking smiley";
}

if (isset($_POST['course']))
{
echo "You submitted: " . join(', ', $_POST['course']) . ".\n";
echo "<br>";
}

if ($rows <1) echo "Error!!!!!!!!!!!!!!!!!. The record has not been added to the database.";
else echo "$rows record has been added to the database.";

echo "<br><a href='courseadd.php?'>Add another new course</a>";
echo "<br><a href='readcourse.php?'>Back to course table details</a>";

?>



my output from this is

sno 1
year of study is 2000

course id's in the array are: Array
You submitted: 3, 2, 1, 6.
Error!!!!!!!!!!!!!!!!!. The record has not been added to the database.


so yes the data is arriving
but my foreach loops do not work

what am I doing wrong?????????


Re: Prac
May 23, 2006 02:40PM
MAC

Do we need to follow the requirements to the letter?

ie in para 2 of the tut, the flow of information is "suggested" I see I have already deviated from this flow is this a problem?

I have provided for two forms one for the student to register, and another to select the courses.

One form is to full, looks very bussy,
and it does not allow a student to return and register for extra courses later on.

avatar
Mac
Re: Prac
May 23, 2006 04:12PM
Bit confused as to what the pages are doing. I assume the first page list the course by name on a form, and the student selects the course she wants to register for. This is then send to the next page....

Why do you want a WHERE cname is not null clause? No need for that - you should only have rows with course names in it, so there is thus no need to look for rows without course name.

You're quire correct in suggesting two forms, but for purposes of this prac the flow suggests a once-off registration process, which we are happy with for this assignment. We need to confine your aspirations winking smiley

You're welcome to deviate on the logic within a flow pattern, but once you deviate from the flow you are on your own (and possibly heading for verrrrrrry late nights). Rome wasn't built in one day, neither a registration system in two months especially if you've just started PHP!

The logic should be that:

1. The student is presented with a form with coursenames. She ticks the checkboxes of the courses she wishes to register for, and enters her sno in a text box.
2. The forms sends the course id (cid) (forget about the course name) and her sno to the next page. cid as an array.
3. Here you need to extract the cid from the array. I'mleaving it to you - there is plenty of info on using arrays in forms on the web. For each cid, do a db query to find the course name for that cid (so you can echo what course she has registered for).
4. Then do a simple insert statement, using the sno, the cid and the year (which you define here - why send it with the previous form - she knows she is registering for 2006? )
Re: Prac
May 23, 2006 04:25PM
OK I try again

I want to know how to deal with NULL as I do not find a working solution with google. For this project it is not critical but I use it from time to time in other systems and if I want to work in PHP I need to know things like this

I still have no Idea why my foreach loops do not work?

Re: Prac
May 23, 2006 04:29PM
so we don't need to delve into LAST_INSERT_ID() and mysql_insert_id()?
avatar
Mac
Re: Prac
May 24, 2006 08:29AM
In the example above the course name is already available, so I need to get the cid with something else than mysql_insert_id.
Re: Prac
May 24, 2006 08:52AM
Mac,

Must the student be able to register for more than one course at a time?

From my reading of the prac, we only need to have the student register for one course at a time.

I have my courses listed in a drop down box on my registration form, and once selecting a course, the information gets written to all the relevant tables.



avatar
Mac
Re: Prac
May 24, 2006 09:27AM
The prac - flowchart - assumes one course at a time.
Off course you can use a multiple selection drop-down box, or seperate the student and course info, or use chekboxes for courses - the options are endless. You can do more if you want, but thn you move outside the boundaries of support (we can't support the development of a world-leading registration system, execept if you'e preapred to give us 60% of sales smile
Re: Prac
May 24, 2006 01:01PM
I am still really bttling to insert $sno and $cid into the course_student table - I just get zeroes. So last night I decided on a new approach in the hope that it will clarify problems (if only in my own mind).

I now have a student registration form that works fine and creates a $sno. When that registration form is processed I have created a page that says 'registration successful, click here to register for courses ..'
but I want to echo the student number that has just been created by that student.

How do I get that specific $sno from the previous page?
avatar
Mac
Re: Prac
May 24, 2006 02:42PM
I'll use an example from another application, admin_page.php to add names, and id and tips to a db with a form.

Some syntax always stay the same - you just learn and remember it.


//connection to db always required
mysql_connect();

$add=$_POST['Add']; like you've learned

// from the submit button in the form - to see if form was posted
if(isset($Add)) // standard syntax to see if a variable exists. You can get away without using isset, as in if($add){ }, but this is safer.

//get variables posted with form
$new_name=$_POST['new_name'];
$new_tip=$_POST['new_tip'];

//insert statement, insert variables in exact order as the fields exist in the table, Say they are,and their order is name, id and tip

mysql("db_name", "INSERT INTO table_name VALUES (
'$new_name',
'', //this is an auto increment field - send an empty '' and it will increment by itself
'$new_tip' //note the last does not have a comma - watchout!

)"winking smiley;

// I want to get the id just generated - do a simple select after the insert (i'm not using it in this example, so I put it here as an example)

mysql_select_db("db_name"winking smiley or die( "Unable to select database"winking smiley;
$result = mysql_query( "SELECT * from table_name WHERE name='$new_name'"winking smiley; // WHERE clause to point the query to the exact row whos id I want - I have the $new_name from here above, so I use that to point the query. Of course I can use WHERE name='$new_name AND tip='$tip' as well just in case names can be the same then tip will narrow it down even further.

while($row = mysql_fetch_row($result)) // $result is what I used here above. $row can be any thing - you create the $row variable using the mysql_fect_row function which is built into PHP.
{
$id = $row[1]; using this syntax (there are other ways to do selects) [0] would be the first field, [1] the second field and so on. The order in which the fields exist in the table.

}
// here I echo my results in a html table

echo "<table border=0 align=center><tr><td><b><u>Admin Interface to add a Name and Tip.</u></b><br><br>
<strong>Tip added!</strong><br><br>
<a href='admin_page.php'>Add another</a><BR>or<BR> // this will reload this page, but since Add does not exist in this URL I'm sending, the form below will show
<a href='index.php'>Go to website</a><br><br>";
echo "</td><td></tr><table>";

//should I wanted to use the $id, I can use it however I want to use it. E.g. <a href='page.php?id=$id'>Go do this now</a><br><br>";
}
else //show the form
{
echo "<div align=center><b><u>Admin Interface to add name and tip.</u></b><br><br>
<a href='index.php'>Go to website</a><br><br>
<form method='POST' action=''>"; // leaving action empty - it will go to this same page
echo " <br><b>Add New Name and Tip</b><br><br></div>";
echo "
<table border='0' cellpadding='3' cellspacing='0' style='border-collapse: collapse' bordercolor='#111111' align=center>
<tr>
<td><b>Name</b></td>
<td><input type='text' name='new_name'></td>
</tr><tr>
<td><b>Tip</b></td>
<td><textarea name='new_tip' rows=5 cols=50></textarea></td>
</tr><tr>
<td><b>Action</b></td><td>
<input type='submit' value='Add' name='Add'> //
<input type='reset' value='Reset'></form>
</td>
</tr>
</table><hr size='1'>";

}
?>
Sorry, you do not have permission to post/reply in this forum.