<?php
if (isset($_POST['submit'])) {
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli('localhost', 'user', 'password', 'mysampledb');
// replace every piece of data in SQL with question mark
$sql = "INSERT INTO SampleTable (name, text) VALUES (?,?)"
// 2 question marks ^^^
// prepare this query
$stmt = $mysqli->prepare($sql);
// bind every variable, adding s for each to the types string
$stmt->bind_param('ss', $_POST['name'], $_POST['text']);
// 2 s letters ^^ ^^ 2 variables ^^
/* Execute prepared statement */
$stmt->execute();
}
1条答案
按热度按时间chy5wohz1#
文件 * 示例.html*
文件 * 示例.php*
这是一个非常基本的例子。今天许多PHP开发者都转向PDO。Mysqli并没有过时,但是PDO要容易得多。