Welcome! Log In Create A New Profile

Advanced

please help - problems with final prac

Posted by Anonymous User 
Announcements Last Post
Announcement : Programming Students at UNISA School of Computing 06/19/2019 02:01PM
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
Anonymous User
please help - problems with final prac
November 03, 2008 01:35PM
Still busy with the final prac, having problems which are going to drive me mad since I cannot find any solution:

STUDENT REGISTRATION
1. the first page provides a form where the student can enter personal data like his name
***
2. the next page
- inserts this data into the student table
- provides a sno - printf("Your student no is: %d\n", mysql_insert_id());
- provides year of registration - $year = date("Y"winking smiley;
***
3. then the student can make his course choice on the next page via drop down list
this is the code:
<form method="post" action="studentreg_course_addrecordfrombrowser.php">

<select name="cid_chosen">

<?php
$coursesql = "SELECT cname
FROM course
ORDER BY cname";
$result = mysql_query($coursesql)
or die("Invalid query : " . mysql_error());
while ($row = mysql_fetch_array($result)) {
echo '<option value="' . $row['cid'] . '">' . $row['cname'] . '</option>' . "\r\n";
}
?>

</select>

<br>
<br>

<input name="Submit registration" type='submit' id="Submit registration" value="Submit course">
</form>
***
By the way: whenever trying to correct within the lines of <select>....</select>, Dreamweaver is closing indicating that it has a problem. so I have to take PHP designer to correct the file. Does someone knows what this could be?
4. here is the problem - the last page should now
a) capture the cid of this course choice and display it
$cid_chosen=$_POST['cid_chosen'];
echo $cid_chosen;

PROBLEM
the cid of the chosen course is not shown
???

b) insert the sno, cid and year into the course_student table
<?php

$csquery = "INSERT INTO course_student
(sno,
cid,
year)
VALUES
('" . $_POST['sno'] . "',
'" . $_POST['cid'] . "',
'" . $_POST['year'] . "')";
$result = mysql_query($csquery);
$rows = mysql_affected_rows();
?>

PROBLEM
a record is inserted but the values are all null:
sno = 0, cid = 0, year = 0
???


*******************************************************************************

EDITING a course


1. First I provide a table listing all courses (names) and links to add, edit, delete
- adding works fine -
here is the link for editing:
<a href="course_edit.php?action=edit&id=<?php
echo $row['cid']; ?>">EDIT</a>
2. On the next page
a) I make a query to provide the name of the chosen course
$cnamesql = "SELECT * FROM course
WHERE cid = '" . $_GET['id'] . "'";
$result = mysql_query($cnamesql)
or die("Invalid query: " . mysql_error());
$row = mysql_fetch_array($result);
$cname = $row['cname'];
b) then I have a small form where the coursename can be edited:
<form action="commit_edit.php" method="post">
<br>
<br>
<input type="text" name="cname" value="<?php echo $cname ?>">
<br>
<br>
<input type="submit" name="SUBMIT" value="Edit course">
</form>

3. PROBLEM - On the confirmation page I get the message that the course was edited - but nothing is edited.
I can reload in my browser - I refresh in phpMyAdmin - nothing


this is my query:
$sql = "UPDATE course SET
cname = '" . $_POST['cname'] . "',
WHERE cid = '" . $_GET['id'] . "'";
$result = mysql_query($sql);

PROBLEM - In the displayed message the cid should be mentioned but only 0 is here:
The course was edited: 0


this is my code:
<?php
printf("The course was edited: %d\n", mysql_insert_id());
?>

******************************************************************************


DELETING a course

1. First I provide a table listing all courses (names) and links to add, edit, delete
- adding works fine -
here is the link for deleting:
<a href="course_delete.php?type=course&id=<?php
echo $row['cid']?>">DELETE</a>

2. PROBLEM - On the next page I get the confirmation that the record is deleted but it is still there, in the browser and in phpMyAdmin????

a) first I select the appropriate single record, the ID
$id=$_GET['id'];
b) then I make the delete query
$query="DELETE FROM course WHERE id='$cid'";
mysql_query($query);
echo "Record deleted";


Please provide me with some hints how to come out of this mess - I have checked many tutorials now but I cannot find the solution - perhaps I am so confused now that I do not see it????

Thanks in advance for any kind of help!

Iris
Re: please help - problems with final prac
November 03, 2008 03:21PM
$sql = "UPDATE course SET
cname = '" . $_POST['cname'] . "',
WHERE cid = '" . $_GET['id'] . "'";
$result = mysql_query($sql);[/color]


IRIS Concerning the above code - I think you I either use $_POST or $_GET ,implying that if you use both one is invalid - that's what I think ,because the assumption is that data is passed by one form at a time ,which can only be done one way or the other.
avatar
Mac
Re: please help - problems with final prac
November 03, 2008 06:33PM
Dreamweaver closing is a Dreamweaver problem. Reinstall?

Since you only provide half the code, the error could be anywhere....

Work step by step to trace the error. A longer route, but with experience you will catch one. If it will make you feel better, I have been struggling with a something for two days now AND I CANNOT SEE THE ERROR!!!!!!!!!!!!!!!!!!!!! But get used to it.

