Answer the question
In order to leave comments, you need to log in
How to make a poll out of voting?
Hello dear experts.
Please do not kick me hard, I'm just learning.
There is a voting script, counts, displays the results, and so on. everything is great. fastened an adaptive layout to it, an admin panel with authorization and user registration. everything seems to be not bad. but I would like to modify it a little, but I don’t have enough mind (before kicking, please note that I’m just learning).
Now it goes like this:
I create a vote, it contains answers 1,2,3 or 10 answers and so on.
How can I make the same poll / vote be created, but there were several votes, preferably an unlimited number. For example, I clicked a button, a new question with answer options was added. it turns out, for example, Poll No. 1
There, question No. 1
is option "a"
- option "b"
- option "c"
There is question number 2
- option "a"
- option "b"
- option "c"
Here is the structure of the tables and the code itself when you create a new vote.
Thanks in advance to everyone who helped, I was already desperate (although apparently there is a task to work with relationships between tables or even rows. My head is boiling, confused)
<?php
include_once "config.php"; # подключаем файл с настройками
db_connect(); # подключаемся к базе
# если кнопка НЕ нажата, то выводим форму
if (!isset($_POST['submit'])) {
?>
<form action="" method="POST">
Введите ваш вопрос
<input type="text" name="quest" placeholder="Текст Вашего вопроса" class="form-control">
<br/>
Варианты ответа (1 строчка - 1 вариант)<br/>
<textarea class="form-control" rows="5" id="textareaDefault" name="variant"></textarea>
<br/>
<div class="col-lg-6">
</div>
<input type="submit" name="submit" value="Добавить" class="btn btn-primary"/>
</form>
<br/>
</div>
<?php
}
else { # если кнопка нажата, то заносим в базу опрос
$err = array();
$quest = mysql_real_escape_string($_POST['quest']);
$variant = mysql_real_escape_string($_POST['variant']);
# Проверяем заполнено ли поле для ввода вопроса
if (trim($quest) == '') {
$err[] = "Вы не задали вопрос";
}
# Проверяем введены ли варианты ответа
if (trim($variant) == '') {
$err[] = "Вы не ввели варианты ответа";
}
else {
# Разделяем строки и засовываем их в массив
$variant= explode('\r\n', $variant);
if (count($variant) < 2) {
$err[] = "Вариантов ответа должно быть минимум 2!";
}
}
# Проверяем нет ли уже такого вопроса в базе
$quest_query = mysql_query("SELECT count(*) FROM `poll_question` WHERE quest_name = '".$quest."'") or die(mysql_query());
$count = mysql_result($quest_query,0,0);
if ($count > 0 ) $err[] = "Такой вопрос уже есть в базе, задайте другой";
unset($quest_query);
# Проверяем были ли ошибки, если ошибки были выводим их
if (count($err) != 0) {
echo "<b>Ошибки:</b><br/>";
foreach ($err as $error) echo $error."<br/>";
echo "<br><a href=\"javascript:history.go(-1);\">Вернуться назад</a>";
}
# Если ошибок небыло заносим данные в базу
else {
# Записываем вопрос в базу
$q = mysql_query("INSERT INTO `poll_question` SET quest_name = '".$quest."', quest_act = 1") or die (mysql_error());
unset($q);
# Узнаем ID только что записанного вопроса
$q = mysql_query("SELECT * FROM `poll_question` WHERE quest_name = '".$quest."'") or die(mysql_error());
$row = mysql_fetch_array($q);
$id_quest = $row['quest_id'];
unset($q,$row);
#Записываем в базу варианты ответа
foreach ($variant as $var)
$q = mysql_query("INSERT INTO `poll_variant` SET var_id_quest = ".$id_quest.", var_name = '".$var."', var_voice = 0") or die(mysql_error());
echo 'Опрос успешно добавлен!<br/>';
echo '<a href="/journal.php">Добавить еще?</a>';
}
}
?>
CREATE TABLE `poll_question` (
`quest_id` int(5) NOT NULL auto_increment,
`quest_name` varchar(255) NOT NULL,
`quest_act` int(1) NOT NULL,
PRIMARY KEY (`quest_id`)
);
CREATE TABLE `poll_variant` (
`var_id` int(5) NOT NULL auto_increment,
`var_id_quest` int(5) NOT NULL,
`var_name` varchar(255) NOT NULL,
`var_voice` int(4) NOT NULL,
PRIMARY KEY (`var_id`)
);
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question