要将todo数据存储到数据库中,可以使用PHP和MySQL来实现。以下是一个简单的示例代码:
CREATE TABLE todos (
id INT AUTO_INCREMENT PRIMARY KEY,
task VARCHAR(255) NOT NULL,
completed TINYINT(1) NOT NULL DEFAULT 0
);
// 连接到数据库
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";
$conn = new mysqli($servername, $username, $password, $dbname);
// 检查连接是否成功
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// 获取todo数据
$task = $_POST['task'];
// 插入todo数据到数据库
$sql = "INSERT INTO todos (task) VALUES ('$task')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
// 关闭数据库连接
$conn->close();
<form action="insert_todo.php" method="post">
<input type="text" name="task" placeholder="Enter your todo task">
<button type="submit">Add Todo</button>
</form>
通过以上步骤,你可以使用PHP将todo数据存储到数据库中。请记得根据实际情况修改数据库连接信息和表名。