Johnnyboy123 Posted May 4, 2011 Share Posted May 4, 2011 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? Quote Link to comment Share on other sites More sharing options...
fugix Posted May 4, 2011 Share Posted May 4, 2011 the url that you have shown is exactly correct..you can transfer as many values as you'd like up to 1000 chars in the url Quote Link to comment Share on other sites More sharing options...
Johnnyboy123 Posted May 4, 2011 Author Share Posted May 4, 2011 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>"; ? Quote Link to comment Share on other sites More sharing options...
fugix Posted May 4, 2011 Share Posted May 4, 2011 try href=\"editcourse.php?cid=$person['cid']&cname=$person['cname']\" > Quote Link to comment Share on other sites More sharing options...
Johnnyboy123 Posted May 4, 2011 Author Share Posted May 4, 2011 <?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 Quote Link to comment Share on other sites More sharing options...
TOA Posted May 4, 2011 Share Posted May 4, 2011 Look into normalization. If you use the cid as the Foreign key, you won't need to do all this. Example: Course Table Course_ID - pk Course_Name Student Table Student_ID - pk Student_Name Enroll Table Enroll_ID - pk Student_ID - fk Course_ID - fk That way, it doesn't matter if you change the name, the id is the unique identifier Just my 2 cents Quote Link to comment Share on other sites More sharing options...
fugix Posted May 4, 2011 Share Posted May 4, 2011 That error is because your code isn't concatenated correctly. Is my fault. Everything thing else looks fine except for that. Quote Link to comment Share on other sites More sharing options...
Johnnyboy123 Posted May 4, 2011 Author Share Posted May 4, 2011 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. Quote Link to comment Share on other sites More sharing options...
fugix Posted May 4, 2011 Share Posted May 4, 2011 this should do it <?php echo "<a href='editcourse.php?cid={$person['cid']}&cname={$person['cname']}' > edit </a>"; ?> Quote Link to comment Share on other sites More sharing options...
Johnnyboy123 Posted May 5, 2011 Author Share Posted May 5, 2011 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 Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted May 5, 2011 Share Posted May 5, 2011 String values should be wrapped in quotes within your queries. Your first query should be fine however I'd write it as $cid = (int) $_GET['cid']; $q = "SELECT * FROM course WHERE cid = $cid"; For your second query you should write it as $cname = mysql_real_escape_string($_GET['cname']); $q2 = "SELECT * FROM student WHERE cname = '$cname'"; Those two queries should work as expected but you could actually merge those two queries together using a JOIN. Quote Link to comment Share on other sites More sharing options...
fugix Posted May 5, 2011 Share Posted May 5, 2011 yeah, i beleive that you received that error because in your query your get vars appeared like $_GET[cid]"; instead of $_GET['cid']"; you forgot to wrap your indices in quotes Quote Link to comment Share on other sites More sharing options...
Johnnyboy123 Posted May 5, 2011 Author Share Posted May 5, 2011 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 Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted May 5, 2011 Share Posted May 5, 2011 Read my reply above. It is because your queries are not constructed correctly. Quote Link to comment Share on other sites More sharing options...
Johnnyboy123 Posted May 5, 2011 Author Share Posted May 5, 2011 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!"; } ?> Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted May 6, 2011 Share Posted May 6, 2011 Again you're not writing your queries correctly $u2 = "UPDATE student SET `cname` = '$_POST[cname]' WHERE cname = $_POST[cname]"; $_POST[cname] should be wrapped in quotes, like you did in the second line of code above. Also posting raw post values into a query without sanitizing it will result in your queries being hijacked using SQL injection. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.