如何使用Prepared Statement和php插入到mysql中
这个问题在这里已经有了答案:
sample.html
<form action="sample.php" method="POST">
<input name="sample" type="text">
<input name="submit" type="submit" value="Submit">
</form>
sample.php
<?php
if (isset($_POST['submit'])) {
$mysqli = new mysqli('localhost', 'user', 'password', 'mysampledb');
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %sn", mysqli_connect_error());
exit();
}
$stmt = $mysqli->prepare("INSERT INTO SampleTable VALUES (?)");
$stmt->bind_param('s', $sample); // bind $sample to the parameter
$sample = isset($_POST['sample'])
? $_POST['sample']
: '';
/* execute prepared statement */
$stmt->execute();
printf("%d Row inserted.n", $stmt->affected_rows);
/* close statement and connection */
$stmt->close();
/* close connection */
$mysqli->close();
}
?>
这是一个非常基本的例子。 今天许多PHP开发人员正在转向PDO。 Mysqli不是过时的,但PDO要容易得多,恕我直言。
链接地址: http://www.djcxy.com/p/16731.html上一篇: how to insert into mysql using Prepared Statement with php