我想要输出的总分。我已经编写了php程序,以便将得分分配给每个年龄和腰部。例如,如果我输入年龄为21,则得分为10,如果我输入年龄为71然后分数是11,依此类推。如果我输入的数字是21和71,那么我应该得到的总分是21。但是我得到的总分是0。
为什么?
任何人都可以帮忙。例如
Enter your age:21
Enter your waist:71
提交输出后应如下:
the score of age is 10
the score of waist is 11
total score is 21.
但是我得到了
the score of age is 10
the score of waist is 11
total score is 0.
T2Dsystem.html
<html>
<body>
<head>
<title>DIABETES RISK SCORE SYSTEM</title>
</head>
<body bgcolor="lightgreen" text="black" style="font-size:18pt font-family:Garamond"><center>
<h2>DIABETES RISK SCORE SYSTEM</h2></center>
<form action="if.php" method="post"> <br/><br/>
Enter your name:<input type="text" name="fname"pattern="[A-Za-z]+" required /><br> <br>
<br/>Enter your Gender:<br/><input type="radio" name="fgender"value="female" checked />Female<br>
<input type=radio name="fgender" value=male>Male</td><br> <br>
<label for="age">Enter your Age: </label>
<select name="age">
<option value="">--select the age--</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
</select>
<br/><br/><br/> <label for="fwaist">Enter your Waist in cm's: </label>
<select name="fwaist">
<option value="">--select the waist--</option>
<option value="71">71</option>
<option value="72">72</option>
<option value="73">73</option>
</select>
<br/><br/><br/><input type="submit"/>
</form>
</body>
</html>
if.php
<html>
<body>
<?php
$age = array("21" => 10, "22" => 20, "23" => 30);
$fwaist = array("71" => 11, "72" => 21, "73" => 31);
$conn=new mysqli('localhost','root','');
if($conn->connect_error){
die("connection failed" .$conn->connect_error);
}
echo "\n DB connected successfully";
mysqli_select_db($conn,"onepage_db");
echo "\n DB is selected as Test successfully";
$sql="INSERT INTO onepage_table (fname,fgender,age,fwaist) VALUES('$_POST[fname]','$_POST[fgender]','$_POST[age]','$_POST[fwaist]')";
if($conn->query($sql)===TRUE){
echo "\n New record created successfully \n";
} else {
echo "Error:" .$sql."<br>" .$conn->error;
}
if("$_POST[age]"==21)
{
echo "\n score of age is ".$age['21']."<br/>";
}
elseif("$_POST[age]"==22)
{
echo "\n the score of age is ".$age['22']."<br/>";
}
else
{
echo"\n the score of age is ".$age['23']."<br/>";
}
if("$_POST[fwaist]"==71)
{
echo "\n score of waist is ".$fwaist['71']."<br/>";
}
elseif("$_POST[fwaist]"==72)
{
echo "\n the score of waist is ".$fwaist['72']."<br/>";
}
else
{
echo"\n the score of waist is ".$fwaist['73']."<br/>";
}
if(isset($_POST['submit'])){
$x1 = $_POST["age"];
$x2 = $_POST["fwaist"];
}
$total=$x1+$x2;
echo "\n total score is ",$total;
mysqli_close($conn);
?>
</body>
</html>
i expect the output like 10+11=21 , but the actual output is 0.
答案 0 :(得分:0)
您需要给您的提交输入一个名称
<input type="submit" name="submit"/>
您还应该更新PHP,以确保它不会引起错误或意外行为。您有很多错误,我发表了评论,以便您可以看到更改。
<html>
<body>
<?php
$age = array("21" => 10, "22" => 20, "23" => 30);
$fwaist = array("71" => 11, "72" => 21, "73" => 31);
$conn=new mysqli('localhost','root','');
if($conn->connect_error){
die("connection failed" .$conn->connect_error);
}
echo "\n DB connected successfully";
mysqli_select_db($conn,"onepage_db");
echo "\n DB is selected as Test successfully";
$sql="INSERT INTO onepage_table (fname,fgender,age,fwaist) VALUES('{$_POST['fname']}','{$_POST['fgender']}','{$_POST['age']}','{$_POST['fwaist']}')";
// Key needs to be wrapped in quotes, when concating strings with array values like that you need to wrap them in curly brackets {}
if($conn->query($sql)){ // No need to add === TRUE you can simply check like this
echo "\n New record created successfully \n";
} else {
echo "Error:" .$sql."<br>" .$conn->error;
}
// anytime you are calling $_POST you should check to see if you have actualyl submitted the form.
if(isset($_POST['submit'])){
// Check first if present in the array, if not display not found, if found display value.
if(isset($age[$_POST['age']])){
echo "\n score of age is ".$age[$_POST['age']]."<br/>";
}else{
echo "\n score of age not found <br/>";
}
if(isset($fwaist[$_POST['fwaist']])){
echo "\n score of waist is ".$fwaist[$_POST['fwaist']]."<br/>";
}else{
echo "\n score of waist not found <br/>";
}
// calculate total and display
$total = $age[$_POST['age']] + $fwaist[$_POST['fwaist']];
echo "\n total score is " . $total;
}
mysqli_close($conn);
?>
</body>
</html>
答案 1 :(得分:0)
很难说明您的示例,但我敢打赌这两个值都是字符串而不是整数。
不管是不是问题所在,还是要从用户输入中获取值之后再执行其他任何操作。
1-确保它们存在
if (!isset($_POST['age'])) {
// we don't have an age at all, something is wrong exit here
}
2-确保将所有用户输入清除后得到的结果,在这种情况下,如果您尝试传递字符串或字符串以外的其他内容,我们可以使用intval来确保我们有一个整数。 int,您将得到一个错误,因此请确保您也能处理该错误。
if (is_string($_POST['age']) || is_int($_POST['age'])) {
// your safe
$int_age = intval($_POST['age']);
}
对所有值进行此操作,然后将其添加。