我的程序没有插入到数据库中,尝试了各种方法,我是php的新手并试图用这个来测试自己,但我发现很难得到。我认为问题在于与数据库的连接,但我没有得到它
<!DOCTYPE html>
<html>
<head>
<title>LIST</title>
</head>
<body>
<h1> TODO LIST </h1>
<?php
if(isset($_POST["submit"])){
$servername = "localhost";
$username = "root";
$password = "";
try {
//create a database conneection
$conn = mysqli_connect("localhost", "root", "");
if(!$conn){
die("Database connection failed: ". mysql_error());
}
$sql = "INSERT INTO tasks (task, date, time) VALUES (:task, :date, :time)";
$query = $conn->prepare($sql);
$query->execute(array(':task'=>$task,
':date'=>$date,':time'=>$time));
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
}
?>
<form action = "todolist.php" method = "post">
Task: <input type="text" name="task" id="task"><br/>
Date: <input type="date" name="date" id="date"><br/>
Time: <input type="time" name="time" id="time"><br/>
<input type = "submit" value = "submit" name="submit">
</form>
</body>
</html>
答案 0 :(得分:0)
您不是选择任何数据库。 尝试这样的事情。
$conn = mysqli_connect("localhost", "root", "", "Yourdatabasename");
答案 1 :(得分:0)
您是否已连接到数据库?你没有选择任何bdd,日志是正确的吗?
也许试试:
ax.set_yticks([])
首先,查看您是否已连接到数据库
答案 2 :(得分:0)
我在我的一个项目中使用这种方式。创建 connect.php 文件并创建如下连接:
<?php
$conn = mysqli_connect("localhost", "root", "", "Yourdatabasename");
?>
现在在index.php文件中检查是否按下了提交按钮:
<?php
if(isset($_POST["submit"]))
{
// include connection file
include("connect.php");
// get values
$subject = mysqli_real_escape_string($con, $_POST["subject"]);
$comment = mysqli_real_escape_string($con, $_POST["comment"]);
// Insert in database
$query = "INSERT INTO comments(comment_subject, comment_text)VALUES ('$subject', '$comment')";
mysqli_query($con, $query);
}
?>