3.982 views
Hallo, allemaal is htmlspecialchar of escape voor pdo nodig? Ik wil een formulier maken dat bezoekers via front end kunnen invullen zodat in de database wordt opgeslagen.
<?php
error_reporting(E_ALL);
ini_set('display_errors',true);
?>
<?php
/*
Allows the user to both create new records and edit existing records
*/
// connect to the database
include("config.php");
// creates the new/edit record form
// since this form is used multiple times in this file, I have made it a function that is easily reusable
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
<form action="" method="post">
<div>
<strong>First Name: *</strong> <input type="text" name="voornaam"
<br/>
<strong>Last Name: *</strong> <input type="text" name="achternaam"
/>
<p>* required</p>
<input type="submit" name="submit" value="Submit" />
</div>
</form>
</body>
</html>
<?php
// if the form's submit button is clicked, we need to process the form
if (isset($_POST['submit']))
{
// get the form data
$voornaam = mysqli_real_escape_string($_POST['voornaam']);
$achternaam = mysqli_real_escape_string($_POST['achternaam']);
// check that voornaam and achternaam are both not empty
if ($voornaam == '' || $achternaam == '')
{
// if they are empty, show an error message and display the form
$error = 'ERROR: Please fill in all required fields!';
renderForm($voornaam, $achternaam, $error);
}
else
{
// insert the new record into the database
if ($stmt = $mysqli->prepare("INSERT formulier (voornaam, achternaam) VALUES (?, ?)"))
{
$stmt->bind_param("ss", $voornaam, $achternaam);
$stmt->execute();
$stmt->close();
}
// show an error if the query has an error
else
{
echo "ERROR: Could not prepare SQL statement.";
}
// redirec the user
header("Location: view.php");
}
}
// close the mysqli connection
$mysqli->close();
?>
Johan de wit op 25/09/2016 22:27:46
Hartstikke bedankt Koen! Dit is veel duidelijk geworden.
Ik wil een formulier maken dat de bezoekers formulier kunnen invullen zoals NAW gegevens etc etc, daarom kwam ik zo`n vraag.
Nu loop ik tegen een fout aan waar ik niet uit kan komen, moet ik hiervoor een nieuwe topic openen of hier met een nieuwe titel verder gaan?
Hierbij een script wat goed werkt alleen ik kan een deel "wijzigen" niet verwijderen dan krijg ik een witte scherm zonder foutmeldingen (staat ook aan).
<?php error_reporting(E_ALL); ini_set('display_errors',true); ?> <?php /* Allows the user to both create new records and edit existing records */ // connect to the database include("config.php"); // creates the new/edit record form // since this form is used multiple times in this file, I have made it a function that is easily reusable function renderForm($first = '', $last ='', $error = '', $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" /> <p>ID: <?php echo $id; ?></p> <?php } ?> <strong>First Name: *</strong> <input type="text" name="voornaam" <br/> <strong>Last Name: *</strong> <input type="text" name="achternaam" /> <p>* required</p> <input type="submit" name="submit" value="Submit" /> </div> </form> </body> </html> <?php } /* EDIT RECORD */ // if the 'id' variable is set in the URL, we know that we need to edit a record if (isset($_GET['id'])) { // if the form's submit button is clicked, we need to process the form if (isset($_POST['submit'])) { // make sure the 'id' in the URL is valid if (is_numeric($_POST['id'])) { // get variables from the URL/form $id = $_POST['id']; $voornaam = htmlentities($_POST['voornaam'], ENT_QUOTES); $achternaam = htmlentities($_POST['achternaam'], ENT_QUOTES); // check that voornaam and achternaam are both not empty if ($voornaam == '' || $achternaam == '') { // if they are empty, show an error message and display the form $error = 'ERROR: Please fill in all required fields!'; renderForm($voornaam, $achternaam, $error, $id); } else { // if everything is fine, update the record in the database if ($stmt = $mysqli->prepare("UPDATE players SET voornaam = ?, achternaam = ? WHERE id=?")) { $stmt->bind_param("ssi", $voornaam, $achternaam, $id); $stmt->execute(); $stmt->close(); } // show an error message if the query has an error else { echo "ERROR: could not prepare SQL statement."; } // redirect the user once the form is updated header("Location: view.php"); } } // if the 'id' variable is not valid, show an error message else { echo "Error!"; } } // if the form hasn't been submitted yet, get the info from the database and show the form else { // make sure the 'id' value is valid if (is_numeric($_GET['id']) && $_GET['id'] > 0) { // get 'id' from URL $id = $_GET['id']; // get the recod from the database if($stmt = $mysqli->prepare("SELECT * FROM players WHERE id=?")) { $stmt->bind_param("i", $id); $stmt->execute(); $stmt->bind_result($id, $voornaam, $achternaam); $stmt->fetch(); // show the form renderForm($voornaam, $achternaam, NULL, $id); $stmt->close(); } // show an error if the query has an error else { echo "Error: could not prepare SQL statement"; } } // if the 'id' value is not valid, redirect the user back to the view.php page else { header("Location: view.php"); } } } /* NEW RECORD */ // if the 'id' variable is not set in the URL, we must be creating a new record else { // if the form's submit button is clicked, we need to process the form if (isset($_POST['submit'])) { // get the form data $voornaam = htmlentities($_POST['voornaam'], ENT_QUOTES); $achternaam = htmlentities($_POST['achternaam'], ENT_QUOTES); // check that voornaam and achternaam are both not empty if ($voornaam == '' || $achternaam == '') { // if they are empty, show an error message and display the form $error = 'ERROR: Please fill in all required fields!'; renderForm($voornaam, $achternaam, $error); } else { // insert the new record into the database if ($stmt = $mysqli->prepare("INSERT formulier (voornaam, achternaam) VALUES (?, ?)")) { $stmt->bind_param("ss", $voornaam, $achternaam); $stmt->execute(); $stmt->close(); } // show an error if the query has an error else { echo "ERROR: Could not prepare SQL statement."; } // redirec the user header("Location: view.php"); } } // if the form hasn't been submitted yet, show the form else { renderForm(); } } // close the mysqli connection $mysqli->close(); ?>
Ben van Velzen op 25/09/2016 23:13:41
Ik zie dit alvast:
$voornaam = htmlentities($_POST['voornaam'], ENT_QUOTES);
Dit moet je dus heel erg niet doen! htnlentities etc gebruik je bij WEERGAVE, nooit in je query.
Koen Hollander op 25/09/2016 23:17:16
[quote="Johan de wit op 25/09/2016 22:27:46"]
Hartstikke bedankt Koen! Dit is veel duidelijk geworden.
Ik wil een formulier maken dat de bezoekers formulier kunnen invullen zoals NAW gegevens etc etc, daarom kwam ik zo`n vraag.
Nu loop ik tegen een fout aan waar ik niet uit kan komen, moet ik hiervoor een nieuwe topic openen of hier met een nieuwe titel verder gaan?
<?php error_reporting(E_ALL); ini_set('display_errors',true); ?> <?php /* Allows the user to both create new records and edit existing records */ // connect to the database include("config.php"); // creates the new/edit record form // since this form is used multiple times in this file, I have made it a function that is easily reusable ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> </head> <body> <form action="" method="post"> <div> <strong>First Name: *</strong> <input type="text" name="voornaam" <br/> <strong>Last Name: *</strong> <input type="text" name="achternaam" /> <p>* required</p> <input type="submit" name="submit" value="Submit" /> </div> </form> </body> </html> <?php // if the form's submit button is clicked, we need to process the form if (isset($_POST['submit'])) { // get the form data $voornaam = mysqli_real_escape_string($_POST['voornaam']); $achternaam = mysqli_real_escape_string($_POST['achternaam']); // check that voornaam and achternaam are both not empty if ($voornaam == '' || $achternaam == '') { // if they are empty, show an error message and display the form $error = 'ERROR: Please fill in all required fields!'; renderForm($voornaam, $achternaam, $error); } else { // insert the new record into the database if ($stmt = $mysqli->prepare("INSERT formulier (voornaam, achternaam) VALUES (?, ?)")) { $stmt->bind_param("ss", $voornaam, $achternaam); $stmt->execute(); $stmt->close(); } // show an error if the query has an error else { echo "ERROR: Could not prepare SQL statement."; } // redirec the user header("Location: view.php"); } } // close the mysqli connection $mysqli->close(); ?>
<?php
error_reporting(E_ALL);
ini_set('display_errors',true);
?>
<?php
/*
Allows the user to both create new records and edit existing records
*/
// connect to the database
include("config.php");
// creates the new/edit record form
// since this form is used multiple times in this file, I have made it a function that is easily reusable
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
<form action="" method="post">
<div>
<strong>First Name: *</strong> <input type="text" name="voornaam"
<br/>
<strong>Last Name: *</strong> <input type="text" name="achternaam"
/>
<p>* required</p>
<input type="submit" name="submit" value="Submit" />
</div>
</form>
</body>
</html>
<?php
// if the form's submit button is clicked, we need to process the form
if (isset($_POST['submit']))
{
// get the form data
$voornaam = mysqli_real_escape_string($_POST['voornaam']);
$achternaam = mysqli_real_escape_string($_POST['achternaam']);
// check that voornaam and achternaam are both not empty
if ($voornaam == '' || $achternaam == '')
{
// if they are empty, show an error message and display the form
$error = 'ERROR: Please fill in all required fields!';
renderForm($voornaam, $achternaam, $error);
}
else
{
// insert the new record into the database
if ($stmt = $mysqli->prepare("INSERT formulier (voornaam, achternaam) VALUES (?, ?)"))
{
$stmt->bind_param("ss", $voornaam, $achternaam);
$stmt->execute();
$stmt->close();
}
// show an error if the query has an error
else
{
echo "ERROR: Could not prepare SQL statement.";
}
// redirec the user
header("Location: view.php");
}
}
// close the mysqli connection
$mysqli->close();
?>
Ben van Velzen op 26/09/2016 11:10:47
Dat, en je maakt gebruik van prepared statements. Als je dan gaat escapen dan doe je de escaping dubbel, waardoor je data vervuild de database in gaat. Prepared statements doen de escaping voor je. Dat maakt ze zo krachtig. Haal de escaping dus gewoon weg. Is nergens goed voor. Het enige dat het nu zal doen is je data verknallen.
Call to undefined function renderForm()