html数据传到mysql简单例子

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>testmysql</title>
</head>

<body>

<form action = 'loginmysql.php' method="post">
分数: <input type="text" name="right" ><br>
<button type="submit">提交到mysql</button>
</form>

</body>
</html>
<?php
$serverName = "localhost";
$username = "root";//linux数据库名称
$password = "password";//数据库密码
$dbname = "2048";//自己创建的数据库名

// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检测连接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
} 
$rightscore= $_POST['right'];//html表单传过来的数据
$sql = "INSERT INTO 2048score (score)
VALUES ($rightscore)";//into 后面是表名,接下来是字段,最后是值

if ($conn->query($sql) === TRUE) {
    echo "新记录插入成功";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
posted @ 2017-09-21 15:04  云胡同学  阅读(1777)  评论(0编辑  收藏  举报