Znalazłem to co potrzebuje http://4programmers.net/PHP/Sonda_na_bazie_danych , ale nie mogę tego uruchomić, nie wiem co źle robię. Pewnie jest to banalne, ale nie dla laika.
Wykorzystuje bazę danych 5.1.42, skonfigurowana jako Developer Machine (tzn. łącze się z nią podając tylko hasło "123456", uruchomiając z "C:\Program Files\MySQL\MySQL Server 5.1\bin").
Pierwszy krok:
Tworzę bazę danych.
create database sonda;
use sonda;
CREATE TABLE poll_questions (
id int NOT NULL PRIMARY KEY AUTO_INCREMENT,
title varchar(255) NOT NULL,
date_add datetime NOT NULL,
date_begin datetime NOT NULL,
date_end datetime NOT NULL,
stop int NOT NULL DEFAULT 0
);
CREATE TABLE poll_answers (
id_answer int NOT NULL PRIMARY KEY AUTO_INCREMENT,
id_poll int NOT NULL,
answer varchar(255) NOT NULL,
votes int NOT NULL DEFAULT 0
);
następnie dodaje do bazy danych pytanie i odpowiedzi:
INSERT INTO poll_questions VALUES(
'',
'Twój ulubiony język programowania?',
now(),
now(),
'2020-03-01',
0
);
INSERT INTO poll_answers VALUES('', 1, 'C/C++', 0);
INSERT INTO poll_answers VALUES('', 1, 'Java', 0);
INSERT INTO poll_answers VALUES('', 1, 'PHP', 0);
INSERT INTO poll_answers VALUES('', 1, 'Python', 0);
INSERT INTO poll_answers VALUES('', 1, 'Inny', 0);
Drugi krok:
Następnie robię 1 plik:
index.php
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
</head>
<body>
<?php
error_reporting(E_ALL ~ E_NOTICE);
class Database {
public function __construct() {
mysql_connect('localhost', 'root', '123456');
mysql_select_db('sonda');
}
public function query($sql) {
return mysql_query($sql);
}
public function numrows($sql) {
return mysql_num_rows($sql);
}
public function fetch($sql) {
return mysql_fetch_array($sql);
}
}
$db = new Database;
class Poll {
public $db;
public $other = true; // czy pokazywac inne sondy
public $desc_sort = true; // sortowanie innych sond od najnowszych
public $id; // id sondy
public $new_fields = array(); // funkcje z nowy polami do formularza
public $no_add = false; // nie dodawac (np. ktoras z funkcji z $new_fields mowi, ze dane niepoprawne)
public function __construct()
{
$this->db = new Database();
}
public function display()
{
$sql = $this->db->query('SELECT
q.id, q.title, q.date_begin, q.date_end, q.stop,
a.id_answer, a.answer, a.votes,
(SELECT sum(votes) FROM poll_answers WHERE id_poll = q.id GROUP BY id_poll) as sum
FROM
poll_questions as q, poll_answers as a
WHERE
q.id = a.id_poll AND q.id = ' .
(!isset($_GET['id']) ? '(SELECT max(id) FROM poll_questions)' : (int)$_GET['id']));
if($this->db->numrows($sql) > 0)
{
$now = date('Y-m-d');
while($row = $this->db->fetch($sql))
{
if($_POST['vote'] && !$this->no_add)
{
$row['sum']++;
if($row['id_answer'] == $_POST['vote']) $row['votes']++;
}
if(!$b)
{
$this->id = $row['id'];
if($row['stop'] == 1 || $_POST['vote'] && !$this->no_add) $noform = true;
// podstawowe dane o ankiecie
$ret .= '<b>' . $row['title'] . '</b><p />Łącznie oddano głosów: ' . $row['sum'].
'<br />Data rozpoczęcia: ' . $row['date_begin'] .
'<br />Data zakończenia: ' . $row['date_end'];
if($row['date_end'] <= $now) $ret .= '<p />Ankieta się już zakończyła.';
elseif($row['stop'] == 1) $ret .= '<p />Glosowanie w ankiecie zostało wstrzymane.';
$ret .= '<p />';
// wyswietlenie formularza
if(!isset($_COOKIE['poll' . $this->id]) && $row['date_end'] > $now && !$noform)
{
$ret .= '<form action="" method="post">';
foreach($this->new_fields as $v) $ret .= $v;
$form = true;
} elseif(isset($_COOKIE['poll' . $this->id]) && $row['date_end'] > $now && !$noform)
{
$ret .= 'Głosowałeś już w tej sondzie.<p />';
}
// user zaglosowal
if($_POST['vote'] && !$this->no_add)
{
$ret .= 'Twój głos został dodany.<p />';
if(!isset($_COOKIE['poll' . $this->id]))
{
$this->db->query('UPDATE poll_answers SET votes=votes+1 WHERE id_answer='.$_POST['vote']);
setcookie('poll' . $this->id, $this->id, time()+3600 * 3600 * 30); // 22 lata
}
$noform = true;
}
$b = true;
}
// wyswietlenie wariantow odpowiedzi
if($form)
$ret .= '<input type="radio" name="vote" value="' . $row['id_answer'] . '" /> ' .
$row['answer'] . '<br />';
else
{
$ret .= $row['answer'].', ' . $row['votes'] . ' glosow, ' .
($row['sum'] > 0 ? round($row['votes']*100/$row['sum']) : 0) . '% ' .
'<div style="background: red; height: 10px; width: ' .
($row['votes'] == 0 || $row['sum'] == 0 ? 5 : round($row['votes'] * 200 / $row['sum'])) .
'px"></div><br />';
}
}
if($form) $ret .= '<br /><input type="submit" name="submit" value="Głosuj!" /></form>';
if($this->other) $ret .= '<p /><b>Inne sondy</b><p />' . $this->other($this->id);
}
else $ret = 'Nie ma takiej sondy w bazie.';
return $ret;
}
public function other($id)
{
$sql = 'SELECT id, title FROM poll_questions WHERE id <> ' . $id . ' ORDER BY id ' . ($this->desc_sort ? 'DESC' : 'ASC');
$sql = $this->db->query($sql);
if($this->db->numrows($sql) > 0)
{
$ret = '<ul>';
while($row = $this->db->fetch($sql))
$ret .= '<li><a href="' .$_SERVER['PHP_SELF'] . '?id=' . $row['id'] . '">' . $row['title'] . '</a></li>';
return $ret . '</ul>';
}
else return '(brak)';
}
}
$poll = new Poll;
echo $poll->display();
?>
</body>
</html>
Co trzeba poprawić ?