Jump to content

belbin09

Members
  • Posts

    25
  • Joined

  • Last visited

Everything posted by belbin09

  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.
  15. So I am trying to write to a text file that I can then display on enrolled.php with the students name, number and courses selected. Then when a different student is selected it writes over the file. Based on the examples I have seen online and what my teacher has shown us I have tried writing the code for this. However I keep getting these errors. Do I have to implode in order to write to the txt file? Sorry this is the first time I have written in php. Warning: fopen(load.txt): failed to open stream: Permission denied in /Applications/XAMPP/xamppfiles/htdocs/assignment1/enrolled.php on line 28 Notice: Undefined variable: cfile in /Applications/XAMPP/xamppfiles/htdocs/assignment1/enrolled.php on line 29 Warning: fwrite() expects at most 3 parameters, 4 given in /Applications/XAMPP/xamppfiles/htdocs/assignment1/enrolled.php on line 29 Warning: fclose() expects parameter 1 to be resource, boolean given in /Applications/XAMPP/xamppfiles/htdocs/assignment1/enrolled.php on line 30 index.php <!DOCTYPE html> <html> <body> <h1>Course Selection</h1><br> <form action="enrolled.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 $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"; } ?> <br><br> <input type = "submit" value="submit" name= "submit"> </form> </body> </html> enrolled.php <?php $students = "student.txt"; // text file for students and student number //converting a string into a variable $name = $_POST["name"]; $number = $_POST["snumber"]; //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, $name, $number, $cfile."\n"); fclose ($handle); $readin = file ('load.txt'); break; } } // end of while if (!$found) { include 'index.php'; } fclose($sfile) ?> course.txt student.txt
  16. I was having someone help me but they are confusing me more than anything. So I do apologize for the questions. I have the code reading the text file perfectly. However as soon as I include the else statement it outputs it 6 times which I know is because its within the while statement. However when I leave it out of the while statement and create a new if statement it will always show the form if ($name != $list[0] && $number != $list[1]) { include 'index.php'; } while ($sline = fgets ($sfile)) { $list = explode(",",trim ($sline)); //test array against text input if ($name == $list[0] && $number == $list[1]) { echo "Welcome $name $number"; } else { include 'index.php'; } } // end of while fclose($sfile) ?>
  17. You did. Where I rewrote my code I wasn't sure if the same answer applied.
  18. I figured out what was wrong with the text name snumber. I forgot the equal sign on the html side of the form. Made the correction from list[1] to list. No errors. However without list[1] meaning the name how will it be compared to the txt file for name and number?
  19. I have a form that asks for the users name and student number. Then in a separate script I going to test it against a txt file that looks like this: Jim Smith, 400424565 Sarah Hillier, 534712479 Jonathan Quinlan, 764134296 Keith Roberts, 123456789 Sarah Hillier, 200343656 Chloe Butler, 678123987 I am new to php so I am not sure where I went wrong. I keep getting warnings: Notice: Undefined index: snumber in /Applications/XAMPP/xamppfiles/htdocs/assignment1/enrolled.php on line 9 Warning: in_array() expects parameter 2 to be array, string given in /Applications/XAMPP/xamppfiles/htdocs/assignment1/enrolled.php on line 21 enrolled.php <?php $students = "student.txt"; // text file for students and student number //converting a string into a variable $name = $_POST["name"]; $number = $_POST["snumber"]; //open student file and explode into an array $sfile = fopen($students, 'r+') or die ("Student file does not exist"); while ($sline = fgets ($sfile)) { $list = explode(",", $sline); //test array against text input if (in_array($name, $list[0])) { if (in_array($number, $list[1])) { echo $name; echo $number; } // end of inner if } //end of outer if } // end of while fclose($sfile) ?>
  20. Thanks phpmillion and Sepodati. I am going to look at my code and clean it up so that way I can look at the suggests you have given me
  21. It does tell me a line. Its line 91 which is: if ($sname_array == $name[0] && $snumber_array == $name[1]) { which you are saying should be something like this: if($sname_array[0] == $name[0] && $snumber_array[0] == $name[1] ) { Although that gives me the same error
  22. Hi, I am very new to PHP. I am trying to program a course registration using a txt file for students and course. The user inputs the name and student number and selects a course from the drop down menu. Currently I have tried to take the name and number submitted and turn it into a variable called $sname and $snumber and then turn that variable into an array so that I can check it against the array of the students name and number. However I keep getting the error that $sname_array is an unidentified variable. Because I am so new at this I am not sure exactly how I go about fixing this. Thank you <!DOCTYPE html> <html> <body> <h1>Course Selection</h1><br> <?php // Course $coursedata = "course.txt"; // text file coursedata $students = "student.txt"; // text file for students and student number if (isset ($_POST['course'])) { $course = ($_POST['course']); $cfile = fopen ($coursedata, 'r+') or die ("File does not exist"); $found = FALSE; while ($line = fgets ($cfile) and ! $found) { $drop = explode ("||", trim ($line)); $found = $course === $drop[0]; } fclose ($coursedata); //close data if ($found) { echo "<p>You have selected $drop[0]</p>\n"; } // end of if ($found) } // end of if (isset) ?> <form action="index.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 //turning student name into a variable if (isset ($_POST['name'])) { $sname = ($_POST['name']); $sname_array = explode (' ', $sname); } // turning student number into a variable if (isset ($_POST['snumber'])) { $snumber = ($_POST['snumber']); $snumber_array = explode (' ', $snumber); } //form // select course 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"; //student file array $sfile = fopen($students, 'r+') or die ("File does not exist"); while ($sline = fgets ($sfile)) { $name = explode (',', $sline); } fclose ($sfile); //checking name entered against student txt if ($sname_array == $name[0] && $snumber_array == $name[1] ) { echo "Correct"; } else { echo "incorrect"; } ?> <br><br> <input type = "submit" value="submit" name= "submit"> </form> </body> </html> The student text file looks like this: Jim Smith, 400424565 Sarah Hillier, 534712479 Jonathan Quinlan, 764134296 Keith Roberts, 123456789 Sarah Hillier, 200343656 Chloe Butler, 678123987 and the course text file looks like this: 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 The last number represent the number of seats still available course.txt student.txt
×
×
  • 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.