na szybko, amatorsko
class news
{
private $table_name = 'table_news';
private $db;
private static $dbHost = 'localhost'; private static $dbDataBase = 'nazwa_bazy'; private static $dbUser = 'login'; private static $dbPassword = 'haslo';
public function __construct()
{
try
{
echo '<h1>Dodaj swoje newsy</h1>'; $this->view();
}
catch (Exception $e)
{
throw new Exception($e->getMessage(), $e->getCode());
}
}
public function view()
{
echo '<div><form method="post" action="nazwa_tego_pliku.php"> <p>Temat: <input type="text" name="topic"/></p>
<p>News: <input type="text" name="news"/></p>
<p><input type="submit" value="Dodaj"/></p>
</form>
</div><br />';
$sql = $this->connect_db();
if(!$result = $sql->query("SELECT * FROM ".$this->table_name.""))
{
throw new exception ('Blad zapytania select do bazy');
}
else
{
echo '<tr bgcolor=\"#CCCCFF\"><td>id</td><td>temat</td><td>nowosc</td><td>data</td></tr>'; while ($row = $result->fetch_array())
{
echo '<td>'.$row['id'].'</td><td>'.$row['temat'].'</td><td>'.$row['news'].'</td><td>'.$row['data_dodania'].'</td>'; }
}
}
public function connect_db()
{
try
{
$this->db = new mysqli($this->dbHost, $this->dbUser, $this->dbPassword, $this->dbDataBase);
if(!$db)
{
throw new exception ('Blad laczenia z baza danych');
}
else
{
return $db;
}
}
catch (Exception $e)
{
throw new Exception($e->getMessage(), $e->getCode());
}
}
public function add_news()
{
try
{
$temat = $_POST['topic'];
$news = $_POST['news'];
$sql = $this->connect_db();
if(!$result = $sql->query("INSERT INTO ".$this->table_name." ('id','temat','news','data_dodania') VALUES (null,'".$temat."','".$news."','".date('y-m-d h:m:i')."')")) {
throw new exception ('Blad zapytania insert do bazy');
}
}
catch (Exception $e)
{
throw new Exception($e->getMessage(), $e->getCode());
}
}
public function update_news($new_temat, $new_news, $id)
{
try
{
$sql = $this->connect_db();
if(!$result = $sql->query("UPDATE ".$this->table_name." SET ('temat' = '".$new_temat."', 'news' = '".$new_news."') WHERE id = '".$id."' "))
{
throw new exception ('Blad zapytania update do bazy');
}
}
catch (Exception $e)
{
throw new Exception($e->getMessage(), $e->getCode());
}
}
public function delete_news($id)
{
try
{
$sql = $this->connect_db();
if(!$result = $sql->query("DELETE FROM ".$this->table_name." WHERE id = '".$id."' "))
{
throw new exception ('Blad usuwania newsow');
}
}
catch (Exception $e)
{
throw new Exception($e->getMessage(), $e->getCode());
}
}
}
Ten post edytował Mastersieciweb 24.03.2015, 22:26:08