Welcome! Log In Create A New Profile

Advanced

the course_man page for the Project

Posted by theSinthesizer 
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
avatar the course_man page for the Project
September 17, 2010 01:04PM
i guyz,

i want to know how i can prevent my program from entering the same entry when i refresh the page?

Here's my code:

the form:

Language: HTML
<html> <head> <title>Add Course</title> </head> <body> <form method=';GET'; action=';course_man.php';> Add Course Name <input name=';course_name'; type=';text';> <br> <br> <input name=';option'; type=';hidden'; value=';add';> <input type=';submit';> </form> </body> </html>



the course_man page (not yet complete - only adding so far):

Language: PHP
<?php $title = "Course Manager"; /*connecting to the database*/ $link_id = mysql_connect("localhost", "lizwe", ""); mysql_select_db("registration"); /*testing connection*/ if ($link_id) echo "Success in connecting!"; else echo "Failed to connect."; /*Doing this so that program doesn';t enter the same record again*/ if (empty ($_GET[';option';])) { $option = "none"; } /*Getting the value of "option" variable from the "add_course" form*/ else $option = $_GET[';option';]; /*selecting appropriate action to take based on the value of "option" variable*/ switch ($option) { case "add": /*getting the value of "course_name" variable from the add_course.html page*/ $course_name = $_GET[';course_name';]; /*inserting value of "course_name" variable into the table "course"*/ $query = "INSERT INTO course VALUES (';$course_name';, ';';)"; $result = mysql_query($query); /*to test if the record was added to the database*/ $rows = mysql_affected_rows(); if ($rows < 1) echo "Error. The record has not been added."; else echo "$rows record has been added"; break; } /*The following table writes the "Add Course" link*/ echo "<table border=';0'; width=';600'; cellspacing=';0'; cellpadding=';0';>"; echo "<tr>"; echo "<td width=';60%';>"; echo "<a href=';add_course.php';>Add Course</a>"; echo "</td>"; echo "</tr>"; echo "</table>"; /*The following code lists the records in the "course" table*/ echo "<table border=';1'; width=';600'; cellspacing=';0'; cellpadding=';0';>"; $result = mysql_query("SELECT * FROM course"); while ($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td width=';60%';>"; echo $row["cname"]; echo "</td>"; echo "<td width=';20%';>"; echo "Delete"; echo "</td>"; echo "<td width=';20%';>"; echo "Edit"; echo "</td>"; echo "</tr>"; } echo "</table>"; /*this line deletes the "option" variable and all memory allocated to it.Without this line, the same value for the "option" variable from "add_course.html" would remain when the page is refreshed. And so the "switch/case ';add'; " would be entered again and so inserting the same record */ unset($_GET[';option';]); ?>

mac forgive me for posting so much code....could be encouraging cheating. smile
avatar
Mac
Re: the course_man page for the Project
September 17, 2010 03:07PM
Why do you want to refresh the page?

Having said that, one should try and cover for all eventualities, thus you can 1stly check if the course name exists, and if it does not, only then do an insert, e.g.

Language: PHP
//mysql select query e.g. "select from course where cname=.';$course_name';". $rows=mysql_num_rows....... if($rows > 0) { //i.e. there is already a row with a course name like this echo "Course already exists <a href......>Try again</a>"; } else { //insert record }
avatar Re: the course_man page for the Project
September 17, 2010 06:41PM
I just thought that it's something that I must have covered in case you do refresh the page...maybe little things like these count as part of the final mark.smile

>>>
The One that owns The Technology rulez the world!
avatar Re: the course_man page for the Project
September 22, 2010 03:43PM
@mac,

so far i've used hidden form fields for passing variables and i'm now going to use the method of passing the variable via the URL.
I've sat the whole of today and yesterday trying to figure out how I can pass one of my variables through a session, but with no luck! I need to pass a variable for the editing and deleting part.

Will I get penalized if I don't use sessionz because I can't figure out how I can use them with my code?

>>>
The One that owns The Technology rulez the world!
avatar
Mac
Re: the course_man page for the Project
September 22, 2010 04:00PM
Are you using the updated php5 code for sessions?
avatar Re: the course_man page for the Project
September 22, 2010 04:13PM
I'm using the type of code that's in the textbook

>>>
The One that owns The Technology rulez the world!
avatar Re: the course_man page for the Project
September 22, 2010 04:27PM
@mac,

i'm using the correct syntax. It's just that i need to pass a variable for the deleting and editing part of the program, and i've tried to figure out how i can use a session but there's no way. I tried but the program doesn't work. And that's why i'm asking if i'll get penalized (since we didn't cover this in the tasks).

>>>
The One that owns The Technology rulez the world!
avatar
Mac
Re: the course_man page for the Project
September 23, 2010 07:18AM
You can pass variables using a form, hidden fields or with the URL.
avatar Re: the course_man page for the Project
September 25, 2010 07:15PM
@mac,

i've identified areas where i'm using nested if statements - not too nested just one level. Will I score higher marks if i use elseif instead of a nested if for instance?

>>>
The One that owns The Technology rulez the world!
avatar
Mac
Re: the course_man page for the Project
September 27, 2010 06:51AM
Well, just keep in mind that you want a page to execure as fast as possible. Having multiple if's requires each one to be evaluated, whereas with an elseif you can shorten execution time in many instances.
avatar Re: the course_man page for the Project
September 27, 2010 04:13PM
yo mac,

please explain what you mean by this (i quote the prac letter):

"Clicking the Edit link will open the course details in a form, where the user can edit the details
and do an update of the course name."

What course detailz are u refering to because cname is the only editable field in course_man.php confused smiley

>>>
The One that owns The Technology rulez the world!
avatar
Mac
Re: the course_man page for the Project
September 28, 2010 07:56AM
The description is generic i.e. I explain what a form does (allow you to edit details), and then tell you what you need to do in this case -> update the course name.

But it probably is confusing but you got it right anyway smiling smiley
avatar Re: the course_man page for the Project
September 30, 2010 10:47AM
hi mac,

i purposefully deleted my database. My course_man.php page was working all along, but now when i try to enter info. from afresh i get problems. The "SELECT * FROM table" statements give me boolean(false) outputs. How can i go about fixing this problem?

>>>
The One that owns The Technology rulez the world!
avatar
Mac
Re: the course_man page for the Project
September 30, 2010 11:38AM
Well, since your code did work before you deleted the db and now does not, the problem could only be that you have not recreated the db correctly?
avatar Re: the course_man page for the Project
September 30, 2010 12:23PM
@mac,

sorry, i meant to say that i deleted the data in my tables. The database is cool.

>>>
The One that owns The Technology rulez the world!
avatar Re: the course_man page for the Project
September 30, 2010 04:17PM
someone in an external forum told me abt mysql_error. turnz out i hadn't selected the database!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! angry smiley

>>>
The One that owns The Technology rulez the world!
avatar
Mac
Re: the course_man page for the Project
October 01, 2010 07:02AM
Which is why you should post your code in order for us to help you out smiling smiley
Sorry, only registered users may post in this forum.

Click here to login