Created
July 27, 2015 14:31
-
-
Save kirilkirkov/f2719db25c830a058471 to your computer and use it in GitHub Desktop.
Example about prepare statements. No SQL Injections... no bugs, be safe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$servername = "localhost"; | |
$username = "root"; | |
$password = "toor"; | |
$dbname = "test"; | |
// Create connection | |
$conn = new mysqli($servername, $username, $password, $dbname); | |
// Check connection | |
if ($conn->connect_error) { | |
die("Connection failed: " . $conn->connect_error); | |
} | |
// prepare and bind | |
$stmt = $conn->prepare("INSERT INTO MyGuests (firstname, lastname, email) VALUES (?, ?, ?)"); | |
$stmt->bind_param("sss", $firstname, $lastname, $email); | |
// set parameters and execute | |
$firstname = "John"; | |
$lastname = "Doe"; | |
$email = "[email protected]"; | |
$stmt->execute(); | |
$firstname = "Mary"; | |
$lastname = "Moe"; | |
$email = "[email protected]"; | |
$stmt->execute(); | |
$firstname = "Julie"; | |
$lastname = "Dooley"; | |
$email = "[email protected]"; | |
$stmt->execute(); | |
echo "Records are inserted successful !"; | |
$stmt->close(); | |
$conn->close(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The above example is Prepared Statements in MySQLi
The argument (in $stmt->bind_param ...) may be one of four types: