gpt4 book ai didi

php - 将多个字段的值相加然后插入 php 中的 MySQl 列

转载 作者:行者123 更新时间:2023-11-30 22:18:14 24 4
gpt4 key购买 nike

我有一个用 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();

?>

最佳答案

例如尝试这样的事情。我锻炼它对我来说效果很好

<?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();

?>

关于php - 将多个字段的值相加然后插入 php 中的 MySQl 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37517541/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com