任何人都可以用语法帮助我吗?

时间:2012-11-29 10:06:41

标签: php

如果任何变量都是x(我们没有设置),我试着写,然后返回一条消息,“你错过了一个问题”。

我尝试使用我需要的邮件设置变量$r,然后在$Q18行下方放置 - if $Q1 == "x"然后echo $r,但这不起作用。< / p>

有人可以提供有用的帮助吗?整个代码如下......

<!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 个答案:

答案 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();