Jump to content

belbin09

Members
  • Content Count

    25
  • Joined

  • Last visited

Community Reputation

0 Neutral

About belbin09

  • Rank
    Member
  1. Removed the PRIMARY KEY from uid now it is registering the student for the same course
  2. Hi, I am almost done my assignment however I am having an issue. I am trying to read enrolment and if the student is already registered for the course then print an error. However I am only able to register one student in one course each before getting the error message, which leads me to believe that it isn't reading the enrolment file properly. I seam to have an issue with this. Any help would be grateful. Thank you // if the course is found and the student is found then check if they have registered if ($found==3) { $equery= "SELECT * FROM enrolment WHERE uid ='$number' AND code = '$course'"; $eresult= mysqli_query($link, $equery); while ($erow = mysqli_fetch_array($eresult)) { if ($erow['code'] == $course && $number == ($erow['uid'])) { } // if ($erow['code'] == $course && $number == ($erow['uid'])) } // while ($erow = mysqli_fetch_array($eresult)) } // if ($found==3) //if the student isn't registered in the course $equery = "INSERT INTO enrolment(uid,code) VALUES ('$number','$course')"; if (mysqli_query($link, $equery)) { echo "New record created successfully"; }// if (mysqli_query($link, $equery)) else { echo "Error: You have already registered for the course"; } // else echo "Error: " .$link->error; mysqli_close ($link);
  3. Thank you. I'll try that. I am only trying to output the results to make sure it works. Later I will be writing the course and student number into a different table and then comparing those results to the users input to make sure they haven't already registered for that course
  4. Hi, I am trying to read in the name and student number that the user entered and compare it to what is in the mysql database. My teacher suggested using found. However I am still not getting anything to echo out. Can someone lead me in the right direction? next.php <?php require 'connect.php'; //linking up to the database $link = mysqli_connect(HOST, USER, PASS, DB) or die (mysqli_connect_error()); //making a variable from the user data $name = mysqli_real_escape_string ($link, $_POST["name"]); $number = mysqli_real_escape_string ($link, $_POST["snumber"]); $course = $_POST["pcourse"]; // select all from table student which show student name and number $squery = "SELECT * FROM students"; $sresult = mysqli_query($link, $squery); while ($srow = mysqli_fetch_array($sresult)) { if ($name == $srow['uid'] && $number == $srow['student']) { if ($found) { echo "$srow[uid] $srow[student]"; } else { echo "Student does not exist"; } } } mysqli_close ($link); ?> <html> <body> <form action="index.php" method="post"> <br> <input type = "submit" value="back" name="back"> </form> </body> </html> This is my index.php that I use as my form <!DOCTYPE html> <html> <body> <h1>Course Selection</h1><br> <form action="next.php" method="post"> Name: <input type="text" name="name" placeholder="Name" required="required" maxlength="50"> <br><br> Student Number: <input type="text" name= "snumber" required="required" maxlength="9"> <br><br> <?php //form require 'connect.php'; $link = mysqli_connect(HOST, USER, PASS, DB) or die(mysqli_connect_error()); echo "Select a course: <select name = \"pcourse\">\n"; $query = "SELECT * FROM course"; $result = mysqli_query($link, $query); while ($row = mysqli_fetch_array($result)) { echo "<option> $row[code=auto:0] $row[name] $row[max]</option><br>"; } mysqli_free_result($results); mysqli_close ($link); echo " </select>\n"; ?> <br><br> <input type = "submit" value="submit" name= "submit"> </form> </body> </html>
  5. My code works fine for the most part, unfortunately though it doesn't seem to be reading the student.txt file. If one student is registered for a course it doesn't let anyone else register for the same course. <?php $students = "student.txt"; // text file for students and student number $reg = "load.txt"; //converting a string into a variable $name = $_POST["name"]; $number = $_POST["snumber"]; $course = $_POST["pcourse"]; //open student file and explode into an array $found = 0; // turning students into an array to read $fileHandle = fopen($students, "r") or die ("Student file does not exist"); while($line=fgets($fileHandle)) { $drop = explode(',',$line); if($name==$drop[0]&&$number == trim($drop[1])) //trip to remove whitespace in student number { $found = 1; break; // break if found } } fclose($fileHandle); $toRep=""; if($found==1) { $fileHandle = fopen("course.txt", 'r'); while($line=fgets($fileHandle)) { $drop = explode('||',$line); if($drop[1]==$course) { $found = 2; // course found if($drop[2]>0) { $found = 3; // couse has seats $toRep= $line; break; } } } } else { echo "Student could not be found."; die(); } if($found ==1) { echo "The course cannot be found"; die(); } if($found==2) { echo "The course is full"; die(); } if($found==3) { $loadHandle = fopen("load.txt",'r'); while($line = fgets($loadHandle)) { $drop = explode(',', $line); if(trim($drop[1])==$course) { echo "You have already registered for the course."; die(); } } } $loadHandle2 = fopen("load.txt", 'a'); fwrite($loadHandle2,"$name,$course\n"); fclose($loadHandle2); fclose($loadHandle); $courseHandle = fopen('course.txt', 'r'); $whole = file_get_contents('./course.txt');// read the whole file $drop = explode('||', $toRep); $count = (int)$drop[2]; $count = $count - 1; // make one seat less $newStr = $drop[0]."||".$drop[1]."||".$count."\n"; // make a new string with new seat count $whole =str_replace($toRep, $newStr, $whole); // replace the old text with new $courseHandle2 = fopen('course.txt', 'w'); fwrite($courseHandle2, $whole); // overwrite the entire file. echo "Sucessfully Enrolled." ?>
  6. Hi my teacher hasn't actually taught us how to use file( ) or FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES. He has only shown us how to use fopen, fgets and explode statements. How exactly would file( ) change my code?
  7. If that line is from enrolled.php its because I was trying to access the course.txt file from index.php
  8. Using text files as per teachers instructions. I am trying to read the load.txt file and then if the student isn't already registered for the course then it will print to load.txt. The problem I am having is that the same course is getting printed to load.txt file even if on index.php I select a different course. Then when I changed students if gives me the echo statement of already being registered in the course. Its printing the student number, and comp-3006. Here is the txt file: English||COMP-1004||4 Web Development||COMP-6002||5 Object-Oriented Java||COMP-1006||3 Networking Essentials||COMP-1035||4 Dynamic Websites AMP||COMP-3006||2 <?php $students = "student.txt"; // text file for students and student number //converting a string into a variable $name = $_POST["name"]; $number = $_POST["snumber"]; $coursedata = "course.txt"; // text file coursedata $cfile = fopen ($coursedata, 'r'); while ($line = fgets ($cfile)) { $drop = explode ('||', trim ($line)); } fclose ($cfile); //open student file and explode into an array $sfile = fopen($students, 'r') or die ("Student file does not exist"); $found = 0; // turning students into an array to read while ($sline = fgets ($sfile)) { $list = explode(",",trim($sline)); //test array against text input if ($name == $list[0] && $number == $list[1]) { $found = 1; //load number and course selected into load.txt; $handle = fopen ('load.txt', 'r'); while ($loadf = fgets ($handle)) { $data = explode (',', trim($loadf)); if ($data[0] == $number && $data[1] != $drop[1]) { } else { echo "$number already registers for $drop[1]"; } // end of else } // end of while 41 fclose ($handle); $handle = fopen('load.txt', 'a'); fwrite($handle, "$number, $drop[1] \n"); fclose ($handle); include 'load.txt'; break; } // end of if 34 } // end of while if (!$found) { include 'index.php'; } fclose($sfile); ?>
  9. Using the text files is part of my assignment. We haven't gotten into MySQL
  10. My working code looked like this: <?php $students = "student.txt"; // text file for students and student number //converting a string into a variable $name = $_POST["name"]; $number = $_POST["snumber"]; $coursedata = "course.txt"; // text file coursedata $cfile = fopen ($coursedata, 'r'); while ($line = fgets ($cfile)) { $drop = explode ('||', trim ($line)); } // end of course while fclose ($cfile); //open student file and explode into an array $sfile = fopen($students, 'r') or die ("Student file does not exist"); $found = 0; while ($sline = fgets ($sfile)) { $list = explode(",",trim ($sline)); //test array against text input if ($name == $list[0] && $number == $list[1]) { $found = 1; //load number and course selected into load.txt; $handle = fopen ('load.txt', 'a'); fwrite($handle, "$number, $drop[1] \n"); fclose($handle); include 'load.txt'; break; } } // end of while if (!$found) { include 'index.php'; } // end of found i fclose($sfile); ?> When the user entered in their name, student name and selected a course it would take them to the code listed about. Then I was able to get the code to write the student number and the course code into a text file called load.php and load that to enrolled.php. So my next step was to get it so that way the same student couldn't register for a course they are already enrolled in. However when I entered in the code below it wouldn't display anything. Anyone have any suggestions? <?php $students = "student.txt"; // text file for students and student number //converting a string into a variable $name = $_POST["name"]; $number = $_POST["snumber"]; $coursedata = "course.txt"; // text file coursedata $cfile = fopen ($coursedata, 'r'); while ($line = fgets ($cfile)) { $drop = explode ('||', trim ($line)); } // end of course while fclose ($cfile); //open student file and explode into an array $sfile = fopen($students, 'r') or die ("Student file does not exist"); $found = 0; while ($sline = fgets ($sfile)) { $list = explode(",",trim ($sline)); //test array against text input if ($name == $list[0] && $number == $list[1]) { $found = 1; //load number and course selected into load.txt; $handle = fopen ('load.txt', 'a'); while ($loadf = fgets ($handle)) { $data = explode (',', $loadf); if ($number == $data[0] && $drop[1] != $data[1]) { fwrite($handle, "$number, $drop[1] \n"); } elseif ($number == $data[0] && $drop[1] == $data[1]) { echo "Student is already registered for this course"; } //end of else if include 'load.txt'; break; } // end of while fclose($handle); } // end of if } // end of while if (!$found) { include 'index.php'; } // end of found i fclose($sfile); ?>
  11. Never mind. Found the problem and fixed it. Thank you requinix for all your help
  12. OK so I added in the course.txt file and everything is printing and displaying, however I keep getting this error and I am not sure what it means Warning: fgets(): 3 is not a valid stream resource in /var/www/htdocs/home/bethany/enrolled.php on line 15 $coursedata = "course.txt"; // text file coursedata $cfile = fopen ($coursedata, 'r'); while ($line = fgets ($cfile)) { $drop = explode ('||', trim ($line)); fclose ($cfile); } //open student file and explode into an array $sfile = fopen($students, 'r') or die ("Student file does not exist"); $found = 0; while ($sline = fgets ($sfile)) { $list = explode(",",trim ($sline)); //test array against text input if ($name == $list[0] && $number == $list[1]) { $found = 1; //load name, number and course selected into load.txt; $handle = fopen ('load.txt', 'a'); fwrite($handle, "$number, $drop[1]\n"); fclose ($handle); include 'load.txt'; break; } } // end of while
  13. Thank you requinix. I do have a question about passing arrays. So in index.php I open the course.txt file and create an array. Now in enrolled.php I want to call that array so that I can put it into the fwrite parameters. How would I do that? index.php $coursedata = "course.txt"; // text file coursedata echo "Select a course: <select name = \"pcourse\">\n"; $cfile = fopen ($coursedata, 'r+') or die ("File does not exist or you do not have permission"); while ($line = fgets ($cfile)) { $drop = explode ('||', $line); echo " <option value =\"$drop[0]\">$drop[0] $drop[1] $drop[2]</option>\n"; } fclose ($cfile); echo " </select>\n"; enrolled.php $handle = fopen ('load.txt', 'a'); fwrite($handle, "$number || $drop\n"); fclose ($handle); break; }
  14. I have contacted my teacher and he has given me a few suggestions I am going to check out.
×
×
  • 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.