我想尝试三个问题是或否问题并将它们发布到数据库中以便我可以在以后分析趋势,我目前总计由JS计算,结果由Jquery显示,但我想要向前迈出一步,抓住三个问题的信息,但我没有成功,我没有收到错误信息但是没有在数据库中创建条目。然而JS仍然在工作。有没有人对我错在哪里有任何想法?提前谢谢!
<?php
$host="XXXXXXXX"; // Host name
$username="XXX"; // Mysql username
$password="XXX"; // Mysql password
$db_name="XXXXXX"; // Database name
$tbl_name="test_mysql"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Get values from form
$question0=$_POST['question1'];
$question1=$_POST['question2'];
$question2=$_POST['question3'];
// Insert data into mysql
$sql="INSERT INTO $tbl_name(Q1, Q2, Q3)VALUES('$question0', '$question1', '$question2')";
$result=mysql_query($sql);
?>
<p class="click" id="blue"> You are only charged interest on the amount that is remaining at the end of the month<br>
<input type="radio" name="question0" value="A" class="click" id="blue"> True </radio>
<input type="radio" name="question0" value="B" class="click" id="blue"> False </radio>
<p class="click" id="blue">I have to pay off the balance in full every month <br>
<input type="radio" name="question1" value="A" class="click" id="blue"> True </radio>
<input type="radio" name="question1" value="B" class="click" id="blue"> False </radio>
<p class="click" id="blue">If I don't make a payment my credit score will be unaffected<br id="blue">
<input type="radio" name="question2" value="A" class="click" id="blue"> True </radio>
<input type="radio" name="question2" value="B" class="click" id="blue"> False </radio></p>
<button type="button" class="btn btn-primary btn-xl page-scroll" onclick = "returnScore()" id="click" width="560px">Results</button>
&#13;
答案 0 :(得分:1)
首先,您可以使用ENUM
类型来存储类似于以下模式的答案:
CREATE TABLE answers (
id int NOT NULL AUTO_INCREMENT,
q1 enum('true','false') DEFAULT 'false',
q2 enum('true','false') DEFAULT 'false',
q3 enum('true','false') DEFAULT 'false',
primary key (id)
);
然后在PHP中你可以这样做:
$question1 = $_POST['question1'] == 'A' ? 'true' : false;
$question2 = $_POST['question2'] == 'A' ? 'true' : false;
$question3 = $_POST['question3'] == 'A' ? 'true' : false;
同样非常重要:
mysql_*
PHP扩展已被弃用多年,在PHP7中甚至不再存在。请考虑升级到mysqli_*
甚至更好,PDO。