Jump to content

Add, edit, delete script problems


arjanvr

Recommended Posts

I edited a add, edit, delete script from database and I am running into 2 problems I cannot fix. Can anyone tell me what I am doing wrong?

 

First the index.php works fine.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Toevoegen</title>
</head>

<body>

<?php
# errors weergeven
ini_set('display_errors',1); // 1 == aan , 0 == uit
error_reporting(E_ALL | E_STRICT);
?>

<form method="post">
<table>

	<tr>
		<td>Ambtenaarnummer:</td>
		<td><input type="text" name="ambtenaarnummer" /></td>
	</tr>
	<tr>
		<td>Achternaam:</td>
		<td><input type="text" name="achternaam" /></td>
	</tr>
	<tr>
		<td>Tussenvoegsel</td>
		<td><input type="text" name="tussenvoegsel" /></td>
	</tr>
	<tr>
		<td>voorletters</td>
		<td><input type="text" name="voorletters" /></td>
	</tr>
    <tr>
        <td>Voornamen:</td>
        <td><input type="text" name="voornamen" /></td>
    </tr>
    <tr>
        <td>Groep:</td>
        <td><input type="text" name="groep" /></td>
    </tr>
    <tr>
        <td>Straat:</td>
        <td><input type="text" name="straat" /></td>
    </tr>
    <tr>
        <td>Postcode:</td>
        <td><input type="text" name="postcode" /></td>
    </tr>
    <tr>
        <td>Plaats:</td>
        <td><input type="text" name="plaats" /></td>
    </tr>
    <tr>
        <td>Telefoon:</td>
        <td><input type="text" name="telefoon" /></td>
    </tr>
	<tr>
		<td> </td>
		<td><input type="submit" name="submit" value="add" /></td>
	</tr>
