最简单的php写入数据库,小程序测试用
<?php
function writetosql(){
$servername = "localhost";
$username = "root";
$password = "123456";
$dbname = "test";
$ts = time();
$fname = $_POST["name"];
$type = $_POST["tt"];
$note = $_POST["note"]?$_POST["note"]:"小懒虫";
// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 修改数据库连接字符集为 utf8
mysqli_set_charset($conn,"utf8");
// 检测连接
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
$sql = 'INSERT INTO test(fname,type,note,time)VALUES("'.$fname.'","'.$type.'","'.$note.'","'.
$ts.'")';
//echo $sql;
if ($conn->query($sql) === TRUE) {
echo "新记录插入成功";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}
?>