Use prepared statements:
$sql = "INSERT INTO joukkueet (name, citynumber, city) VALUES (:number, :nameO, :city)";
$statement = $db->prepare($sql);
$sth = $statement->execute( ['number' => $number, 'nameO' => $nameO, 'city' => $city] );
The things like :number
, :nameO
and :city
are placeholders for values. Using the $db->prepare
function in PDO creates a PDOStatement object. Now, this object has a method execute
, and as you can see, we give it an array whose keys correspond to the placeholder names. So if you have :nameO
in the prepared statement, the array should have a value with the key nameO
, which PDO will safely add into the SQL code.
Of course, it is best that you check the parameters before just using them (e.g. length checks or making sure that $number
is numeric). You ask to check that it is not empty.
Instead of simply:
$nameO = $_POST["name"];
$number = $_POST["number"];
$city = $_POST["city"];
I would suggest:
if (empty($_POST['name']) || empty($_POST['number']) || empty($_POST['city'])) {
exit('Please input all data!');
}
$nameO = $_POST["name"];
$number = $_POST["number"];
$city = $_POST["city"];
You may wish to handle the error another way than to stop with exit
, of course. But a check with empty()
can guarantee that there is at least some data in the variables.