I posted earlier today about converting my MySQL to MySQLi - Upon further research I came across the following, Prepared Statements. It seems that this may be a good way to go but I am a bit confused about how to implement it.
I am simply taking User-entered data from a form and adding it to my database. I need to find an example of how to do this using this method.
I found the following example, but it appears to me that it is manually adding it via prepared code. Can anyone please explain explain this to me?
`
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// 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 = "john@example.com";
$stmt->execute();
`
The area of the above code example concerns the section below:
`
// set parameters and execute
$firstname = "John";
$lastname = "Doe";
$email = "john@example.com";
$stmt->execute();
`
Would I just write one of these statements for each of the pre-existing rows in my table? That snippet looks to me that it is assigning values to the variables in advance. Am I missing something about this?
Any help would be much appreciated.
Thank you,
Matthew