</table>
<?php
if (isset($_POST['submit']))
	{	   
	include 'db.php';
                    $ambtenaarnummer=$_POST['ambtenaarnummer'] ;
					$achternaam= $_POST['achternaam'] ;					
					$tussenvoegsel=$_POST['tussenvoegsel'] ;
					$voorletters=$_POST['voorletters'] ;
                    $voornamen=$_POST['voornamen'] ;
                    $groep= $_POST['groep'] ;                    
                    $straat=$_POST['straat'] ;
                    $postcode=$_POST['postcode'] ;
                    $plaats=$_POST['plaats'] ;
                    $telefoon=$_POST['telefoon'] ;
                                            
												
         mysql_query("INSERT INTO `controleurs`(ambtenaarnummer,achternaam,tussenvoegsel,voorletters,voornamen,groep,straat,postcode,plaats,telefoon) 
         VALUES ('$ambtenaarnummer','$achternaam','$tussenvoegsel','$voorletters','$voornamen','$groep','$straat','$postcode','$plaats','$telefoon')");

   }
?>
</form>
<table border="1">
	
			<?php
            error_reporting(E_ALL);
			include("db.php");
			
				
			$result=mysql_query("SELECT * FROM controleurs");
			
			while($test = mysql_fetch_array($result))
			{
				$id = $test['ID'];	
				echo "<tr align='center'>";	
				echo"<td><font color='black'>" .$test['ambtenaarnummer']."</font></td>";
				echo"<td><font color='black'>" .$test['achternaam']."</font></td>";
				echo"<td><font color='black'>". $test['tussenvoegsel']. "</font></td>";
				echo"<td><font color='black'>". $test['voorletters']. "</font></td>";
				echo"<td><font color='black'>". $test['voornamen']. "</font></td>";
                echo"<td><font color='black'>" .$test['groep']."</font></td>";
                echo"<td><font color='black'>" .$test['straat']."</font></td>";
                echo"<td><font color='black'>". $test['postcode']. "</font></td>";
                echo"<td><font color='black'>". $test['plaats']. "</font></td>";
                echo"<td><font color='black'>". $test['telefoon']. "</font></td>";	
				echo"<td> <a href ='view.php?ID=$id'>Edit</a>";
				echo"<td> <a href ='del.php?ID=$id'><center>Delete</center></a>";
									
				echo "</tr>";
			}
			mysql_close($conn);
			?>
</table>

</body>
</html>

Then we have the delete script (del.php). It does actually delete stuff but it still shows an error. I got this in the past aswell and was never able to fix it

 

It shows this warning:

Warning: Cannot modify header information - headers already sent by (output started at /home/schoolme/public_html/ret/db.php:10) in /home/schoolme/public_html/ret/del.php on line 11

<?php
    # errors weergeven
    ini_set('display_errors',1); // 1 == aan , 0 == uit
    error_reporting(E_ALL | E_STRICT);
    
    include("db.php");  

	$id =$_REQUEST['ID'];
	
	
	// sending query
	mysql_query("DELETE FROM controleurs WHERE ID = '$id'")
	or die(mysql_error());  	
	
	header("Location: index.php");
?>

And then we have the edit (view.php) script. It shows a lot of warnings but it does not show the data to edit in the fields either.. my guess is this needs the most work but if someone can tell me how to fix it and what I have done wrong it would be very helpfull

<?php
# errors weergeven
ini_set('display_errors',1); // 1 == aan , 0 == uit
error_reporting(E_ALL | E_STRICT);

require("db.php");
$test =$_REQUEST['ID'];

$result = mysql_query("SELECT * FROM controleurs WHERE ID  = '$id'");
$id = mysql_fetch_array($result);
if (!$result) 
		{
		die("Error: Data not found..");
		}
				$id=$test['ID'] ;
                $ambtenaarnummer=$_POST['ambtenaarnummer'] ;
                $achternaam=$_POST['achternaam'] ;                    
                $tussenvoegsel=$_POST['tussenvoegsel'] ;
                $voorletters=$_POST['voorletters'] ;
                $voornamen=$_POST['voornamen'] ;
                $groep=$_POST['groep'] ;                    
                $straat=$_POST['straat'] ;
                $postcode=$_POST['postcode'] ;
                $plaats=$_POST['plaats'] ;
                $telefoon=$_POST['telefoon'] ;

                if(isset($_POST['save']))
        {	
	            $id_save = $_POST['ID'];
	            $ambtenaarnummer_save = $_POST['ambtenaarnummer'];
	            $achternaam_save = $_POST['achternaam'];
	            $tussenvoegsel_save = $_POST['tussenvoegsel'];
                $voorletters_save = $_POST['voorletters'];
                $voornamen_save = $_POST['voornamen'];
                $groep_save = $_POST['groep'];
                $straat_save = $_POST['straat'];
                $postcode_save = $_POST['postcode'];
                $plaats_save = $_POST['plaats'];
                $telefoon_save = $_POST['telefoon'];

	mysql_query("UPDATE books SET ambtenaarnummer ='$ambtenaarnummer_save',achternaam ='$achternaam_save',$tussenvoegsel='tussenvoegsel_save',$voorletters='voorletters_save',$voornamen='voornamen_save',$groep='groep_save',$straat='straat',$postcode='postcode_save',plaats ='$plaats_save',telefoon ='$telefoon_save' WHERE BookID = '$id'")
				or die(mysql_error()); 
	echo "Saved!";
	
	header("Location: index.php");			
}
mysql_close($conn);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<form method="post">
<table>

    <tr>
        <td>Ambtenaarnummer:</td>
        <td><input type="text" name="ambtenaarnummer" /></td>
    </tr>
    <tr>
        <td>Achternaam:</td>
        <td><input type="text" name="achternaam" /></td>
    </tr>
    <tr>
        <td>Tussenvoegsel</td>
        <td><input type="text" name="tussenvoegsel" /></td>
    </tr>
    <tr>
        <td>voorletters</td>
        <td><input type="text" name="voorletters" /></td>
    </tr>
    <tr>
        <td>Voornamen:</td>
        <td><input type="text" name="voornamen" /></td>
    </tr>
    <tr>
        <td>Groep:</td>
        <td><input type="text" name="groep" /></td>
    </tr>
    <tr>
        <td>Straat:</td>
        <td><input type="text" name="straat" /></td>
    </tr>
    <tr>
        <td>Postcode:</td>
        <td><input type="text" name="postcode" /></td>
    </tr>
    <tr>
        <td>Plaats:</td>
        <td><input type="text" name="plaats" /></td>
    </tr>
    <tr>
        <td>Telefoon:</td>
        <td><input type="text" name="telefoon" /></td>
    </tr>
    <tr>
        <td> </td>
        <td><input type="submit" name="submit" value="add" /></td>
    </tr>
</table>

</body>
</html>

Thanks in advance.. if the script is not well secured its for internal use only so it should not be a problem..

 

Sorry for the lengthy question

Link to comment
Share on other sites

It shows this warning:

Warning: Cannot modify header information - headers already sent by (output started at /home/schoolme/public_html/ret/db.php:10) in /home/schoolme/public_html/ret/del.php on line 11

 

The header() function needs to be called before any output to the screen. The following quote is from the PHP manual (http://php.net/manual/en/function.header.php):

 

 

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

Link to comment
Share on other sites

Warning: Cannot modify header information - headers already sent by (output started at /home/schoolme/public_html/ret/db.php:10) in /home/schoolme/public_html/ret/del.php on line 11

 

 

line 10 in your db.php file is outputting something, probably a new-line after your closing ?> tag.

 

as to your last piece of code. i recommend that you first define what you are trying to do, before writing the code. define what input(s) you have, what processing you want to do based on that input, and what output you want. i guess your intent is to take an id in the url, retrieve the matching row from the database, and output those values in form field. then, when that form is submitted, update that row in the database. write (and test) the code for each of those steps/tasks, one at a time.

Link to comment
Share on other sites

And then we have the edit (view.php) script. It shows a lot of warnings but it does not show the data to edit in the fields either..

 

It helps to know what those warnings are so we can help. I would imagine that you're seeing errors like the following:

 

Notice: Undefined index: ambtenaarnummer in...

 

Also note that you're outputting something to the screen before using the header() function here:

<?php
//...
 
echo "Saved!";
 
header("Location: index.php");
 
//...
?>
Link to comment
Share on other sites

 

It helps to know what those warnings are so we can help.

 

sorry forgot to post those..

 

Notice: Undefined variable: id in /home/schoolme/public_html/ret/view.php on line 9

 

Notice: Undefined index: ambtenaarnummer in /home/schoolme/public_html/ret/view.php on line 16

 

Notice: Undefined index: achternaam in /home/schoolme/public_html/ret/view.php on line 17

 

Notice: Undefined index: tussenvoegsel in /home/schoolme/public_html/ret/view.php on line 18

 

Notice: Undefined index: voorletters in /home/schoolme/public_html/ret/view.php on line 19

 

Notice: Undefined index: voornamen in /home/schoolme/public_html/ret/view.php on line 20

 

Notice: Undefined index: groep in /home/schoolme/public_html/ret/view.php on line 21

 

Notice: Undefined index: straat in /home/schoolme/public_html/ret/view.php on line 22

 

Notice: Undefined index: postcode in /home/schoolme/public_html/ret/view.php on line 23

 

Notice: Undefined index: plaats in /home/schoolme/public_html/ret/view.php on line 24

 

Notice: Undefined index: telefoon in /home/schoolme/public_html/ret/view.php on line 25

Link to comment
Share on other sites

Instead of these or in addition to it?

				$id=$test['ID'] ;
                $ambtenaarnummer=$_POST['ambtenaarnummer'] ;
                $achternaam=$_POST['achternaam'] ;                    
                $tussenvoegsel=$_POST['tussenvoegsel'] ;
                $voorletters=$_POST['voorletters'] ;
                $voornamen=$_POST['voornamen'] ;
                $groep=$_POST['groep'] ;                    
                $straat=$_POST['straat'] ;
                $postcode=$_POST['postcode'] ;
                $plaats=$_POST['plaats'] ;
                $telefoon=$_POST['telefoon'] ;

                if(isset($_POST['save']))
        {	
	            $id_save = $_POST['ID'];
	            $ambtenaarnummer_save = $_POST['ambtenaarnummer'];
	            $achternaam_save = $_POST['achternaam'];
	            $tussenvoegsel_save = $_POST['tussenvoegsel'];
                $voorletters_save = $_POST['voorletters'];
                $voornamen_save = $_POST['voornamen'];
                $groep_save = $_POST['groep'];
                $straat_save = $_POST['straat'];
                $postcode_save = $_POST['postcode'];
                $plaats_save = $_POST['plaats'];
                $telefoon_save = $_POST['telefoon'];
Link to comment
Share on other sites

you seem to be trying to program without any idea what the lines of code you are putting down in the file actually do. that won't result in working code any time soon because it will take you an infinite number of attempts before the random code actually does what you intend.

 

programming requires that you know what the statements do, so that you know where and how to use them and can determine how to put them together with other statements in a meaningful way.

 

i'm going to repost this since you might have missed it -

 

as to your last piece of code. i recommend that you first define what you are trying to do, before writing the code. define what input(s) you have, what processing you want to do based on that input, and what output you want. i guess your intent is to take an id in the url, retrieve the matching row from the database, and output those values in form field. then, when that form is submitted, update that row in the database. write (and test) the code for each of those steps/tasks, one at a time.

 

until you have broken the problem down and have a defined list of steps you need to accomplish each task, you won't be able to write code that performs that task. your current code has php statements in it that don't have anything to do with retrieving the data from the database and putting those values into the form fields so that you could edit them.

Link to comment
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.