2012-11-29 65 views
-3

我想寫的如果任何變量是x(因爲在他們沒有設置),然後返回一條消息,「你錯過了一個問題」。任何人都可以幫助我的語法?

我已經嘗試設置變量$r有我需要的,然後在下面的$Q18行配售消息 - if $Q1 == "x"然後echo $r但這並沒有工作。

任何人都可以提供有用的幫助嗎?整個代碼如下...

<!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"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<link href="Locus.css" rel="stylesheet" type="text/css" /> <!-- LINKED ATTACHED STYLE SHEET--> 
<title>Locus Test Complete</title> 
</head> 


<body> 
<H4>Thank you for completing this test, please choose another from the menu.</H4> 
<?php 


/*DECLARING THE VARIABLES FOR ACCESS*/ 
$username="root"; 
$password=""; 
$database_server="localhost"; 


/*CONNECTING TO THE SERVER*/ 
$database="locus"; 
mysql_connect($database_server,$username,$password) or die("cannot connect"); 

/*CONNECTING TO THE DATABASE*/ 
mysql_select_db($database) or die("Unable to select database"); 


/*CHECK THE STATUS AND DECLARE THE VARIABLES*/ 
if(isset($_POST['surname'])) { 
    $Surname = $_POST['surname']; 
} else { 
    $Surname = ""; 
} 

if(isset($_POST['prison'])) { 
    $Prison = $_POST['prison']; 
} else { 
    $Prison = ""; 
} 
/*SHORTHAND WAY TO CHECK THE STATUS AND DECLARE THE VARIABLES*/ 
$NI = isset($_POST['NI']) ? $_POST['NI'] : "x"; 
$Q1 = isset($_POST['Q1']) ? $_POST['Q1'] : "x"; 
$Q2 = isset($_POST['Q2']) ? $_POST['Q2'] : "x"; 
$Q3 = isset($_POST['Q3']) ? $_POST['Q3'] : "x"; 
$Q4 = isset($_POST['Q4']) ? $_POST['Q4'] : "x"; 
$Q5 = isset($_POST['Q5']) ? $_POST['Q5'] : "x"; 
$Q6 = isset($_POST['Q6']) ? $_POST['Q6'] : "x"; 
$Q7 = isset($_POST['Q7']) ? $_POST['Q7'] : "x"; 
$Q8 = isset($_POST['Q8']) ? $_POST['Q8'] : "x"; 
$Q9 = isset($_POST['Q9']) ? $_POST['Q9'] : "x"; 
$Q10 = isset($_POST['Q10']) ? $_POST['Q10'] : "x"; 
$Q11 = isset($_POST['Q11']) ? $_POST['Q11'] : "x"; 
$Q12 = isset($_POST['Q12']) ? $_POST['Q12'] : "x"; 
$Q13 = isset($_POST['Q13']) ? $_POST['Q13'] : "x"; 
$Q14 = isset($_POST['Q14']) ? $_POST['Q14'] : "x"; 
$Q15 = isset($_POST['Q15']) ? $_POST['Q15'] : "x"; 
$Q16 = isset($_POST['Q16']) ? $_POST['Q16'] : "x"; 
$Q17 = isset($_POST['Q17']) ? $_POST['Q17'] : "x"; 
$Q18 = isset($_POST['Q18']) ? $_POST['Q18'] : "x"; 



/*PLACE THE VALUES OF THE VARIABLES WITHIN THE DATABASE TABLE*/ 
$query = "INSERT INTO locusofcontrolscores 
VALUES(Null,'$Surname','$Prison','$NI','$Q1','$Q2','$Q3','$Q4','$Q5','$Q6','$Q7','$Q8','$Q9','$Q10','$Q11','$Q12','$Q13','$Q14','$Q15','$Q16','$Q17','$Q18')"; 


/*CREATE A QUERY FROM THE VARIABLE*/ 
mysql_query($query) or die(mysql_error()); 

/*CLOSE THE SERVER CONNECTION*/ 
mysql_close(); 



?> 



</body> 
</html> 
+1

你已經錯過了問題 – zerkms

+0

@zerkms我們發現他錯過了! –

+0

問題在頂部。 –

回答

0

