Jump to content

Johnnyboy123

Members
  • Posts

    65
  • Joined

  • Last visited

    Never

Everything posted by Johnnyboy123

  1. Ah ok cool thanks seems to work fine. I'm trying to get the cid from the course the student registers for now. Theres a course table with the availible courses and their cid's. I want the cid of the cname (course name) which the students selects to register for to be sent to the course_student table along with the sno. This sno and cid will represent the student and what course he/she is studying for in the course_student table. I'm trying something like this: $cname = mysql_real_escape_string($_POST['cname']); $sql3 = "SELECT cid FROM course WHERE $cname = 'cname' INSERT INTO course_student (cid)"; mysql_query($sql3) or die('Error:' . mysql_error()); Is this on the right path?
  2. Ah thanks, can you use that in a short example for me so I can get the idea please?
  3. Heres my full code for the registration of the student. The entered info is successfully updated in the table. <?php if (isset($_POST['submit'])) { // forms inputs set to variables $sname = mysql_real_escape_string($_POST['sname']); $init = mysql_real_escape_string($_POST['init']); $fname = mysql_real_escape_string($_POST['fname']); $title = mysql_real_escape_string($_POST['title']); $msname = mysql_real_escape_string($_POST['msname']); $dob = mysql_real_escape_string($_POST['dob']); $sex = mysql_real_escape_string($_POST['sex']); $lang = mysql_real_escape_string($_POST['lang']); $idno = mysql_real_escape_string($_POST['idno']); $telh = mysql_real_escape_string($_POST['telh']); $telw = mysql_real_escape_string($_POST['telw']); $cell = mysql_real_escape_string($_POST['cel']); $fax = mysql_real_escape_string($_POST['fax']); $email = mysql_real_escape_string($_POST['email']); $address = mysql_real_escape_string($_POST['address']); $errorstring =""; //default value of error string if (!$sname) $errorstring = $errorstring . "<b>Surname:" .blankfield() ; if (!$fname) $errorstring = $errorstring . "<b>First name:".blankfield(); if (!$title) $errorstring = $errorstring . "<b>title:" .blankfield(); if (!is_numeric($dob)) $errorstring = $errorstring . "<b>Date of birth:".nrfield(); if (!$sex) $errorstring = $errorstring . "<b>sex:" .blankfield(); if (!$idno) $errorstring = $errorstring . "<b>id number:" .nrfield(); if (!$email) $errorstring = $errorstring . "<b>email address:".blankfield(); if (!$address) $errorstring = $errorstring . "<b>address:".blankfield(); if ($errorstring!="") echo "<h1> Please click <a href='student_reg.php'>here</a> to return to the form page and fill out the following fields: </h1><br>$errorstring"; else { // query $sql = "INSERT INTO student (sno, sname, init, fname, title, msname, dob, sex, lang, idno, telh, telw, cel, fax, email, address ) VALUES ('', '$sname', '$init', '$fname', '$title', '$msname', '$dob', '$sex','$lang', '$idno', '$telh', '$telw', '$$cell', '$fax', '$email', '$address')"; mysql_query($sql) or die('Error:' . mysql_error()); echo "You have successfully registerd. Click <a href='index.php'> here</a> to return to the home page"; } ?> The sno field is auto_increment and i cannot change the db structure. The sno updates fine in the student table but how do I edit this so that the sno field, which is generated by the new student when he registers, gets entered in the sno field of my other table course_student aswell? Anyone? Thanks in advance.
  4. As painfully simple as this may be, I seem to be doing something wrong as always. New to php and decided to start using functions. I'm trying to add functions from an external functions.php file to my other pages and then call the functions where necessary. How should I go about it? I tried something like this: Functions.php: <?php function field() { echo 'text heherh'; } ?> and then when calling it to when needed for example: include 'includes/functions.php'; // at the top of the page <?php // and when trying to call the function on a specific line if (!$sname) $errorstring = $errorstring . "<b>Surname:" . field() ; //field being the function name ?> I know this is very simple but I have never used functions before.. tried google, didn't help much and I'm pressed for time so decided to ask here. Thanks in advance
  5. So I have 3 tables: student, course_student and course. The student table is a student's registration info and course is the different courses the student can register for. The course_student table should contain the id fields of the other 2 which I should then use to link the tables and for example display all student's registered for current courses. Student table has an id field: sno and course table has : cid. Course_student contains both. How do I go about linking the tables so the sno id field of the student table and the cid from course updates with those in the course_table?
  6. Ah nevermind figured out how to restrict the number of characters limit and to numbers. But say for instance I have a field such as home tel nr which requires the nr code + nr. So it will have to have characters + numbers e.g (+27 or whatever) for code and then 9405690 for the home nr. How do I restrict it to that? I currently just went with if (!is_numeric($dob)) for numbers.
  7. Ah ok makes sense. Will keep that in mind. How do I go about setting restrictions on the data that has to be entered. I already made it so the field can't be left blank. Say for instance, one of the number fields i.e date of birth, how do I restrict that field so the user can only submit numbers and only a limited amount?
  8. Lol omw can't believe I didn't see that simple solution thanks drummin, must be getting tired haha. Thanks fugix will take a look at it, rather learn the recommended practices from the start than picking up bad habits
  9. Thanks alot. Queries working fine, not getting any errors on the page anymore. Although when I submit my newly typed info to edit it, I get an "unknow column in where clause" error. What causes this? This is my full code now: <?php if (!isset($_POST['submit'])) { $q = "SELECT * FROM course WHERE cid = $_GET[cid]"; $result = mysql_query($q); $person = mysql_fetch_array($result); $cname = mysql_real_escape_string($_GET['cname']); $q2 = "SELECT * FROM student WHERE cname = '$cname'"; $result2 = mysql_query($q2); $person2 = mysql_fetch_array($result2); } ?> <h1> You are editing a course </h1> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <p>Course Name:</p> <INPUT TYPE = "text" name = "cname"value= "<?php echo $person['cname']; ?>" /> <br> <input type= "hidden" name="cid" value="<?php echo $_GET['cid']; ?>" /> <INPUT TYPE = "submit" name="submit" VALUE = "Update"/> </form> <?php if(isset($_POST['submit'])) { $u = "UPDATE course SET `cname` = '$_POST[cname]' WHERE cid = $_POST[cid]"; mysql_query($u) or die(mysql_error()); $u2 = "UPDATE student SET `cname` = '$_POST[cname]' WHERE cname = $_POST[cname]"; mysql_query($u2) or die(mysql_error()); echo "course has been modified!"; } ?>
  10. Think I made a post about this earlier but doesn't show so dont think it posted successfully but if it did, my bad haha. Basicly I have a form which sends info to my database table after submitted. I added some form validation so when the user didn't enter a required field, text appears stating that the field needs to be entered. However, whether the field is entered or not, if the user submits the info still goes to the database. How do I fix it so that it only sends the info to the database once the required fields are entered? Do I go about it with an if statement? Heres my code up until now: <?php if (isset($_POST['submit'])) { // forms inputs set to variables $cname = mysql_real_escape_string($_POST['cname']); $sname = mysql_real_escape_string($_POST['sname']); $init = mysql_real_escape_string($_POST['init']); $fname = mysql_real_escape_string($_POST['fname']); $title = mysql_real_escape_string($_POST['title']); $msname = mysql_real_escape_string($_POST['msname']); $dob = mysql_real_escape_string($_POST['dob']); $sex = mysql_real_escape_string($_POST['sex']); $lang = mysql_real_escape_string($_POST['lang']); $idno = mysql_real_escape_string($_POST['idno']); $telh = mysql_real_escape_string($_POST['telh']); $telw = mysql_real_escape_string($_POST['telw']); $cell = mysql_real_escape_string($_POST['cel']); $fax = mysql_real_escape_string($_POST['fax']); $email = mysql_real_escape_string($_POST['email']); $address = mysql_real_escape_string($_POST['address']); $errorstring =""; //default value of error string if (!$sname) $errorstring = $errorstring . "Surname<br>"; if (!$fname) $errorstring = $errorstring . "First name<br>"; if (!$title) $errorstring = $errorstring . "title<br>"; if (!$dob) $errorstring = $errorstring . "date of birth<br>"; if (!$sex) $errorstring = $errorstring . "sex<br>"; if (!$idno) $errorstring = $errorstring . "id number<br>"; if (!$email) $errorstring = $errorstring . "email address<br>"; if (!$address) $errorstring = $errorstring . "address<br>"; if ($errorstring!="") echo "Please fill out the following fields:<br>$errorstring"; else { //run code die("success!"); } // query $sql = "INSERT INTO student (sno, cname, sname, init, fname, title, msname, dob, sex, lang, idno, telh, telw, cel, fax, email, address ) VALUES ('', '$cname', '$sname', '$init', '$fname', '$title', '$msname', '$dob', '$sex','$lang', '$idno', '$telh', '$telw', '$$cell', '$fax', '$email', '$address')"; mysql_query($sql) or die('Error:' . mysql_error()); } mysql_close($link); ?> It's a project, first time I'm attempting form validation so this is rather new for me. With that being said, please feel free to give any tips and criticism as this is a learning curve for me. Also I'm probably going to add some security later for sql injection etc. so if anyone has some tips on that it would be great. Almost done with that damn project, thanks in advance
  11. you mean like so $q2 = "SELECT * FROM student WHERE cname = $_GET['cname']"; ? when doing that I get the following error: Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\Program Files\EasyPHP-5.3.3\www\Project\editcourse.php on line 31 Could it maybe be that the cname info isn't going through correctly? in the browser it displays as : http://127.0.0.1/project/editcourse.php?cid=8&cname=Netso Because the first set for cid seems fine but the error comes in the cname part, and they are both done the same? <?php $q = "SELECT * FROM course WHERE cid = $_GET[cid]"; $result = mysql_query($q); $person = mysql_fetch_array($result); $q2 = "SELECT * FROM student WHERE cname = $_GET[cname]"; $result2 = mysql_query($q2); $person2 = mysql_fetch_array($result2); ?> ? I'm lost
  12. Seems to be working shows:php?cid=8&cname=Netso in my browser with 8 being cid and netso being cname. Think thats correct. What I'm trying to do now is use those 2 fields transferred to the next page to update 2 different tables 1 using cid and 1 using cname. Im getting this error tho, what does it mean? Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\Program Files\EasyPHP-5.3.3\www\Project\editcourse.php on line 33 Heres my code: <?php if (!isset($_POST['submit'])) { $q = "SELECT * FROM course WHERE cid = $_GET[cid]"; $result = mysql_query($q); $person = mysql_fetch_array($result); $q2 = "SELECT * FROM student WHERE cname = $_GET[cname]"; $result2 = mysql_query($q2); $person2 = mysql_fetch_array($result2); } ?> <h1> You are editing a course </h1> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <p>Course Name:</p> <INPUT TYPE = "text" name = "cname"value= "<?php echo $person['cname']; ?>" /> <br> <input type= "hidden" name="cid" value="<?php echo $_GET['cid']; ?>" /> <INPUT TYPE = "submit" name="submit" VALUE = "Update"/> </form> <?php if(isset($_POST['submit'])) { $u = "UPDATE course SET `cname` = '$_POST[cname]' WHERE cid = $_POST[cid]"; mysql_query($u) or die(mysql_error()); $u2 = "UPDATE student SET `cname` = '$_POST[cname]' WHERE cname = $_POST[cname]"; mysql_query($u2) or die(mysql_error()); echo "course has been modified!"; } ?> Is this the correct way to go about it? Thanks for all the help so far
  13. Ah that's interresting.. thanks DevilsAdvocate. However, we we're provided a specific database structure which we can't change, either way I'd like to figure out the php?cid problem I'm having as I dont really know how to correctly type the code and why I'm getting the errors and would like to know for future reference. Thanks though, didn't think of your method, I'll look into it.
  14. <?php echo "<a href=\"editcourse.php?cid=$person['cid']&cname=$person['cname']\" > edit </a>"; ?> Is this right? Error: Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\Program Files\EasyPHP-5.3.3\www\Project\course_man.php on line 92 Heres my full code: <?php //query database $query=mysql_query("SELECT * FROM course "); while ($person = mysql_fetch_array($query)): echo "<a href=\"editcourse.php?cid=$person['cid']&cname=$person['cname']\" > edit </a>"; $cname=$person['cname']; //Display different courses echo "<table> <tr> <td> $cname </td> </tr> </table> "; endwhile; ?> <p> <a href="selectdelcourse.php"> Delete </a> <hr> <FORM action = "addcourse.php" method ="post"> <p>Add course:</p> <INPUT TYPE = "text" name="course"/> <INPUT TYPE = "Submit" name="submit" VALUE = "Submit"/> </FORM> </body> </html> ?> Dunno if that will help haha
  15. So if I try and send a row "cid" and "cname" How will I type the code? Played around a bit but keep getting this in the browser :.php?cid=&cname=9Internet ( 9 being the cid and internet being the course) Here is an example of my code ( I know it's way off but it's the last thing I tried when playing around with it. echo "<a href=\"editcourse.php?cid=&cname=" . $person['cid'] . $person['cname'] ."\" > edit </a>"; How will I go about typing this code? Something like : echo "<a href=\"editcourse.php?cid="$person['cid']"&cname="$person['cname']" " . "\" > edit </a>"; ?
  16. Very simple question. I have a table with courses and a table with students registered for those courses. I have a page where I can change the course name. What I want to do is, when changing the course name in the course table, I want all students in the students table who are already registered for that course to have that course name updated as well in the student table. I'm currently using a field cid to update the courses in the course table however, the student table doesn't have a cid field rather a cname ( course name) which I want to use to update the fields in the student table. Is it possible to use <a href=\"editcourse.php?cid=" for more than one field of info? i.e transfer the cid + cname and so you can use $_GET[cid]"; and $_GET[cname]"; ? Something like this maybe ( dont know how the code will go) "editcourse.php?cid=&cname=" ? Do I have the right idea for how to go about this?
  17. Thanks guys, learning some helpful tips. Got my pages sorting and it's displaying fine.
  18. So I have 3 tables, student, student_course and course. I'm only using student and course atm, probably should look into student_course haha well those are the tables provided to us for the project I'm working on. I'm trying to list all the courses from the course table and then when clicking on a course I want to display all the students who registered for this course( which would probably be the student table) Here is my code for displaying the courses: <?php $display_sql ="SELECT cname FROM course ORDER BY cid DESC"; $display_query = mysql_query($display_sql) or die(mysql_error()); $rsDisplay = mysql_fetch_assoc($display_query); ?> <html> <head> </head> <body> <p> Click course to display all students registered for that course</p> <?php do { ?> <p><a href="test.php?cname=<?php echo $rsDisplay['cid']; ?>"> Course: <?php echo $rsDisplay['cname']; ?> </a> </p> <?php } while ($rsDisplay = mysql_fetch_assoc($display_query)) ?> And here is my code for displaying the students from the student table registered for the specific course you click on: <?php $q = "SELECT * FROM student WHERE cname = $_GET[cname]"; $confirm_query = mysql_query($q); $rsconfirm = mysql_fetch_assoc($confirm_query); ?> <html> <head> </head> <body> <p> Displaying all student </p> <p> First Name <?php echo $rsconfirm['sname']; ?> </p> <p> Surname: <?php echo $rsconfirm['fname']; ?> </p> I'm very new to php. Im using "test.php?cname=" to transfer the cname (course name) info from my course table to the next page where I use $_GET[cname]";. cname isn't a primary key in my table, rather just a row. Can it be done like that? Or should you just use primary keys? I managed to display the courses but when clicking it, it wont display the registered users and gives me an error: Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\Program Files\EasyPHP-5.3.3\www\Project\test.php on line 28 What does that mean? Line 28 is this part : $q = "SELECT * FROM student WHERE cname = $_GET[cname]"; //26 $confirm_query = mysql_query($q); //27 $rsconfirm = mysql_fetch_assoc($confirm_query); //28 Any criticism welcome. When coding I'm still trying to figure out what it is I'm doing nevermind getting it to work haha. Thanks in advance
  19. Got it sorted Thanks for all the help guys.
  20. @ PFMaBiSmAd Thank you. Very informative. Haha yes I spotted that "?id=value" after reading your first post which made me look at my code and think about what I'm doing. Busy trying to correct this error now when submitting new info "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE ID = 28' at line 19" However, it now shows the previously entered values without any errors when I select edit. Which is good progress thanks
  21. @ GuiltyGear Thank you for your reply. I made the changes which you suggested. I am still however, getting the same errors. Sorry for not pointing out which lines the errors we're. Here are the errors: Notice: Undefined index: sno in C:\Program Files\EasyPHP-5.3.3\www\Project\editstudent.php on line 27 Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\Program Files\EasyPHP-5.3.3\www\Project\editstudent.php on line 29 line 27 and 29 would refer to these lines of code: <?php if (!isset($_POST['sno'])) //25 { //26 $q = "SELECT * FROM student WHERE sno = " . $_GET['sno']; //27 $result = mysql_query($q); //28 $person = mysql_fetch_array($result); //29 } //30 ?> Thank you for your help.
  22. Been trying to sort this out for a while now, had an earlier post on this as well yet haven't solved it yet. Code has changed a bit since then but still giving me trouble and I have a due date for this project is is sneaking up on me. I am trying edit previously entered information from a table which contains a student's registration information into a form. The previously entered info wont display and I am having numerous errors Here is my code <?php if (!isset($_POST['sno'])) { $q = "SELECT * FROM student WHERE sno = " . $_GET['sno']; $result = mysql_query($q); $person = mysql_fetch_array($result); } ?> <h1> You are editing a student </h1> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <p>Course name:</p> <select name="input1"> <?php $q = "SELECT cname FROM course "; $result = mysql_query($q); WHILE ($course = mysql_fetch_array($result)) { echo "<OPTION>{$course['cname']}</option>"; } echo " </SELECT>" ?> <br> <p>Surname:</p> <INPUT TYPE = "text" name = "input2"value= " <?php echo $person['sname']; ?>" /> <br> <p>Initials:</p> <INPUT TYPE = "text" name="input3"value="<?php echo $person['init']; ?>" /> <br> <p>Full First Name:</p> <INPUT TYPE = "text" name="input4"value="<?php echo $person['fname']; ?>" /> <br> <p>Title:</p> <INPUT TYPE = "text" name="input5"value="<?php echo $person['title']; ?>" /> <br> <p>Maiden or previous surname:</p> <INPUT TYPE = "text" name="input6"value="<?php echo $person['msname']; ?>" /> <br> <p>Date of Birth:</p> <INPUT TYPE = "text" name="input7"value="<?php echo $person['dob']; ?>" /> <br> <p>Gender:</p> Male <input type="radio" name="input8" value="m" /> Female <input type="radio" name="input8" value="f" /><br/> <br> <p>Language</p> <select name="input9"> <option >English</option> <option >Afrikaans</option> </select> <br> <p>Identity Number:</p> <INPUT TYPE = "text" name="input10"value="<?php echo $person['id']; ?>" /> <br> <p>Home Telephone Code + Number:</p> <INPUT TYPE = "text" name="input11"value="<?php echo $person['telh']; ?>" /> <br> <p>Work Telephone Code + Number:</p> <INPUT TYPE = "text" name="input12"value="<?php echo $person['telw']; ?>" /> <br> <p>Cell Phone Number:</p> <INPUT TYPE = "text" name="input13"value="<?php echo $person['cel']; ?>" /> <br> <p>Fax Code + Number:</p> <INPUT TYPE = "text" name="input14"value="<?php echo $person['fax']; ?>" /> <br> <p>E-mail Address:</p> <INPUT TYPE = "text" name="input15"value="<?php echo $person['email']; ?>" /> <br> <p>Postal Address of student:</p> <textarea name="input16" COLS=50 ROWS=5 /><?php echo $person['address']; ?> </textarea> <br> <input type= "hidden" name="sno" value="<?php echo $_GET['sno']; ?>" /> <INPUT TYPE = "Submit" name="submit" VALUE = "Update"/> </form> <?php if(isset($_POST['submit'])) { $u = "UPDATE student SET `cname` = '$_POST[input1]', `sname` = '$_POST[input2]', `init` = '$_POST[input3]', `fname` = '$_POST[input4]', `title` = '$_POST[input5]', `msname` = '$_POST[input6]', `dob` = '$_POST[input7]', `sex` = '$_POST[input8]', `lang` = '$_POST[input9]', `idno` ='$_POST[input10]', `telh` = '$_POST[input11]', `telw` = '$_POST[input12]', `cel` = '$_POST[input13]', `fax` = '$_POST[input14]', `email` = '$_POST[input15]', `address` = '$_POST[input16]' WHERE ID = $_POST[sno]"; mysql_query($u) or die(mysql_error()); echo "User has been modified!"; header("Location: index.php"); } ?> The errors are ad followed. When I open the form I get these erros and the fields are blank: Notice: Undefined index: sno in C:\Program Files\EasyPHP-5.3.3\www\Project\editstudent.php on line 27 Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\Program Files\EasyPHP-5.3.3\www\Project\editstudent.php on line 29 When i enter new info and submit it I get these errors: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' Notice: Undefined index: sno in C:\Program Files\EasyPHP-5.3.' at line 19 And my fields contain random code e.g "<br /> <b>Notice</b>: Undefined variable: person in <b>C:\Program Files\EasyPHP-5.3.3\www\Project\editstudent.php</b> on line <b>97</b><br />" Also I dont know if this would help at all but here's the previous page which contains the code for displaying the students information and allowing to edit it, which links to the edit page <?php while ($rows = mysql_fetch_array($query)): echo "<a href=\"editstudent.php?id=" . $rows['sno'] ."\" > edit </a>"; $sno=$rows['sno']; $cname=$rows['cname']; $sname=$rows['sname']; $fname=$rows['fname']; echo " <table border=1px> <tr> <td>$sno</td> <td>$cname</td> <td>$sname</td> <td>$fname</td> </tr> </table>" ; endwhile; ?> Any help would be greatly appreciated. Thanks in advance for your time and effort.
  23. So I have 2 tables, a student table and a course table. When I add a new course to my course table it should be available to students to sign up for which goes into the student table when they do. However now I have to create a page which displays the different courses (course table) and when you click on a course it should display all the users registered for that specific course. Also when a course gets deleted all the student registered for that course should be deleted. So deleting a course in the course table should delete student in the student table.. I have already created a page which display the different course information and I am familiar with creating pages that allows you to delete table info from the page. However, I am lost on how to display the students when clicking on the course and how to link the 2 tables so when deleting a course it deletes the students aswell. Where should I begin with this and how should I go about it? This is my code up until now for displaying the different courses: <?php $query=mysql_query("SELECT * FROM course "); while ($rows = mysql_fetch_array($query)): $cname=$rows['cname']; $cid=$rows['cid']; echo "<table> <tr> <td> $cname </td> <td> $cid </td> </tr> </table> "; endwhile;
  24. Name says it all. I have a form and 2 tables that are going to have to be linked with each other. Although what I'm trying to do now is very basic and I'm struggling haha Though this is my first php project Basicly I got a form with a drop down menu, which I want to display the contents of my table (course) which the user then selects from and submits to another table (student) Here is my code for this part <?php <?php $q = "SELECT cname FROM course "; $result = mysql_query($q); $course = mysql_fetch_array($result); ?> <div id="apdiv3"> <FORM action = "demo.php" method ="post"> <p>Course name:</p> <select name="cname"> <OPTION> <?php echo $course['cname']; ?> </option> </SELECT> ?> It only displays 1 item from my course table. What am I doing wrong? And also please explain so I can learn from my mistakes
×
×
  • 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.