concrete Posted January 12, 2012 Share Posted January 12, 2012 Hi all, I am completing an assignment for Uni and I have run it to a bit of difficulty. I hope someone can help me out. I am deleting a record from the database and I want it to then display the student record number, firstname and surname. I know it is probably something simple but I can see where I am going wrong! <?php $id=$_REQUEST['id']; $firstname=$_GET['firstname']; $surname=$_GET['surname']; $db="prs"; $link=mysql_connect("localhost","gandalf","bella") or die("Cannot Connect to the database!"); mysql_select_db($db,$link) or die ("Cannot select the database!"); $query="DELETE FROM students WHERE upn='".$id."'"; if(!mysql_query($query,$link)) {die ("An unexpected error occured while <b>deleting</b> the record, Please try again!");} else { echo "Student with Student Number #".$id." ".$firstname." ".$surname." removed successfully!";} ?> Thanks in advance for any and all help, it is much appreciated. Cheers, Fergal Quote Link to comment https://forums.phpfreaks.com/topic/254886-newbie-help/ Share on other sites More sharing options...
litebearer Posted January 12, 2012 Share Posted January 12, 2012 what error are you getting/experiencing? Quote Link to comment https://forums.phpfreaks.com/topic/254886-newbie-help/#findComment-1306900 Share on other sites More sharing options...
dharmeshpat Posted January 12, 2012 Share Posted January 12, 2012 dude don't do the hard delete do soft delete example put one more column in database say "status" when user is created the status column for that user will be default active and as soon as u try to delete the record don't delete the record just change the status to deactivated and now when ever u try to display the list of user just display the list with active status Quote Link to comment https://forums.phpfreaks.com/topic/254886-newbie-help/#findComment-1306901 Share on other sites More sharing options...
QuickOldCar Posted January 12, 2012 Share Posted January 12, 2012 This should be easier to read, enclose in the code tags please. Change your mysql connection credentials, you posted them online for everyone to see. <?php $id=$_REQUEST['id']; $firstname=$_GET['firstname']; $surname=$_GET['surname']; $db="prs"; $link=mysql_connect("localhost",".....",".....") or die("Cannot Connect to the database!"); mysql_select_db($db,$link) or die ("Cannot select the database!"); $query="DELETE FROM students WHERE upn='".$id."'"; if(!mysql_query($query,$link)) { die ("An unexpected error occured while <b>deleting</b> the record, Please try again!"); } else { echo "Student with Student Number #".$id." ".$firstname." ".$surname." removed successfully!"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/254886-newbie-help/#findComment-1306902 Share on other sites More sharing options...
concrete Posted January 12, 2012 Author Share Posted January 12, 2012 what error are you getting/experiencing? The record is deleted and I move to the next page I want the following to be displayed: Student with Student Number #002 - John Smith has been removed successfully! What I am getting is: Student with Student Number #002 - has been removed successfully! The firstname ans Surname are not displaying. Quote Link to comment https://forums.phpfreaks.com/topic/254886-newbie-help/#findComment-1306935 Share on other sites More sharing options...
concrete Posted January 12, 2012 Author Share Posted January 12, 2012 QuickOldCar - that is dummy data, thanks anyway.. dharmeshpat - my tutor has gave us the bare bone and we have to adapt it to suit ourselves. So I have to use this code. Thanks anyway Fergal Quote Link to comment https://forums.phpfreaks.com/topic/254886-newbie-help/#findComment-1306937 Share on other sites More sharing options...
Muddy_Funster Posted January 12, 2012 Share Posted January 12, 2012 You are changing your transport method in the middle of the code: $id=$_REQUEST['id']; $firstname=$_GET['firstname']; $surname=$_GET['surname']; Is there a reason for that? I suspect that the values in the $_GET[] array are not actualy being populated. Quote Link to comment https://forums.phpfreaks.com/topic/254886-newbie-help/#findComment-1306939 Share on other sites More sharing options...
concrete Posted January 12, 2012 Author Share Posted January 12, 2012 Hi Muddy_Funster, There is no real reason for this! How do I populate the Array? Sorry if this is a silly question but I have been using PHP for 1 week!! Quote Link to comment https://forums.phpfreaks.com/topic/254886-newbie-help/#findComment-1306945 Share on other sites More sharing options...
Muddy_Funster Posted January 12, 2012 Share Posted January 12, 2012 $_GET[] uses the url to provide values to variables. $_REQUEST[] uses all of $_GET[] $_POST[] and $_COOKIE[] methods. $_POST[] method is normaly used by forms. $_COOKIE[] uses cookie session information. Where are you expecting to get the $_GET['firstname'] and $_GET['surname'] information from? Is it from a previous page? is it taken from the database or is it entered by the user? can you show the code where the information is first populated? Quote Link to comment https://forums.phpfreaks.com/topic/254886-newbie-help/#findComment-1306949 Share on other sites More sharing options...
concrete Posted January 12, 2012 Author Share Posted January 12, 2012 I want to take the data from the database or if possible from the previous page! <?php $db="prs"; $link=mysql_connect("localhost","..........",".........") or die("Cannot Connect to the database!"); mysql_select_db($db,$link) or die ("Cannot select the database!"); $query="SELECT * FROM students"; $resource=mysql_query($query,$link); echo " <table align=\"center\" border=\"1\" cellpadding=\"5\" cellspacing=\"0\" bordercolor=\"#de5f57\" width=\"80%\"> <tr> <td><h4>First Name</h4></td> <td><h4>Surname</h4></td> <td><h4>Address</h4></td> <td><h4>Phone</h4></td> <td><h4>Class</h4></td> <td><h4>Delete</h4></td> </tr> "; while($result=mysql_fetch_array($resource)) { echo "<tr class=\"table_data\"><td>".$result[1]."</td><td>".$result[2]."</td><td>".$result[3]."</td><td>".$result[8]."</td><td>".$result[10]."</td><td> <a href=\"del_student1.php?id=".$result[0]."\"><img border=\"0\" src=\"images/delete.png\" onmouseover=\"this.src='images/delete_over.png';\" onmouseout=\"this.src='images/delete.png';\" /></a> </td></tr>"; } echo "</table>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/254886-newbie-help/#findComment-1306954 Share on other sites More sharing options...
Muddy_Funster Posted January 12, 2012 Share Posted January 12, 2012 This here is your key part of the code for what you want to do: <a href=\"del_student1.php?id=".$result[0]."\"> That code is passing the varible key "id" into the $_GET[] method with the value of "$result[0]" (which is the id returned from the SELECT query you ran earlier.) This is done by the use of the ? at the end of the url declaration. To pass fistname and lastname along with it, you will need to add them onto the end using an & for each key you want to populate in the $_GET[]. To make this all more readable, and less complicated, instead of breaking out of the string each time and using a "." to join the string together, we keep using the double quotes and wrap the array refferences inside {}'s So you should end up with: <a href=\"del_student1.php?id={$result[0]}&firstname={$result[1]}&surname={$result[2]}\"> you should change your $_REQUEST to $_GET on the other page, for consistancy. Quote Link to comment https://forums.phpfreaks.com/topic/254886-newbie-help/#findComment-1306958 Share on other sites More sharing options...
concrete Posted January 12, 2012 Author Share Posted January 12, 2012 Ahh lad, You're a legend Really Appreciate your help! Quote Link to comment https://forums.phpfreaks.com/topic/254886-newbie-help/#findComment-1306961 Share on other sites More sharing options...
Muddy_Funster Posted January 12, 2012 Share Posted January 12, 2012 lol, it's seldom these days I get called "lad", and even more seldom "legend" . You are very welcome however. Quote Link to comment https://forums.phpfreaks.com/topic/254886-newbie-help/#findComment-1306962 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.