要將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數據存儲到數據庫中。請記得根據實際情況修改數據庫連接信息和表名。