您现在的位置是:网站首页 > 学无止境

简单PHP投票系统 手写代码

ImdaM 2020-04-20 03:40:36 0人围观
简介简单PHP投票系统手写代码votohtml代码:

简单PHP投票系统 手写代码

voto.html代码:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="addvoto.php" method="post">
<table>
<tr>
<td>投票标题</td>
<td>
<input name="title" />
</td>
</tr>
<tr>
<td>1</td>
<td><input name="key1" placeholder="请输入投票选项"/></td>
</tr>
<tr>
<td>2</td>
<td><input name="key2" placeholder="请输入投票选项"/></td>
</tr>
<tr>
<td>3</td>
<td><input name="key3" placeholder="请输入投票选项"/></td>
</tr>
<tr>
<td colspan="2"><input type="button" value="添加选项" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="创建投票" /></td>
</tr>
</table>
</form>
</body>
</html>


添加投票入库:addvoto.php

<?php
header("Content-type:text/html;charset=utf8");
$pdo=new PDO("mysql:host=127.0.0.1;dbname=test","root","root");

isset($_POST['title']) ? $title = $_POST['title'] : $title='' ;
isset($_POST['key1']) ? $key1 = $_POST['key1'] : $key1='' ;
isset($_POST['key2']) ? $key2 = $_POST['key2'] : $key2='' ;
isset($_POST['key3']) ? $key3 = $_POST['key3'] : $key3='' ;


$option = array(
'key1' => $key1,
'key2' => $key2,
'key3' => $key3
);

$option=json_encode($option);

$sql = "INSERT INTO `vote` (`title`,`option`) VALUES ('$title','$option') ";

$num=$pdo->exec($sql);

if($num>0){
echo "<script>alert('创建投票成功!');history.go(-1);</script>";
}


显示投票:showvoto.php

<?php
header("Content-type:text/html;charset=utf8");
$pdo=new PDO("mysql:host=127.0.0.1;dbname=test","root","root");
isset($_GET['vid']) ? $v_id = $_GET['vid'] : $v_id=0 ;
$sql = "SELECT * FROM `vote` WHERE id = $v_id ";
$v_result=$pdo->query($sql)->fetch(PDO::FETCH_ASSOC);

$arr=json_decode($v_result['option'],true);

?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="dovoto.php" method="post">
<table>
<tr>
<td colspan="2" style="color:red">
问题1: <?= $v_result['title'] ?>
<input type="hidden" name="v_id" value="<?= $v_result['id'] ?>">
</td>
</tr>



<?php
$i=1;
foreach ($arr as $k => $v) {
?>
<tr>
<td colspan="2"><input type="radio" name="key" value="<?= $i ?>" /> <?= $i ?> . <?= $v ?></td>
</tr>
<?php
$i++;
}
?>

<tr>
<td colspan="2"><input type="submit" value="提交投票" /></td>
</tr>
</table>
</form>

</body>
</html>


上一篇:php CURL简单代码

下一篇:bug(DEBUG)什么意思

文章评论