Aiuto per sondaggio PHP
-
Salve, io è da tanto che provo a fare funzionare un sondaggio in php ma non riesco a fargli salvare i dati nel database e a farglieli richiamare. vi posto l'intero script:
config.php
<?php $mysql[host] = "localhost"; $mysql[user] = "root"; $mysql[pass] = ""; $mysql[name] = "dbsond"; $question = "Come vi sembra il nostro sito?"; $answers = array("Stupendo", "Bello", "Brutto", "Schifo"); @mysql_connect($mysql[host], $mysql[user], $mysql[pass]); @mysql_select_db($mysql[name]); ?>index.php
<?php require("config.php"); echo '<form action="vote.php" method="post">'; echo "<strong>{$question}</strong><br><br>"; for ($i = 0; $i <= count($answers) - 1; $i++) { echo "<input type=\"radio\" name=\"a\" value=' . ($i + 1) . '>{$answers[$i]}<br>"; } echo '<br><input type="submit" value="Vota">'; echo '<a href="results.php">Risultati</a>'; echo '</form>'; ?>vote.php
<?php require("config.php"); mysql_query("UPDATE sonda SET tot_{$_POST[a]} = tot_{$_POST[a]} + 1"); header("Location: results.php"); ?>results.php
<?php require("config.php"); $query = @mysql_query("SELECT * FROM sonda"); $result = @mysql_fetch_array($query); echo "<strong>{$question}</strong><br><br>"; for ($x = 1; $x <= count($answers); $x++) { $total = $total + $result[tot_ . $x]; } for ($i = 1; $i <= count($answers); $i++) { @$percent = $result[tot_ . $i] / $total; $percent = $percent * 100; $percent = number_format($percent, 1); echo "" . $answers[$i - 1] . " (<strong>{$percent}%</strong>)<br />"; } ?>qualcuno sa perchè non vengono aggiunti i dati al database e neanche ripresi?
Ringrazio in anticipo. -
a scusate mi sono dimenticato il file della tabelle che cè nel database:
install.php
<?php require("config.php"); mysql_query("CREATE TABLE sonda ( id INT( 11 ) NOT NULL AUTO_INCREMENT , tot_1 VARCHAR( 255 ) DEFAULT 0 NOT NULL , tot_2 VARCHAR( 255 ) DEFAULT 0 NOT NULL , tot_3 VARCHAR( 255 ) DEFAULT 0 NOT NULL , tot_4 VARCHAR( 255 ) DEFAULT 0 NOT NULL , INDEX (id));"); mysql_query("INSERT INTO sonda VALUES ('',0,0,0,0)"); echo "Linstallazione è avvenuta con successo!<br />"; ?>
Ciao! Sembra che tu sia interessato a questa conversazione, ma non hai ancora un account.
Stanco di dover scorrere gli stessi post a ogni visita? Quando registri un account, tornerai sempre esattamente dove eri rimasto e potrai scegliere di essere avvisato delle nuove risposte (tramite email o notifica push). Potrai anche salvare segnalibri e votare i post per mostrare il tuo apprezzamento agli altri membri della comunità.
Con il tuo contributo, questo post potrebbe essere ancora migliore 💗
Registrati Accedi