我正在尝试学习如何使用HTML,PHP和MYSQL,并尝试使用这三种语言制作一个投票网站。我的HTML和MYSQL代码都可以正常工作,但我的PHP代码无法正常工作。我认为问题在于我使用的是单选按钮,而不是候选人的文本输入,但我不知道如何转换数据。
HTML代码:
<html>
<head>
<title>U.S Election 2016</title>
</head>
<body>
<h1>U.S Election 2016 - Voting Page</h1>
<form action="insert_vote.php" method="post">
<table border="0">
<tr>
<td>Voter ID</td>
<td><input type="text" name="VoterID" maxlength="13" size="13" /></td>
</tr>
<tr>
<td>Candidate :</td>
<td>
<input type="radio" name="Candidate" value="Hillary Clinton" checked>
Hillary Clinton<br>
<input type="radio" name="Candidate" value="Donald Trump" checked>
Donald Trump<br>
<input type="radio" name="Candidate" value="Gary Johnson" checked>
Gary Johnson<br>
<input type="radio" name="Candidate" value="Jill Stein" checked>
Jill Stein<br>
<input type="radio" name="Candidate" value="None" checked>
Undecided/None of the Above<br>
</td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Cast Vote" /></td>
</tr>
</table>
</form>
</body>
</html>PHP代码:
<html>
<head>
<title>U.S Election 2016</title>
</head>
<body>
<h1>U.S Election 2016 - Voting Page</h1>
<?php
// create short variable names
$VoterID=$_POST['VoterID'];
$Candidate=$_POST['Candidate'];
if (!$VoterID || !$Candidate) {
echo "You have not entered all the required details.<br />"
."Please go back and try again.";
exit;
}
if (!get_magic_quotes_gpc()) {
$VoterID = intval($VoterID);
$Candidate = addslashes($Candidate);
}
@ $db = new mysqli('localhost', 'Voting2016', 'Voting123', 'voting');
if (mysqli_connect_errno()) {
Echo "Error: Could not connect to database. Please try again later.";
exit;
}
$query = "insert into voters values
('".$VoterID."', '".$Candidate."')";
$result = $db->query($query);
if ($result) {
echo $db->affected_rows." Vote has been cast and stored into database.";
} else {
Echo "An error has occurred. Your vote was not added.";
}
$db->close();
?>
</body>
</html>每次我提交表单时,它都会问我是否要下载PHP文件:

发布于 2016-12-02 09:05:03
看起来你的PHP解释器没有运行。如果它告诉你下载一个PHP文件,很可能是因为没有任何东西能解释它。查看WAMP (Linux上的LAMP)使其正确启动和运行。
编辑:看一下代码,你似乎对编程很陌生。我建议您考虑一些编程概念,例如MVC。你需要分离数据,逻辑和表示,否则它将是一片混乱,混乱会产生bug。PHP允许您做一些令人讨厌的事情,并且在不太了解编程的情况下开始使用此语言可能会让您养成一些坏习惯,因此请注意:)
https://stackoverflow.com/questions/40900865
复制相似问题