od paru dni próbuję przypomnieć sobie podstawy OOP tworząc prostego cmsa z funkcją dodawania artykułów.
Jednak wywala mi się błąd: No database selected. Do kodu włączyłem nowe funkcje mysqli, więc chyba tam jest błąd, niestety po godzinach ślęczenia, nie potrafię go znaleźć.
Formularz:
Kod
<html>
<body>
<form action="dzieki.php" method="post">
Autor: <input type="text" name="autor"><br /><br />
Tytuł: <input type="text" name="tytul"><br /><br />
Tekst: <input type="text" name="tekst"><br /><br />
<input type="submit" value="Dodaj">
</form>
</body>
</html>
<body>
<form action="dzieki.php" method="post">
Autor: <input type="text" name="autor"><br /><br />
Tytuł: <input type="text" name="tytul"><br /><br />
Tekst: <input type="text" name="tekst"><br /><br />
<input type="submit" value="Dodaj">
</form>
</body>
</html>
klasy (class/DB_pt.class.php):
Kod
<?php
class DB {
protected $db_name = 'aaa';
protected $db_user = 'root';
protected $db_pass = 'aaa';
protected $db_host = 'localhost';
public function connect() {
$connection = new mysqli($this->db_host, $this->db_user, $this->db_pass, $this->db_name);
//mysql_select_db($this->db_name);
return true;
}
public function insert($data, $table) {
$columns = "";
$values = "";
foreach ($data as $column => $value) {
$columns .= ($columns == "") ? "" : ", ";
$columns .= $column;
$values .= ($values == "") ? "" : ", ";
$values .= $value;
}
$sql = "insert into $table ($columns) values ($values)";
mysql_query($sql) or die(mysql_error());
return mysql_insert_id();
echo 'Dziękujemy, że dodałeś artykuł:)';
}
}
?>
class DB {
protected $db_name = 'aaa';
protected $db_user = 'root';
protected $db_pass = 'aaa';
protected $db_host = 'localhost';
public function connect() {
$connection = new mysqli($this->db_host, $this->db_user, $this->db_pass, $this->db_name);
//mysql_select_db($this->db_name);
return true;
}
public function insert($data, $table) {
$columns = "";
$values = "";
foreach ($data as $column => $value) {
$columns .= ($columns == "") ? "" : ", ";
$columns .= $column;
$values .= ($values == "") ? "" : ", ";
$values .= $value;
}
$sql = "insert into $table ($columns) values ($values)";
mysql_query($sql) or die(mysql_error());
return mysql_insert_id();
echo 'Dziękujemy, że dodałeś artykuł:)';
}
}
?>
dzieki.php
Kod
<?php
include 'class/DB_pt.class.php';
$db = new DB();
$db -> connect();
$data = array(
"$_POST[autor]" => "autor",
"$_POST[tytul]" => "tytul",
"$_POST[tekst]" => "tekst"
);
$db->insert($data, 'artykuly');
?>
include 'class/DB_pt.class.php';
$db = new DB();
$db -> connect();
$data = array(
"$_POST[autor]" => "autor",
"$_POST[tytul]" => "tytul",
"$_POST[tekst]" => "tekst"
);
$db->insert($data, 'artykuly');
?>
Na pewno ścieżki i nazwy tabel są prawidłowe, czy ktoś może mi wskazać błąd?