arjanvr Posted September 19, 2013 Share Posted September 19, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/282282-add-edit-delete-script-problems/ Share on other sites More sharing options...
cyberRobot Posted September 19, 2013 Share Posted September 19, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/282282-add-edit-delete-script-problems/#findComment-1450236 Share on other sites More sharing options...
mac_gyver Posted September 19, 2013 Share Posted September 19, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/282282-add-edit-delete-script-problems/#findComment-1450238 Share on other sites More sharing options...
cyberRobot Posted September 19, 2013 Share Posted September 19, 2013 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"); //... ?> Quote Link to comment https://forums.phpfreaks.com/topic/282282-add-edit-delete-script-problems/#findComment-1450241 Share on other sites More sharing options...
arjanvr Posted September 19, 2013 Author Share Posted September 19, 2013 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): It refered to line 12 in db.php while at line 10 there was the ?> After removing 2 blanc lines this problem was solved. Quote Link to comment https://forums.phpfreaks.com/topic/282282-add-edit-delete-script-problems/#findComment-1450242 Share on other sites More sharing options...
arjanvr Posted September 19, 2013 Author Share Posted September 19, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/282282-add-edit-delete-script-problems/#findComment-1450243 Share on other sites More sharing options...
cyberRobot Posted September 19, 2013 Share Posted September 19, 2013 Basically, the POST variables aren't set yet. You could fix it by doing something like <?php //... $ambtenaarnummer = (isset($_POST['ambtenaarnummer'])) ? $_POST['ambtenaarnummer'] : ''; //... ?> Of course, you'll need to fix the other variables. :-) Quote Link to comment https://forums.phpfreaks.com/topic/282282-add-edit-delete-script-problems/#findComment-1450245 Share on other sites More sharing options...
mac_gyver Posted September 19, 2013 Share Posted September 19, 2013 ummm. the use of the first set of $_POST variables doesn't have anything to do with the code where they are at. the errors are the result of incorrect logic and the logic needs to be defined first. Quote Link to comment https://forums.phpfreaks.com/topic/282282-add-edit-delete-script-problems/#findComment-1450247 Share on other sites More sharing options...
arjanvr Posted September 19, 2013 Author Share Posted September 19, 2013 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']; Quote Link to comment https://forums.phpfreaks.com/topic/282282-add-edit-delete-script-problems/#findComment-1450248 Share on other sites More sharing options...
mac_gyver Posted September 19, 2013 Share Posted September 19, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/282282-add-edit-delete-script-problems/#findComment-1450268 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.