Have a form with only one text field. Send it to the next page, catch it with a $_POST (as defined with the <form> tag. Echo the variable cached. If it is there, insert/update it into the db. You can always use the EMPTY icon in phpmyadmin to start over (which cleans the table in one shot. Also, it is much easier to refresh phpmyadmin page - right click next to the table as opposed to refreshing the browser which means you have to select the db, table, browse etc everytime). Then add a next form field, and so on.

If you echo the variable on the next page (as you do) and it shows correctly, then you know the problem must lie in the SQL statement.If you can't find it there, then perhaps it lies with the user permissions to update the db? See the phpmyadmin front page - I sometimes find it is better to add a user with rights here, and reload the privileges from the link provided on the front page of phpmyadmin than going to the mysql database and doing it from there....

But clearly, from the little code you have posted, 72158468 is spot on. If you send variables with a link, then you catch it with $_GET. A form-send variable either with $_POST or $_GET, depending on what you specified in the <form> tag. You can't change the method on the next page!!!!
avatar
Mac
Re: please help - problems with final prac
November 03, 2008 06:42PM
And while I am it - it is great that you are using code like this:
$cnamesql = "SELECT * FROM course WHERE cid = '" . $_GET['id'] . "'";

But perhaps leave code like this for later if it gets late night. See the simple code below, which minimizes parse errors etc - you can upgrade your code later by adding fancy

'" . $_GET['id'] . "'";


and so forth - you are just increasing the risk for parse errors with all the ' and " and .


$mail=mysql("registration", "SELECT id, memno, club, name, surname, flag FROM players order by surname"winking smiley;
$nummail = mysql_numrows($mail);
$i = 0;
while ($i<$nummail)
{
$id= mysql_result($mail,$i,'id');
$memno= mysql_result($mail,$i,'memno');
$clubname= mysql_result($mail,$i,'club');
$name= mysql_result($mail,$i,'name');
$sname= mysql_result($mail,$i,'surname');
$flag= mysql_result($mail,$i,'flag');
$length=strlen($id);

echo "$id, $memno,$clubname .... etc....";
$i++;
}


The use of $mail here probably does not make much sense here - but I chose to name it like this for it for my own reasons. And you can name it whatever you want to.
Re: please help - problems with final prac
November 03, 2008 09:56PM
Hi Iris,

I quickly scanned through your code, regarding your DELETE problem, i think you might be mixing up your variables in your query:

this is your code:
a)
$id=$_GET['id'];
b) then I make the delete query
$query="DELETE FROM course WHERE id='$cid'";
mysql_query($query);
echo "Record deleted";


SHouldn't it be : $query="DELETE FROM course WHERE cid = $id";
?
since you are comparing the cid (field name in the course table) with your newly created $id variable?
Re: please help - problems with final prac
November 04, 2008 07:07AM
If I'm reading correctly:

You're passing the following variable to the page which is to insert the data into course_student table

$cid_chosen=$_POST['cid_chosen'];

And when trying to write it into the database you are using:
VALUES 
('" . $_POST['sno'] . "', 
'" . $_POST['cid'] . "',

Shouldn't it be $_POST['cid_chosen'] instead of $_POST['cid']?
avatar
Mac
Re: please help - problems with final prac
November 04, 2008 08:32AM
Yes, the more I look at this code the more I see numerous errors.

So my previous post still stands - start over, do line by line and test.

I would do this:

$mail=mysql("registration", "SELECT cid, cname FROM course order by cname"winking smiley;
$nummail = mysql_numrows($mail);
$i = 0;
while ($i<$nummail)
{
$cid= mysql_result($mail,$i,'cid');
$cname= mysql_result($mail,$i,cname);

echo "$cid $cname";
echo "<option value='$cid'>$cname</option>";
$i++;
}

Once I am sure the cid and cname is echoed correctly, then comment out
//echo "$cid $cname";
Comment it out as opposed to deleting it because you may well come back later to use it again for error checking

Then I would move to the next page, and first echo the $cid and $cname (and any other variables) on top of the page before I start to use it. If you do it this way you will work faster. Later as you gain experience you will obviously echo less for errors.
Anonymous User
Re: please help - problems with final prac
November 05, 2008 05:43PM
Kisses on your feet!!!!

Edit & Delete is now working - the rest will hopefully be solved too.

Thanks to all of you!

Iris

This just shows me that your head must be free to solve problems especially when programming...
Re: please help - problems with final prac
November 08, 2008 12:37AM
Hey guys... abit confusion on my side. Students can register, Admins can add, edit, delete students or courses. Lecturers can generate course profiles.

There is no database table for users, only students, and if its internally then would i need a login and authorization screen? Thanks for your help smiling smiley
avatar
Mac
Re: please help - problems with final prac
November 08, 2008 07:40PM
No need for authorization, i.e. users in this prac...
Re: please help - problems with final prac
November 09, 2008 01:08AM
so maybe just a drop down list of what you are while you doing a task? in the paper it specifies 3 types of users; students, admins, and lecturers. So how would i go about using them correctly? Thanks for your help mac smiling smiley
avatar
Mac
Re: please help - problems with final prac
November 09, 2008 02:58PM
You will still have those 3 users, but no authentication required. When I click to register for a course, the assumption is that I'm doing it as a student etc. If you want to add extra information, you are welcome. I certainly don't want to limit you, and is excited if you are already thinking of making it more user-friendly. It shows the PHP bug has bitten smiling smiley
Sorry, only registered users may post in this forum.

Click here to login