我正在设置调查问卷,以便在我们的常见问题解答页面底部,我是PHP新手,这是我第一次尝试连接数据库(没有教程)。
我已经设置了HTML页面,设置了PHP,并在MySQL中设置了一个表。
每次提交时数据库都会创建一个新行,但所有行都有“0”而不是分配给输入/ div的值。请帮忙!
编辑:我将HTML更新为现在的形式,但是,当我提交时,我得到了404(并且行根本没有更新。可能有什么问题?
以下是HTML:
<?php
//error_reporting(0);
require 'db/connect.php';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<html>
<head>
<title>State Requirements Feedback</title>
<!--<link rel='stylesheet' type='text/css' href='stylesheet.css'/>-->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script language="javascript">
$(document).ready(function() {
$('.rating').click(function() {
$('.rating').removeClass('selected');
ratingClick(this);
});
});
function ratingClick(that) {
console.log(that.id);
if (that.id == 'rating4' || that.id == 'rating5') {
$('#questions').fadeOut('slow');
$('#thankYou').fadeIn('slow');
} else {
$('#getMore').fadeIn();
$(that).toggleClass('selected');
}
}
$(document).ready(function() {
$('#submit').click(function(){
$('#questions').fadeOut('slow');
$('#thankYou').fadeIn('slow');
});
});
</script>
<style>
.ratings {
float: left;
width: 100%;
}
.rating {
margin: 7px;
font-weight: bold;
background-color: aliceblue;
}
.rating:hover {
background-color:#990000;
color: white;
}
#getMore {
display:none;
clear:both;
background-color:aliceblue;
border:solid black 1px;
padding:0px 5px 5px 10px;
margin:0px 0px 0px 7px;
}
#thankYou {
display:none;
font-weight: bold;
}
.selected {
background-color: #990000;
color: white;
}
textarea {
resize: none;
}
body {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
}
h2 {
margin-bottom: 5px;
font-size: 12px
}
</style>
</head>
<body>
<form id="questions" action="connect.php" method="post">
<h2>How helpful is this article?</h2>
<div class="ratings">
<input type="radio" name="Q1" class="rating" id="rating1" value="1">Not at all helpful
<input type="radio" name="Q1" class="rating" id="rating2" value="2">Not very helpful
<input type="radio" name="Q1" class="rating" id="rating3" value="3">Somewhat helpful
<input type="radio" name="Q1" class="rating" id="rating4" value="4">Very helpful
<input type="radio" name="Q1" class="rating" id="rating5" value="5">Extremely helpful
</div>
<div id="getMore">
<h2>Please tell us why you didn't find this article helpful:</h2>
<input type='checkbox' name="Q2_1" value="1">Not related to my issue<br/>
<input type='checkbox' name="Q2_2" value="1">Too complicated explanations<br/>
<input type='checkbox' name="Q2_3" value="1">Too much information<br/>
<input type='checkbox' name="Q2_4" value="1">Incorrect information<br/>
<input type='checkbox' name="Q2_5" value="1">Unclear information<br/>
<input type='checkbox' name="Q2_6" value="1">Incomplete information<br/>
<h2>Do you have any other feedback about this article?</h2>
<p><input type="text" name="Q3" /><p>
<div id = "submit"><input type='submit' value="Submit" /></div>
</div>
</form>
<div id="thankYou">
Thanks for your feedback!
</div>
</body>
</html>
这是php文档:
<?php
define('DB_NAME', 'staterequirements');
define('DB_USER', 'myuser');
define('DB_PASSWORD', 'mypass');
define('DB_HOST', 'localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if(!$db_selected) {
die('Can\'t use ' . DB_NAME . ' : ' . mysql_error());
}
$Q1 = (isset($_POST['Q1']) ? $_POST['Q1'] : null);
$Q2_1 = (isset($_POST['Q2_1']) ? $_POST['Q2_1'] : null);
$Q2_2 = (isset($_POST['Q2_2']) ? $_POST['Q2_2'] : null);
$Q2_3 = (isset($_POST['Q2_3']) ? $_POST['Q2_3'] : null);
$Q2_4 = (isset($_POST['Q2_4']) ? $_POST['Q2_4'] : null);
$Q2_5 = (isset($_POST['Q2_5']) ? $_POST['Q2_5'] : null);
$Q2_6 = (isset($_POST['Q2_6']) ? $_POST['Q2_6'] : null);
$Q3 = (isset($_POST['Q3']) ? $_POST['Q3'] : null);
$sql = "INSERT INTO response (Q1, Q2_1, Q2_2, Q2_3, Q2_4, Q2_5, Q2_6) VALUES ('$Q1', '$Q2_1', '$Q2_2', '$Q2_3', '$Q2_4', '$Q2_5', '$Q2_6')";
if (!mysql_query($sql)) {
die('Error: ' . mysql_error());
}
mysql_close();
}?>
答案 0 :(得分:2)
这些要点是您问题的一部分。
此:
<div id="questions" action="connect.php" method="post">
应该是<form
而不是div
:
<form id="questions" action="connect.php" method="post">
而且:
<div class="ratings" name="Q1">
<div class="rating" id="rating1" value="1">Not at all helpful</div>
<div class="rating" id="rating2" value="2">Not very helpful</div>
<div class="rating" id="rating3" value="3">Somewhat helpful</div>
<div class="rating" id="rating4" value="4">Very helpful</div>
<div class="rating" id="rating5" value="5">Extremely helpful</div>
</div>
我不确定您是否要使用下拉菜单select
或checkboxes
或radio
按钮或输入,但divs
无效 form elements 强>
我还想指出,强烈建议您使用MySQLi_
和/或PDO代替弃用的MySQL_
,因为您的(已发布)代码 open to injection 强>