您可以嘗試以下操作。

isset()檢查的結果分配給一個數組,然後遍歷該數組以找到任何具有x值的元素。

$arr['NI'] = isset($_POST['NI']) ? $_POST['NI'] : "x"; 
$arr['Q1'] = isset($_POST['Q1']) ? $_POST['Q1'] : "x"; 
$arr['Q2'] = isset($_POST['Q2']) ? $_POST['Q2'] : "x"; 
$arr['Q3'] = isset($_POST['Q3']) ? $_POST['Q3'] : "x"; 

等等...

foreach($arr as $key => $item) 
{ 
    if ($item == 'x') 
    { 
     echo "You have missed question $key"; 
    } 
} 

//展開後回答

/*SHORTHAND WAY TO CHECK THE STATUS AND DECLARE THE VARIABLES*/ 
$answers = array(); // Declare array and assign values to it 
$answers['NI'] = isset($_POST['NI']) ? $_POST['NI'] : "x"; 
$answers['Q1'] = isset($_POST['Q1']) ? $_POST['Q1'] : "x"; 
$answers['Q2'] = isset($_POST['Q2']) ? $_POST['Q2'] : "x"; 
$answers['Q3'] = isset($_POST['Q3']) ? $_POST['Q3'] : "x"; 
$answers['Q4'] = isset($_POST['Q4']) ? $_POST['Q4'] : "x"; 
$answers['Q5'] = isset($_POST['Q5']) ? $_POST['Q5'] : "x"; 
$answers['Q6'] = isset($_POST['Q6']) ? $_POST['Q6'] : "x"; 
$answers['Q7'] = isset($_POST['Q7']) ? $_POST['Q7'] : "x"; 
$answers['Q8'] = isset($_POST['Q8']) ? $_POST['Q8'] : "x"; 
$answers['Q9'] = isset($_POST['Q9']) ? $_POST['Q9'] : "x"; 
$answers['Q10'] = isset($_POST['Q10']) ? $_POST['Q10'] : "x"; 
$answers['Q11'] = isset($_POST['Q11']) ? $_POST['Q11'] : "x"; 
$answers['Q12'] = isset($_POST['Q12']) ? $_POST['Q12'] : "x"; 
$answers['Q13'] = isset($_POST['Q13']) ? $_POST['Q13'] : "x"; 
$answers['Q14'] = isset($_POST['Q14']) ? $_POST['Q14'] : "x"; 
$answers['Q15'] = isset($_POST['Q15']) ? $_POST['Q15'] : "x"; 
$answers['Q16'] = isset($_POST['Q16']) ? $_POST['Q16'] : "x"; 
$answers['Q17'] = isset($_POST['Q17']) ? $_POST['Q17'] : "x"; 
$answers['Q18'] = isset($_POST['Q18']) ? $_POST['Q18'] : "x"; 

$error = ""; 
// Check if any of the answers are 'x' 
foreach ($answers as $key => $item) 
{ 
    if ($item == 'x') 
    { 
     $error .= "You have missed question $key.<br/>"; 
    } 
} 

if ($error == "") // No Errors 
{ 
    extract($answers); // Thanks Baba -- Converts array into variables 

/*PLACE THE VALUES OF THE VARIABLES WITHIN THE DATABASE TABLE*/ 
$query = "INSERT INTO locusofcontrolscores 
VALUES(Null,'$Surname','$Prison','$NI','$Q1','$Q2','$Q3','$Q4','$Q5','$Q6','$Q7','$Q8','$Q9','$Q10','$Q11','$Q12','$Q13','$Q14','$Q15','$Q16','$Q17','$Q18')"; 


/*CREATE A QUERY FROM THE VARIABLE*/ 
mysql_query($query) or die(mysql_error()); 

} else { 
    echo $error; // There was one or more 'x' - display error or deal with it in some other way 
} 

/*CLOSE THE SERVER CONNECTION*/ 
mysql_close(); 
+0

$ Q1 - $ Q18是帶有5個選項的單選按鈕,這是否會導致問題? –

+0

什麼樣的問題? – Baba

+0

@ArrConnor你需要描述一下問題。我們看不到您的輸出。 – Kami

相關問題