我有一个用PHP和MySQL构建的多页调查表。每个页面都要求用户在调查结束时选择1到10之间的数字,然后将数据插入到MySQl数据库中。
我需要做的是创建一个额外的列,将所有2个字段的值相加并将其插入total_score字段。
如果我能指出正确的方向,我会有点卡住。
我的代码如下:
<?php
session_start(); // Session starts here.
$servername = "";
$username = "";
$password = "";
$dbname = "";
$_SESSION['answer_20'] = $_POST['answer_20'];
$firstname = $_SESSION['firstname'] ;
$lastname = $_SESSION['lastname'];
$gender = $_SESSION['gender'];
$postcode = $_SESSION['postcode'];
$ethnicity = $_SESSION['ethnicity'] ;
$lbote = $_SESSION['lbote'];
$atsi = $_SESSION['atsi'];
$numberofchildreninncare = $_SESSION['numberofchildreninncare'] ;
$youngestchild = $_SESSION['youngestchild'];
$ifother = $_SESSION['ifother'];
$eldestchild = $_SESSION['eldestchild'];
$ageofchild = $_SESSION['ageofchild'];
$relationship = $_SESSION['relationship'];
$education = $_SESSION['education'];
$liveshome = $_SESSION['liveshome'];
$answer_01 = $_SESSION['answer_01'];
$answer_02 = $_SESSION['answer_02'];
$answer_03 = $_SESSION['answer_03'];
$answer_04 = $_SESSION['answer_04'];
$answer_05 = $_SESSION['answer_05'];
$answer_06 = $_SESSION['answer_06'];
$answer_07 = $_SESSION['answer_07'];
$answer_08 = $_SESSION['answer_08'];
$answer_09 = $_SESSION['answer_09'];
$answer_10 = $_SESSION['answer_10'];
$answer_11 = $_SESSION['answer_11'];
$answer_12 = $_SESSION['answer_12'];
$answer_13 = $_SESSION['answer_13'];
$answer_14 = $_SESSION['answer_14'];
$answer_15 = $_SESSION['answer_15'];
$answer_16 = $_SESSION['answer_16'];
$answer_17 = $_SESSION['answer_17'];
$answer_18 = $_SESSION['answer_18'];
$answer_19 = $_SESSION['answer_19'];
$answer_20 = $_SESSION['answer_20'];
$client_id = $_SESSION['client_id'];
$family_id = $_SESSION['family_id'];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO results (firstname,lastname,gender,postcode,ethnicity,lbote,atsi,numberofchildreninncare,youngestchild,ageofchild,ifother,eldestchild,relationship,education,liveshome,answer_01,answer_02,answer_03,answer_04,answer_05,answer_06,answer_07,answer_08,answer_09,answer_10,answer_11,answer_12,answer_13,answer_14,answer_15,answer_16,answer_17,answer_18,answer_19,answer_20,client_id,family_id,parent_subscale,connect_subscale,total_score)
VALUES ('$firstname','$lastname','$gender','$postcode','$ethnicity','$lbote','$atsi','$numberofchildreninncare','$youngestchild','$ageofchild','$ifother','$eldestchild','$relationship','$education','$liveshome','$answer_01','$answer_02','$answer_03','$answer_04','$answer_05','$answer_06','$answer_07','$answer_08','$answer_09','$answer_10','$answer_11','$answer_12','$answer_13','$answer_14','$answer_15','$answer_16','$answer_17','$answer_18','$answer_19','$answer_20','$client_id','$family_id','$parent_subscale','$connect_subscale','$total_score')";
$today = date("d M Y");
if ($conn->query($sql) === TRUE) {
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
答案 0 :(得分:0)
示例尝试类似这样的东西.i锻炼这对我来说很好用
<?php
session_start(); // Session starts here.
$servername = "";
$username = "";
$password = "";
$dbname = "";
$_SESSION['answer_20'] = $_POST['answer_20'];
$firstname = $_SESSION['firstname'] ;
$lastname = $_SESSION['lastname'];
$gender = $_SESSION['gender'];
$postcode = $_SESSION['postcode'];
$ethnicity = $_SESSION['ethnicity'] ;
$lbote = $_SESSION['lbote'];
$atsi = $_SESSION['atsi'];
$numberofchildreninncare = $_SESSION['numberofchildreninncare'] ;
$youngestchild = $_SESSION['youngestchild'];
$ifother = $_SESSION['ifother'];
$eldestchild = $_SESSION['eldestchild'];
$ageofchild = $_SESSION['ageofchild'];
$relationship = $_SESSION['relationship'];
$education = $_SESSION['education'];
$liveshome = $_SESSION['liveshome'];
$client_id = $_SESSION['client_id'];
$family_id = $_SESSION['family_id'];
$total_score=0;
for($i=1;$i<=20;$i++)
{
if($i<10)
{
$no = "0".$i;
$answer_.$no='';
if(isset($_SESSION['answer_'. $no]))
{
$answer_.$no =$_SESSION['answer_'. $no];
$total_score += $_SESSION['answer_'. $no];
}
}
else
{
$answer_.$i='';
if(isset($_SESSION['answer_'. $i]))
{
$answer_.$i =$_SESSION['answer_'. $i];
$total_score += $_SESSION['answer_'. $i];
}
}
}
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO results (firstname,lastname,gender,postcode,ethnicity,lbote,atsi,numberofchildreninncare,youngestchild,ageofchild,ifother,eldestchild,relationship,education,liveshome,answer_01,answer_02,answer_03,answer_04,answer_05,answer_06,answer_07,answer_08,answer_09,answer_10,answer_11,answer_12,answer_13,answer_14,answer_15,answer_16,answer_17,answer_18,answer_19,answer_20,client_id,family_id,parent_subscale,connect_subscale,total_score)
VALUES ('$firstname','$lastname','$gender','$postcode','$ethnicity','$lbote','$atsi','$numberofchildreninncare','$youngestchild','$ageofchild','$ifother','$eldestchild','$relationship','$education','$liveshome','$answer_01','$answer_02','$answer_03','$answer_04','$answer_05','$answer_06','$answer_07','$answer_08','$answer_09','$answer_10','$answer_11','$answer_12','$answer_13','$answer_14','$answer_15','$answer_16','$answer_17','$answer_18','$answer_19','$answer_20','$client_id','$family_id','$parent_subscale','$connect_subscale','$total_score')";
$today = date("d M Y");
if ($conn->query($sql) === TRUE) {
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>