2016-05-30 89 views
1

我有一個多頁面的調查表單,我已經在PHP和MySQL中構建。每個頁面要求用戶在調查結束時選擇1到10之間的數字,以便將數據插入到MySQl數據庫中。添加多個字段的值,然後插入到MySQl列php

我需要做的是創建一個額外的列,將所有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

哪一個(變量名?)? – JYoThI

+0

你好,基本上我想加上answer_01的數字/值到answer_20然後插入int到total_score列 – Phil

+0

試試我更新的答案@Phil – JYoThI

回答

0

例如嘗試這樣的事情。我的鍛鍊這個工作的罰款,我想要此外

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

    ?> 
+0

嗨jothi謝謝你的回覆,這很棒,我的php不是很棒,你能建議如何將它添加到我的原始文章中的代碼中? – Phil

+0

等待我會爲你做 – JYoThI

+0

@ jothi非常感謝你,但是我收到了關於sumit的錯誤'Warning:session_start():無法發送會話緩存限制器 - 已發送的頭文件'和'致命錯誤:最大執行時間第44行的/home/username/public_html/folder/complete.php超出了30秒的任何想法? – Phil

相關問題