OOP INSERT INTO 语句不起作用
OOP INSERT INTO statement not working
我正在创建一个非常简单的留言板来学习 OOP 风格的编程。目的是显示保存在数据库中的post,在显示的post下面,你可以添加一个新的post到数据库,它也会显示在页面上。
我在调用 index.php 页面上的函数时遇到问题,所以当我单击提交时,它会将文本区域中的内容添加到数据库中。有什么建议吗?
Index.php
<?php
ini_set("display_errors", 1);
error_reporting(E_ALL);
require_once("prikbord.class.php");
$p = new Prikbord();
$bericht = $p->getbericht();
if(isset($_POST['submit'])){
$p->setbericht($_POST[$bericht]);
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Prikbord</title>
</head>
<body>
<?php
echo $p->showdata();
?>
<form action="" type="post">
<textarea name="bericht" placeholder="Uw bericht" style="display: block; width: 350px; height: 150px;"></textarea>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
Prikbord.class.php
<?php
class Prikbord {
private $db;
private $data = array();
public function __construct() {
$this->db = new mysqli("localhost", "root", "root", "prikbord");
if($this->db->connect_error) {
die("Fout met database verbinding.");
}
}
public function getbericht() {
$sql = "SELECT naam, datum, bericht FROM prikbord";
$result = $this->db->query($sql);
while($row = $result->fetch_assoc()) {
$this->data[] = $row;
}
return $this->data;
}
public function showdata() {
$return = "";
foreach($this->data as $item) {
$return .= "<div>" . $item["naam"] . "</div>";
$return .= "<div>" . $item["datum"] . "</div>";
$return .= "<div>" . $item["bericht"] . "</div>";
$return .= "<hr style='width: 100px; float: left; display: inline;'>";
$return .= "<br><br>";
}
return $return;
}
public function setbericht($bericht) {
$sql = "INSERT INTO prikbord(bericht) VALUES ('" . $bericht . "')";
$result = $this->db->query($sql);
if($this->result) {
echo "Data insert succesfully.";
}
}
}
?>
您根本没有发布您的数据。你应该像这样打开你的表格:
<form name="myForm" method="POST">
注:是method
,不是type
。
我正在创建一个非常简单的留言板来学习 OOP 风格的编程。目的是显示保存在数据库中的post,在显示的post下面,你可以添加一个新的post到数据库,它也会显示在页面上。
我在调用 index.php 页面上的函数时遇到问题,所以当我单击提交时,它会将文本区域中的内容添加到数据库中。有什么建议吗?
Index.php
<?php
ini_set("display_errors", 1);
error_reporting(E_ALL);
require_once("prikbord.class.php");
$p = new Prikbord();
$bericht = $p->getbericht();
if(isset($_POST['submit'])){
$p->setbericht($_POST[$bericht]);
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Prikbord</title>
</head>
<body>
<?php
echo $p->showdata();
?>
<form action="" type="post">
<textarea name="bericht" placeholder="Uw bericht" style="display: block; width: 350px; height: 150px;"></textarea>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
Prikbord.class.php
<?php
class Prikbord {
private $db;
private $data = array();
public function __construct() {
$this->db = new mysqli("localhost", "root", "root", "prikbord");
if($this->db->connect_error) {
die("Fout met database verbinding.");
}
}
public function getbericht() {
$sql = "SELECT naam, datum, bericht FROM prikbord";
$result = $this->db->query($sql);
while($row = $result->fetch_assoc()) {
$this->data[] = $row;
}
return $this->data;
}
public function showdata() {
$return = "";
foreach($this->data as $item) {
$return .= "<div>" . $item["naam"] . "</div>";
$return .= "<div>" . $item["datum"] . "</div>";
$return .= "<div>" . $item["bericht"] . "</div>";
$return .= "<hr style='width: 100px; float: left; display: inline;'>";
$return .= "<br><br>";
}
return $return;
}
public function setbericht($bericht) {
$sql = "INSERT INTO prikbord(bericht) VALUES ('" . $bericht . "')";
$result = $this->db->query($sql);
if($this->result) {
echo "Data insert succesfully.";
}
}
}
?>
您根本没有发布您的数据。你应该像这样打开你的表格:
<form name="myForm" method="POST">
注:是method
,不是type
。