Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/254886-newbie-help/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/254886-newbie-help/#findComment-1306901
Share on other sites

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!";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/254886-newbie-help/#findComment-1306902
Share on other sites

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.

 

 

Link to comment
https://forums.phpfreaks.com/topic/254886-newbie-help/#findComment-1306935
Share on other sites

$_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?

Link to comment
https://forums.phpfreaks.com/topic/254886-newbie-help/#findComment-1306949
Share on other sites

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>";
 ?>

Link to comment
https://forums.phpfreaks.com/topic/254886-newbie-help/#findComment-1306954
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/254886-newbie-help/#findComment-1306958
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.