arjanvr Posted September 23, 2013 Share Posted September 23, 2013 While still working on my add, edit delete script I receive below error while trying to edit a entry. I am not sure, even though I have error reporting, what this error is caused by.. The error that is returned Notice: Undefined index: id in /home/schoolme/public_html/ret/mysqli/facturen-nw.php on line 70Error! The line it is refering to is: if (is_numeric($_POST['id'])) <?php /* Staat de gebruiker toe om nieuwe records toe te voegen te bewerken */ // connect to the database include("connect-db.php"); # errors weergeven ini_set('display_errors',1); // 1 == aan , 0 == uit error_reporting(E_ALL | E_STRICT); // Maakt nieuw/edit record formulier function renderForm($bedrijfsnaam ='', $factuurbedrag ='', $vervaldatum ='', $voldaan ='', $opmerkingen ='', $id ='') { ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title> <?php if ($id != '') { echo "Edit Record"; } else { echo "New Record"; } ?> </title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> </head> <body> <h1><?php if ($id != '') { echo "Edit Record"; } else { echo "New Record"; } ?></h1> <?php if ($error != '') { echo "<div style='padding:4px; border:1px solid red; color:red'>" . $error . "</div>"; } ?> <form action="" method="post"> <div> <?php if ($id != '') { ?> <input type="hidden" name="id" value="<?php echo $id; ?>" /> <p>ID: <?php echo $id; ?></p> <?php } ?> <strong>Bedrijfs(naam):</strong> <input type="text" name="bedrijfsnaam" value="<?php echo $bedrijfsnaam; ?>"/><br/> <strong>Factuurbedrag:</strong> <input type="text" name="factuurbedrag" value="<?php echo $factuurbedrag; ?>"/><br/> <strong>Vervaldatum:</strong> <input type="text" name="vervaldatum" value="<?php echo $vervaldatum; ?>"/><br/> <strong>Voldaan (ja/nee):</strong> <input type="text" name="voldaan" value="<?php echo $voldaan; ?>"/><br/> <strong>Opmerkingen:</strong> <input type="text" name="opmerkingen" value="<?php echo $opmerkingen; ?>"/><p/> <input type="submit" name="submit" value="Submit" /> </div> </form> </body> </html> <?php } /* BEWERK RECORD */ // Wanneer de 'id' variabel is ingesteld in de URL, weten we dat we een record moeten aanpassen if (isset($_GET['id'])) { // Wanneer de submitknop word ingedrukt word het formulier verwerkt if (isset($_POST['submit'])) { // Zorgt ervoor dat de 'id' geldig is if (is_numeric($_POST['id'])) { // verkrijg variabelen van URL/formulier $id = $_POST['id']; $bedrijfsnaam = htmlentities($_POST['bedrijfsnaam'], ENT_QUOTES); $factuurbedrag = htmlentities($_POST['factuurbedrag'], ENT_QUOTES); $vervaldatum = htmlentities($_POST['vervaldatum'], ENT_QUOTES); $voldaan = htmlentities($_POST['voldaan'], ENT_QUOTES); $opmerkingen = htmlentities($_POST['opmerkingen'], ENT_QUOTES); { // update de database if ($stmt = $mysqli->prepare("UPDATE facturen SET bedrijfsnaam = ?, factuurbedrag = ?, vervaldatum = ?, voldaan = ?, opmerkingen = ? WHERE id=?")) { $stmt->bind_param("sssssi", $bedrijfsnaam, $factuurbedrag, $vervaldatum, $voldaan, $opmerkingen, $id); $stmt->execute(); $stmt->close(); } // toont foutmelding indien nodig else { echo "ERROR: could not prepare SQL statement."; } // redirect na submit van formulier header("Location: facturen.php"); } } // bij ongeldige 'id' komt een foutmelding else { echo "Error!"; } } // Indien het formulier niet verstuurd is word de database en het formulier weergegeven else { // zorgt dat de 'id' geldig is if (is_numeric($_GET['id']) && $_GET['id'] > 0) { // verkrijg 'id' van URL $id = $_GET['id']; // verkrijg de records uit de database if($stmt = $mysqli->prepare("SELECT * FROM facturen WHERE id=?")) { $stmt->bind_param("i", $id); $stmt->execute(); $stmt->bind_result($id, $bedrijfsnaam, $factuurbedrag, $vervaldatum, $voldaan, $opmerkingen); $stmt->fetch(); // toon het formulier renderForm($bedrijfsnaam, $factuurbedrag, $vervaldatum, $voldaan, $opmerkingen, NULL, $id); $stmt->close(); } // toon een error wanneer de query een error heeft else { echo "Error: could not prepare SQL statement"; } } // wanneer het 'id' ongeldig is word de gebruiker naar view.php doorgestuurd else { header("Location: facturen.php"); } } } /* NIEUW RECORD */ // wanneer geen 'id' is ingesteld volgt een nieuwe invoer else { // proces het formlier na submit if (isset($_POST['submit'])) { // verkrijg formulierdata $bedrijfsnaam = htmlentities($_POST['bedrijfsnaam'], ENT_QUOTES); $factuurbedrag = htmlentities($_POST['factuurbedrag'], ENT_QUOTES); $vervaldatum = htmlentities($_POST['vervaldatum'], ENT_QUOTES); $voldaan = htmlentities($_POST['voldaan'], ENT_QUOTES); $opmerkingen = htmlentities($_POST['opmerkingen'], ENT_QUOTES); { // insert the new record into the database if ($stmt = $mysqli->prepare("INSERT facturen (bedrijfsnaam, factuurbedrag, vervaldatum, voldaan, opmerkingen) VALUES (?, ?, ?, ?, ?)")) { $stmt->bind_param("sssss", $bedrijfsnaam, $factuurbedrag, $vervaldatum, $voldaan, $opmerkingen); $stmt->execute(); $stmt->close(); } // Toon een foutmelding indien nodig else { echo "ERROR: Could not prepare SQL statement."; } // stuur gebruiker door header("Location: facturen.php"); } } // wanneer geen formulier is ingediend word de database weergegeven else { renderForm(); } } // sluit mysqli verbinding $mysqli->close(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/282389-edit-script-error/ Share on other sites More sharing options...
ILYAS415 Posted September 23, 2013 Share Posted September 23, 2013 Try adding id="id" to: <input type="hidden" name="id" value="<?php echo $id; ?>" /> Quote Link to comment https://forums.phpfreaks.com/topic/282389-edit-script-error/#findComment-1450884 Share on other sites More sharing options...
arjanvr Posted September 23, 2013 Author Share Posted September 23, 2013 No luck, tried both <input type="hidden" name="id=" value="<?php echo $id="id"; ?>" /> and <input type="hidden" name=id="id" value="<?php echo $id; ?>" /> I noticed on the edit page itself it says Notice: Undefined variable: error in /home/schoolme/public_html/ret/mysqli/facturen-nw.php on line 26 Which is this line <?php if ($error != '') { echo "<div style='padding:4px; border:1px solid red; color:red'>" . $error . "</div>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/282389-edit-script-error/#findComment-1450894 Share on other sites More sharing options...
Ch0cu3r Posted September 23, 2013 Share Posted September 23, 2013 (edited) I noticed on the edit page itself it says Notice: Undefined variable: error in /home/schoolme/public_html/ret/mysqli/facturen-nw.php on line 26 Which is this line <?php if ($error != '') { echo "<div style='padding:4px; border:1px solid red; color:red'>" . $error . "</div>"; } ?> That line can be easily fixed using <?php if (isset($error) && !empty($error) { As for the undefined id index. it appears your form is not submitting the id field on form submission. the $id variable appears to be passed to the renderForm() function as a parameter function renderForm($bedrijfsnaam ='', $factuurbedrag ='', $vervaldatum ='', $voldaan ='', $opmerkingen ='', $id ='') The code for this function will only set the id form field if the $id variable is not emtpy. <?php if ($id != '') { ?> <input type="hidden" name="id" value="<?php echo $id; ?>" /> <p>ID: <?php echo $id; ?></p> <?php } ?> The format of the HTML is correct for the id form field. Where is the function renderForm() called from and how is $id set? Edited September 23, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/282389-edit-script-error/#findComment-1450896 Share on other sites More sharing options...
arjanvr Posted September 23, 2013 Author Share Posted September 23, 2013 I think you mean this, where the id etc is shown? Otherwise i dont understand the question <!DOCTYPEHTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>Overzicht facturen</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> </head> <body> <h1>View Records</h1> <p><b>Bekijk alles</b> | <a href="facturen-pagina.php">Bekijk per 10</a></p> <?php // connect to the database include('connect-db.php'); // get the records from the database if ($result = $mysqli->query("SELECT * FROM facturen ORDER BY bedrijfsnaam")) { // display records if there are records to display if ($result->num_rows > 0) { // display records in a table echo "<table border='1' cellpadding='10'>"; // set table headers echo "<tr><th>id</th><th>Bedrijfs(naam)</th><th>factuurbedrag</th><th>Vervaldatum</th><th>Voldaan<br><small>ja/nee</small></th><th>Opmerkingen</th><th></th><th></th></tr>"; while ($row = $result->fetch_object()) { // set up a row for each record echo "<tr>"; echo "<td>" . $row->id . "</td>"; echo "<td>" . $row->bedrijfsnaam . "</td>"; echo "<td>" . $row->factuurbedrag . "</td>"; echo "<td>" . $row->vervaldatum . "</td>"; echo "<td>" . $row->voldaan . "</td>"; echo "<td>" . $row->opmerkingen . "</td>"; echo "<td><a href='facturen-nw.php?id=" . $row->id . "'>Edit</a></td>"; echo "<td><a href='del.php?id=" . $row->id . "'>Delete</a></td>"; echo "</tr>"; } echo "</table>"; } // if there are no records in the database, display an alert message else { echo "No results to display!"; } } // show an error if there is an issue with the database query else { echo "Error: " . $mysqli->error; } // close database connection $mysqli->close(); ?> <a href="facturen-nw.php">Voeg nieuwe factuur toe</a> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/282389-edit-script-error/#findComment-1450899 Share on other sites More sharing options...
ILYAS415 Posted September 23, 2013 Share Posted September 23, 2013 I'm no expert in PHP, so forgive if this is a stupid question, but why is all your html in a function? I mean, you could try taking all the html out of the function. Perhaps the code below will help me explain better: // Maakt nieuw/edit record formulier function renderForm($bedrijfsnaam ='', $factuurbedrag ='', $vervaldatum ='', $voldaan ='', $opmerkingen ='', $id ='') { ?> All of your HTML is here <? } //your input is processed down here but it seems you dont need the function renderForm Quote Link to comment https://forums.phpfreaks.com/topic/282389-edit-script-error/#findComment-1450905 Share on other sites More sharing options...
arjanvr Posted September 23, 2013 Author Share Posted September 23, 2013 I'm no expert in PHP, so forgive if this is a stupid question, but why is all your html in a function? Most likely because i am still trying to learn it and my knowledge of PHP is lower then yours. I hope to learn by example. Quote Link to comment https://forums.phpfreaks.com/topic/282389-edit-script-error/#findComment-1450906 Share on other sites More sharing options...
ILYAS415 Posted September 23, 2013 Share Posted September 23, 2013 lol sorry if I came across as a dickhead. I was honestly unsure if you could do that or not (and I still am!). Does it work when you take away the function from around the html? Quote Link to comment https://forums.phpfreaks.com/topic/282389-edit-script-error/#findComment-1450907 Share on other sites More sharing options...
arjanvr Posted September 23, 2013 Author Share Posted September 23, 2013 (edited) You didn't so don't worry about it. If i remove the function i get Notice: Undefined variable: id in /home/schoolme/public_html/ret/mysqli/facturen-nw.php on line 24New Record Notice: Undefined variable: error in /home/schoolme/public_html/ret/mysqli/facturen-nw.php on line 25 Notice: Undefined variable: id in /home/schoolme/public_html/ret/mysqli/facturen-nw.php on line 32Bedrijfs(naam): <br /><b>Notice</b>: Undefined variable: bedrijfsnaam in <b>/home/schoolme/public_html/ret/mysqli/facturen-nw.php</b> on line <b>38</b><br />Factuurbedrag: <br /><b>Notice</b>: Undefined variable: factuurbedrag in <b>/home/schoolme/public_html/ret/mysqli/facturen-nw.php</b> on line <b>40</b><br />Vervaldatum: <br /><b>Notice</b>: Undefined variable: vervaldatum in <b>/home/schoolme/public_html/ret/mysqli/facturen-nw.php</b> on line <b>42</b><br />Voldaan (ja/nee): <br /><b>Notice</b>: Undefined variable: voldaan in <b>/home/schoolme/public_html/ret/mysqli/facturen-nw.php</b> on line <b>44</b><br />Opmerkingen: <br /><b>Notice</b>: Undefined variable: opmerkingen in <b>/home/schoolme/public_html/ret/mysqli/facturen-nw.php</b> on line <b>46</b><br /> Fatal error: Call to undefined function renderForm() in /home/schoolme/public_html/ret/mysqli/facturen-nw.php on line 123 Strange thing is that the form is working for me in a dir where i tested it with different input. Edited September 23, 2013 by arjanvr Quote Link to comment https://forums.phpfreaks.com/topic/282389-edit-script-error/#findComment-1450925 Share on other sites More sharing options...
DavidAM Posted September 23, 2013 Share Posted September 23, 2013 Your original code: // Wanneer de 'id' variabel is ingesteld in de URL, weten we dat we een record moeten aanpassen if (isset($_GET['id'])) { // Wanneer de submitknop word ingedrukt word het formulier verwerkt if (isset($_POST['submit'])) { // Zorgt ervoor dat de 'id' geldig is if (is_numeric($_POST['id'])) { is checking to see if $_GET['id'] is set, then retrieving the value from $_POST['id']. These are two different resources. If the FORM method is POST and the "id" is a field in the form (which is what I see), you should be checking $_POST. If you are expecting the "id" to be part of the URL, then you need to retrieve it from $_GET. If you want to allow it to come from either, you can use $_REQUEST, however, since you have it in the form, you have to watch out for the possibility that it comes in both places AND IS DIFFERENT Quote Link to comment https://forums.phpfreaks.com/topic/282389-edit-script-error/#findComment-1450948 Share on other sites More sharing options...
arjanvr Posted September 24, 2013 Author Share Posted September 24, 2013 What I dont understand, I have the same script with different variables running in the same directory but the other script does work while the part you mention is the same.. // Wanneer de 'id' variabel is ingesteld in de URL, weten we dat we een record moeten aanpassen if (isset($_GET['id'])) { // Wanneer de submitknop word ingedrukt word het formulier verwerkt if (isset($_POST['submit'])) { // Zorgt ervoor dat de 'id' geldig is if (is_numeric($_POST['id'])) { // verkrijg variabelen van URL/formulier $id = $_POST['id']; $klantnummer = htmlentities($_POST['klantnummer'], ENT_QUOTES); $bedrijfsnaam = htmlentities($_POST['bedrijfsnaam'], ENT_QUOTES); $adres = htmlentities($_POST['adres'], ENT_QUOTES); $postcode = htmlentities($_POST['postcode'], ENT_QUOTES); $plaats = htmlentities($_POST['plaats'], ENT_QUOTES); $londerhoud = htmlentities($_POST['londerhoud'], ENT_QUOTES); $aantal = htmlentities($_POST['aantal'], ENT_QUOTES); $vonderhoud = htmlentities($_POST['vonderhoud'], ENT_QUOTES); { // update de database if ($stmt = $mysqli->prepare("UPDATE klanten SET klantnummer = ?, bedrijfsnaam = ?, adres = ?, postcode = ?, plaats = ?, londerhoud = ?, aantal = ?, vonderhoud = ? WHERE id=?")) { $stmt->bind_param("ssssssssi", $klantnummer, $bedrijfsnaam, $adres, $postcode, $plaats, $londerhoud, $aantal, $vonderhoud, $id); $stmt->execute(); $stmt->close(); } // toont foutmelding indien nodig else { echo "ERROR: could not prepare SQL statement."; } // redirect na submit van formulier header("Location: view.php"); } } // bij ongeldige 'id' komt een foutmelding else { echo "Error!"; } } So what is the difference why one does work and the other does not? Quote Link to comment https://forums.phpfreaks.com/topic/282389-edit-script-error/#findComment-1450983 Share on other sites More sharing options...
Ch0cu3r Posted September 24, 2013 Share Posted September 24, 2013 Use this code for facturen-new.php <?php /* Staat de gebruiker toe om nieuwe records toe te voegen te bewerken */ // connect to the database include("connect-db.php"); # errors weergeven ini_set('display_errors',1); // 1 == aan , 0 == uit error_reporting(E_ALL | E_STRICT); // Maakt nieuw/edit record formulier function renderForm($bedrijfsnaam ='', $factuurbedrag ='', $vervaldatum ='', $voldaan ='', $opmerkingen ='', $id ='', $error = '') { $title = (empty($id)) ? "New Record" : "Edit Record"; // all HTML is now held within the $HTML variable. $HTML = <<<HTML <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>$title</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> </head> <body> <h1>$title</h1> $error <form action="facturen-new.php" method="post"> <div> HTML; // DO NOT MOVE?CHANGE THE ABOVE LNE if (!empty($error)) $HTML .= "\n<div style='padding:4px; border:1px solid red; color:red'>" . $error . "</div>\n\n"; if(!empty($id)) $HTML .= '<input type="hidden" name="id" value="' . $id . '" /><p>ID:' . $id .'</p>'; $HTML .= <<<HTML <strong>Bedrijfs(naam):</strong> <input type="text" name="bedrijfsnaam" value="$bedrijfsnaam"/><br/> <strong>Factuurbedrag:</strong> <input type="text" name="factuurbedrag" value="$factuurbedrag"/><br/> <strong>Vervaldatum:</strong> <input type="text" name="vervaldatum" value="$vervaldatum"/><br/> <strong>Voldaan (ja/nee):</strong> <input type="text" name="voldaan" value="$voldaan"/><br/> <strong>Opmerkingen:</strong> <input type="text" name="opmerkingen" value="$opmerkingen"/><p/> <input type="submit" name="submit" value="Submit" /> </div> </form> </body> </html> HTML; // DO NOT MOVE/CHANGE THE ABOVE LINE // function returns the generated HTML return $HTML; } // set action to create a new record $action = 'new'; // override action to either edit or update if(isset($_REQUEST['id']) && is_numeric($_REQUEST['id'])) { // get the record id from _GET or _POST $id = (int) $_REQUEST['id']; // what action we're doing // if id is in url eg: facturen.php?id=1 then we're editing the record // If its not in the url then we're updating the record $action = isset($_GET['id']) ? 'edit' : 'update'; } // if the form has been submitted if(isset($_POST['submit'])) { $bedrijfsnaam = htmlentities($_POST['bedrijfsnaam'], ENT_QUOTES); $factuurbedrag = htmlentities($_POST['factuurbedrag'], ENT_QUOTES); $vervaldatum = htmlentities($_POST['vervaldatum'], ENT_QUOTES); $voldaan = htmlentities($_POST['voldaan'], ENT_QUOTES); $opmerkingen = htmlentities($_POST['opmerkingen'], ENT_QUOTES); // run query depending on actions if($action == 'new') // inserts new record { $stmt = $mysqli->prepare("INSERT INTO facturen SET bedrijfsnaam = ?, factuurbedrag = ?, vervaldatum = ?, voldaan = ?, opmerkingen = ?"); $stmt->bind_param("sssss", $bedrijfsnaam, $factuurbedrag, $vervaldatum, $voldaan, $opmerkingen); } elseif($action == 'update') // updates record { $stmt = $mysqli->prepare("UPDATE facturen SET bedrijfsnaam = ?, factuurbedrag = ?, vervaldatum = ?, voldaan = ?, opmerkingen = ? WHERE id=?"); $stmt->bind_param("sssssi", $bedrijfsnaam, $factuurbedrag, $vervaldatum, $voldaan, $opmerkingen, $id); } // executes query and redirects on sucess if($stmt->execute()) { header('Location: facturen.php'); exit; } else { $error = 'Cannot continue! Error with database'; } // otherwise display form with error echo enderForm($bedrijfsnaam, $factuurbedrag, $vervaldatum, $voldaan, $opmerkingen, $id, $error); } elseif($action == 'edit') { $stmt = $mysqli->prepare("SELECT * FROM facturen WHERE id=?"); $stmt->bind_param("i", $id); $stmt->execute(); $stmt->bind_result($id, $bedrijfsnaam, $factuurbedrag, $vervaldatum, $voldaan, $opmerkingen); $stmt->fetch(); echo renderForm($bedrijfsnaam, $factuurbedrag, $vervaldatum, $voldaan, $opmerkingen, $id); } else echo renderForm(); // sluit mysqli verbinding $mysqli->close(); ?> I have gone through it and cleaned it up. You had a lot of repetitive code which was redundant. The script should work fine I have tested that it edit/update/create new records in the database. Quote Link to comment https://forums.phpfreaks.com/topic/282389-edit-script-error/#findComment-1451006 Share on other sites More sharing options...
arjanvr Posted September 24, 2013 Author Share Posted September 24, 2013 Thank you, it is working with this new code. I will compare the 2 codes to see what lines you removed and how i can use that in the future. Quote Link to comment https://forums.phpfreaks.com/topic/282389-edit-script-error/#findComment-1451050 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.