Ik ben nu een 2-tal weken bezig met php & mysql. Ik werk aan de hand van een boek die pas uitgekomen is en van de recentste Lynda.com videos. Ik focus me op Mysqli ipv PDO omdat ik zelf toch uitsluitend werk met Mysql. Het bevalt me enorm! Het enige dat er natuurlijk gebeurd is dat je na een tijdje begint te experimenteren. Het werkt en je bent blij...maar is het wel goed?...of correct?
Ik heb bijvoorbeeld iets simpels in elkaar geflanst, vlug vlug...
Doordat men enorm waarschuwt voor sql injection werk ik met prepared statements....
Kan iemand mij vertellen of mijn spaghetticode op "iets" trekt en veilig is? Ben ik op de goede weg?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>title</title>
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
</head>
<body>
<form action="homes.php" method="POST">
<p>Zoek: <input type="text" name="search" /></p>
<p>Voornaam: <input type="text" name="Voornaam" /></p>
<p>Achternaam: <input type="text" name="Achternaam" /></p>
<p>Adres: <input type="text" name="Adres" /></p>
<p>Discipline: <input type="text" name="Discipline" value="1 tot 5" /></p>
<p>Bevestig zoeken: <input type="submit" name="Submit" /></p>
<p>Voeg toe aan databank <input type="submit" name="Add_DB" value="Add to Database" /></p>
</form>
<?php
require_once 'isset.php';
?>
</body>
</html>
This is the PHP
<?php
require_once 'login.php';
$dbcon = mysqli_connect($db_host, $db_username, $db_password, $db_database);
if(mysqli_connect_errno()) die ("Error during connection");
if(isset($_POST['Submit'])){
$Naam = $_POST['search'];
$Result = mysqli_query($dbcon,"SELECT * FROM customers WHERE Voornaam = '$Naam'");
if(!$Result) die ("Nothing to show");
$Rows = $Result->num_rows;
for($i=0; $i < $Rows; $i++){
$Row = mysqli_fetch_array($Result, MYSQLI_ASSOC);
echo "Voornaam: " . $Row['Voornaam'] . "<br>";
echo "Achternaam: " . $Row['Achternaam'] . "<br>";
echo "Adres: " . $Row['Adres'] . "<br>";
echo "<hr>";
}
}
if(isset($_POST['Add_DB'])){
$sql="INSERT INTO customers(Voornaam, Achternaam, Adres, Actief, Discipline)
VALUES(?,?,?,NOW(),?)";
if($stmt = $dbcon->prepare($sql)){
$stmt->bind_param('sssi', $Voornaam, $Achternaam, $Adres, $Discipline);
$Voornaam = $_POST['Voornaam'];
$Achternaam = $_POST['Voornaam'];
$Adres = $_POST['Adres'];
$Discipline = $_POST['Discipline'];
$stmt->execute();
echo "New records created successfully";
}
}
mysqli_close($dbcon);
?>
Groeten,
Michaël